270 likes | 418 Views
JSP. 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. JSP Syntax.
E N D
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.
JSP Syntax • mix of two basic content forms: • markup. Markup is typically standard HTML or XML • scriptlet elements: delimited blocks of Java code
Five Types of Tags • Declaration tag: <%! %> <%! private int counter = 0 ; private String get Account ( int accountNo) ;%> • Expression tag <%= %> • Directive tag<%@ directive ... %>: page, include, tag library • Scriptlet tag <% ... %> <% String username = ”cs" ; out.println ( username ) ; %> • Action tag <jsp : usebean id = " ...." scope = "application" class = "com..." />
A Simple HTML File <HTML> <BODY> Hello, world </BODY> </HTML>
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
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
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.
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>
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>
JSP Include Directive <HTML> <BODY> Going to include hello.jsp...<BR> <%@ include file="hello.jsp" %> </BODY> </HTML>
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>
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>
SaveName.jsp <% String name = request.getParameter( "username" ); session.setAttribute( "theName", name ); %> <HTML> <BODY> <A HREF="NextPage.jsp">Continue</A> </BODY> </HTML>
NextPage.jsp <HTML> <BODY>Hello, <%= session.getAttribute("theName”) %> </BODY> </HTML>
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.
<HTML> <BODY> <FORM METHOD=POST ACTION="SaveName.jsp"> What's your name? <INPUT TYPE=TEXT NAME=username SIZE=20><BR> What's your e-mail address? <INPUT TYPE=TEXT NAME=email SIZE=20><BR> What's your age? <INPUT TYPE=TEXT NAME=age SIZE=4><P> <INPUT TYPE=SUBMIT> </FORM> </BODY> </HTML>
package user; public class UserData { String username; String email; int age; public void setUsername( String value ){username = value;} public void setEmail( String value ){email = value;} public void setAge( int value ){age = value;} public String getUsername() { return username; } public String getEmail() { return email; } public int getAge() { return age; } }
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>
<jsp:useBean id="user" class="user.UserData" scope="session"/> <HTML> <BODY>You entered<BR> Name: <%= user.getUsername() %><BR> Email: <%= user.getEmail() %><BR> Age: <%= user.getAge() %><BR> </BODY> </HTML>