1 / 32

JSP

JSP. Michelle Johnston, Firebird Services Ltd. JSP Pages. HTML page can become a jsp just by changing the extension to jsp Allows java to be run within HTML pages. Directives. <%@page language="java" %> <%@ include file="/header.jsp" %> <%@ taglib uri="tlds/taglib.tld" prefix="mytag" %>

deo
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 Michelle Johnston, Firebird Services Ltd

  2. JSP Pages • HTML page can become a jsp just by changing the extension to jsp • Allows java to be run within HTML pages

  3. Directives • <%@page language="java" %> • <%@ include file="/header.jsp" %> • <%@ taglib uri="tlds/taglib.tld" prefix="mytag" %> • <%@page language="java" import="java.sql.*,mypackage.myclass" %> • <%@page language="java" session="true" %> • <%@page language="java" session="true" errorPage="error.jsp" %> • <%@page language="java" session="true" contentType="text/html;charset=ISO-8859-1" %>

  4. Declaratives • Declarations are embedded within <!%= …%> • <%@ 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>

  5. Declaratives • Here we are declaring a variable theDate • And the method getDate() • Both will now be available in our scriptlets • Warning! Declarations/declaratives are only evaluated ONCE per page – reload and the date remains the same!

  6. Request method • Get request info • <%=request.getMethod()%> • <%=request.getRequestURI()%> • <%=request.getProtocol()%> • <%=request.getPathInfo()%> • <%=request.getQueryString()%>

  7. Changing Date value • <HTML> • <BODY> • Hello!  The time is now <%= new java.util.Date() %> • </BODY> • </HTML> • Refresh the above page and see the date change!

  8. Exercise 1 • Exercise: Write a JSP to output the values returned by • System.getProperty for various system properties such as • java.version, java.home, os.name, user.name, user.home, user.dir etc.

  9. Scriptlets • Get a parameter • <% • //java code • String userName=null; • userName=request.getParameter("userName"); • %>

  10. Scriptlets • <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>

  11. Outputting Info • Note that System.out.println writes to stdout (console) • To write to the html page, we put the value in <%= %> • Another way would be to use out (an object 'given' to you that allows you to write to html)

  12. Outputting Info • <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>

  13. Exercise 2 • Exercise:  Write a JSP to output the entire line, "Hello!  The time is now ..." but use a scriptlet for the complete string, including the HTML tags.

  14. Using Tables • <TABLE BORDER=2> • <%     • for ( int i = 0; i < n; i++ ) • { •   %>         • <TR> •         <TD>Number</TD>         • <TD><%= i+1%></TD> •   </TR>         • <%     • } • %> • </TABLE>

  15. Exercise 3 • Write a JSP to output all the values returned by System.getProperties with "<BR>" embedded after each property name and value.  Do not output the "<BR>" using the "out" variable.

  16. Directives • Don’t fully qualify classes, import them! • <%@ page import="java.util.*" %> • <HTML> • <BODY> • <%     • System.out.println( "Evaluating date now" );     • Date date = new Date(); • %> • Hello!  The time is now <%= date %> • </BODY> • </HTML>

  17. Importing • To import more than one package, simply comma separate them.. • <%@ page import="java.util.*,java.text.*" %>

  18. Includes • Include puts the full text of the included file embedded into the jsp file (can be html/jsp/anything) • <HTML> • <BODY> • Going to include hello.jsp... • <BR> • <%@ include file="hello.jsp" %> </BODY> • </HTML>

  19. Tags • < not <% • <some:tag> body </some:tag> • <some:tag/> (no body to this tag – xml like) • Predefined tags <jsp:.. • <HTML> • <BODY> • Going to include hello.jsp... • <BR> • <jsp:include page="hello.jsp"/> • </BODY> • </HTML>

  20. Exercise 4 • Change the jsp:include to jsp:forward and note the difference. • Write a JSP to do either a forward or an include, depending upon a boolean variable (hint:  The concepts of mixing HTML and scriptlets work with JSP tags also!)

  21. Calling JSP in Forms • <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>

  22. Creating a session • SaveName.jsp saves the name in a session: • <%    String name = request.getParameter( "username" );    • session.setAttribute( "theName", name ); • %> • <HTML> • <BODY> • <A HREF="NextPage.jsp">Continue</A> </BODY> • </HTML>

  23. Retrieving Session Info • NextPage.jsp shows how to retrieve the saved name. • <HTML> • <BODY> • Hello, <%= session.getAttribute( "theName" ) %> • </BODY> • </HTML>

  24. Exercise 5 • Add another attribute age to the whole of this example (all three pages)

  25. Form Processing • Add email and age to the form in GetName.html • <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>

  26. Using Beans • Create a bean (Java class) with username, email and age as fields • Create setter methods (setUsername, setEmail and setAge) • Create getter methods (getUsername, getEmail and getAge)

  27. UserData class • public class UserData {    • String username;     • String email;     • int age;     • public void setUsername( String value)     • {         • username = value;     • }     • public void setEmail( String value )     • {         • email = value;     • }    

  28. UserData Class cont • public void setAge( int value )     • {         age = value;     • }     • public String getUsername() • { return username; }     • public String getEmail() • { return email; }     • public int getAge() • { return age; } • }

  29. Bean Compilation • Compile bean • Put in the classpath of web server • Now let us change "SaveName.jsp" to use a bean to collect the data. • <jsp:useBean id="user" class="UserData" scope="session"/> • <jsp:setProperty name="user" property="*"/>  • <HTML> • <BODY> • <A HREF="NextPage.jsp">Continue</A> </BODY> • </HTML>

  30. Retrieving Bean Data • Let us modify NextPage.jsp to retrieve the data from bean.. • <jsp:useBean id="user" class="UserData" scope="session"/>  • <HTML> • <BODY> • You entered<BR> • Name: <%= user.getUsername() %><BR> • Email: <%= user.getEmail() %><BR> • Age: <%= user.getAge() %><BR> • </BODY> • </HTML>

  31. Exercise 6 • 1)  Write a JSP/HTML set that allows a user to enter the name of a system property, and then displays the value returned by System.getProperty for that property name (handle errors appripriately.)   • 2)  Go back to the exercises where you manually modified boolean variables.  Instead of a boolean variable, make these come from a HIDDEN form field that can be set to true or false.

More Related