Presentation is loading. Please wait.

Presentation is loading. Please wait.

JSP Based on http://www.jsptut.com/ http://www.visualbuilder.com/jsp/tutorial/

Similar presentations


Presentation on theme: "JSP Based on http://www.jsptut.com/ http://www.visualbuilder.com/jsp/tutorial/"— Presentation transcript:

1 JSP Based on http://www.jsptut.com/

2 JSP JavaServer Pages (JSP) is a technology based on the Java language and enables the development of dynamic web sites. JSP offers a robust platform for web development. JSP files are HTML files with special Tags containing Java source code that provide the dynamic content.

3 JSP Syntax mix of two basic content forms:
markup. Markup is typically standard HTML or XML scriptlet elements: delimited blocks of Java code

4 Five Types of Tags Declaration tag: <%! %>
<%! private int counter = 0 ; private String get Account ( int accountNo) ;%> Expression tag <%=   %> Directive directive ... %>: page, include, tag library Scriptlet tag <% ... %> <%          String username = ”cs" ;         out.println ( username ) ;   %> Action tag <jsp : usebean id = " ...." scope = "application" class = "com..." />

5 A Simple HTML File <HTML> </HTML> <BODY>
Hello, world </BODY> </HTML>

6 Expressions Tag Hello.jsp: save it to the jsp directory of the web server, Visit it with browser <HTML> <BODY> Hello! The time is now <%= new java.util.Date() %> </BODY> </HTML> <%= and %> enclose Java expressions, which are evaluated at run time

7 Scriptlet Tag <HTML> <BODY> <% // This is a scriptlet. Notice that the "date" // variable we declare here is available in the // embedded expression later on. System.out.println( "Evaluating date now" ); java.util.Date date = new java.util.Date(); %> Hello! The time is now <%= date %> </BODY> </HTML> the output from the "System.out.println" on the server log

8 Use predefined variable “out” to Embed the Output in the HTML
<HTML> <BODY> <% // This scriptlet declares and initializes "date" System.out.println( "Evaluating date now" ); java.util.Date date = new java.util.Date(); %> Hello! The time is now // This scriptlet generates HTML output out.println( String.valueOf( date )); </BODY> </HTML> out is of type javax.servlet.jsp.JspWriter.

9 Another pre-defined variable "request".
<HTML> <BODY> <% // This scriptlet declares and initializes "date" System.out.println( "Evaluating date now" ); java.util.Date date = new java.util.Date(); %> Hello! The time is now out.println( date ); out.println( "<BR>Your machine's address is " ); out.println( request.getRemoteHost()); </BODY> </HTML>

10 Directives Tag <%@ page import="java.util.*" %> <HTML>
<BODY> <% System.out.println( "Evaluating date now" ); Date date = new Date(); %> Hello! The time is now <%= date %> </BODY> </HTML>

11 JSP Include Directive <HTML> <BODY> Going to include hello.jsp...<BR> include file="hello.jsp" %> </BODY> </HTML>

12 Declaration Tag use the <%! and %> sequences to enclose your declarations page import="java.util.*" %> <HTML> <BODY> <%! Date theDate = new Date(); Date getDate() { System.out.println( "In getDate() method" ); return theDate; } %> Hello! The time is now <%= getDate() %> </BODY> </HTML>

13 JSP Sessions A session is an object associated with a visitor
<HTML> <BODY> <FORM METHOD=POST ACTION="SaveName.jsp"> What's your name? <INPUT TYPE=TEXT NAME=username SIZE=20> <P><INPUT TYPE=SUBMIT> </FORM> </BODY> </HTML>

14 SaveName.jsp <% String name = request.getParameter( "username" ); session.setAttribute( "theName", name ); %> <HTML> <BODY> <A HREF="NextPage.jsp">Continue</A> </BODY> </HTML>

15 NextPage.jsp <HTML> <BODY>Hello, <%= session.getAttribute("theName”) %> </BODY> </HTML>

16 Javabean scopes page - valid until page completes.
request - bean instance lasts for the client request session - bean lasts for the client session. application - bean instance created and lasts until application ends.

17 <HTML> <BODY> <FORM METHOD=POST ACTION="SaveName
<HTML> <BODY> <FORM METHOD=POST ACTION="SaveName.jsp"> What's your name? <INPUT TYPE=TEXT NAME=username SIZE=20><BR> What's your address? <INPUT TYPE=TEXT NAME= SIZE=20><BR> What's your age? <INPUT TYPE=TEXT NAME=age SIZE=4><P> <INPUT TYPE=SUBMIT> </FORM> </BODY> </HTML>

18 package user; public class UserData { String username; String ; int age; public void setUsername( String value ){username = value;} public void set ( String value ){ = value;} public void setAge( int value ){age = value;} public String getUsername() { return username; } public String get () { return ; } public int getAge() { return age; } }

19 SaveName.jsp <jsp:useBean id="user" class="user.UserData" scope="session"/> <jsp:setProperty name="user" property="*"/> <HTML> <BODY> <A HREF="NextPage.jsp">Continue</A> </BODY> </HTML>

20 <jsp:useBean id="user" class="user
<jsp:useBean id="user" class="user.UserData" scope="session"/> <HTML> <BODY>You entered<BR> Name: <%= user.getUsername() %><BR> <%= user.get () %><BR> Age: <%= user.getAge() %><BR> </BODY> </HTML>


Download ppt "JSP Based on http://www.jsptut.com/ http://www.visualbuilder.com/jsp/tutorial/"

Similar presentations


Ads by Google