1 / 20

JSP

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.

Download Presentation

JSP

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. JSP

  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 tag<%@ 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> <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.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>

  18. 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; } }

  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.UserData" scope="session"/>  <HTML> <BODY>You entered<BR> Name: <%= user.getUsername() %><BR> Email: <%= user.getEmail() %><BR> Age: <%= user.getAge() %><BR> </BODY> </HTML>

More Related