Presentation is loading. Please wait.

Presentation is loading. Please wait.

CS4273: Distributed System Technologies and Programming I Lecture 11: JavaServer Pages (JSP)

Similar presentations


Presentation on theme: "CS4273: Distributed System Technologies and Programming I Lecture 11: JavaServer Pages (JSP)"— Presentation transcript:

1 CS4273: Distributed System Technologies and Programming I Lecture 11: JavaServer Pages (JSP)

2 2 JavaServer Pages (JSP) Why JSP? Get rid of Java programming. Separate presentation from business logic. Reuse predefined components, such as JavaBean, tag libraries. How does JSP work? The entire JSP page gets translated into a Servlet (once) at the first time it is invoked. It is the Servlet code that gets executed at each request. No interpretation of JSP occurs at request time. The original JSP page is totally ignored at request time. JSP tutorial website:

3 3 Example of a Translated JSP The original JSP A Random Number The resulting Servlet code public void _jspService(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html"); HttpSession session = request.getSession(true); JspWriter out = response.getWriter(); out.println(" A Random Number "); out.println(Math.random());... }

4 4 JSP Notations JSP can include Java code, applets, servlets, JavaBeans, etc. JSP Expressions: JSP Scriptlets (for Java code): JSP Declarations: JSP Directives: e.g.,

5 5 The First JSP example JSP files in dir: ~jia/www/java/js/jsp/ URL for run JSP: Simple JSP Test Page Simple JSP Test Page Now, the time is:

6 6 An Example of JSP Scriptlets (Java code) The time returned from a java utility function: <% // This scriptlet declares and initializes "date" java.util.Date date = new java.util.Date(); out.println( date ); out.println( " Your machine's address is " ); // request in next line is in servlet style out.println( request.getRemoteHost()); %>

7 7 An Example of JSP Declaration and Directives <%! Date getDate() { Date theDate = new Date(); return theDate; } %> The time returned from java declared function is:

8 8 Another Example of Scriptlet Example of JSP Scriptlet <% // begin scriptlet String name = request.getParameter( "firstName" ); if ( name != null ) { %> Hello, Welcome to JavaServer Pages! <% // continue scriptlet } // end if else { %> Type your first name and press Submit <% // continue scriptlet } // end else %>

9 9 JSP Directives JSP directives are used for specifying page settings at translation time. Three directives: page, include, and taglib. page directive: define the page setting. e.g., include directive: insert a file at translation-time as if it were originally part of the JSP (note. jsp:include inserts a file at request time!). e.g., taglib directive: specify user-defined tag libraries. e.g.,

10 10 JSP Standard Actions JSP actions allow some common tasks in JSP. JSP containers process actions at request time:. Include a file at request time (Note. The include directive inserts a file at translation time) e.g.,. Forward the processing to another JSP or servlet. This terminates the current JSP’s execution. e.g.,. Allow a browser-object (applets) or html element embedded in jsp. It’s passed to browser without being executed in server! e.g.,

11 11 JSP Standard Actions (Cont.). Pass parameters to another JSP or servlet (used together with include, forward, and plugin).. Specify scope of a JavaBean and assign id. e.g. The next two actions are used with JavaBean instance:

12 12 Example of jsp:forward and jsp:param Forward request to another JSP <% // begin scriptlet String name = request.getParameter( "firstName" ); if ( name != null ) { %> <% } // end if else { %>

Type your first name and press Submit <% } // end else %>

13 13 Example of JSP: forward and jsp:param (Cont.) Processing a jsp:forward request Hello, Your request was received and forwarded at

14 14 Example of JSP: useBean The program numguess.jsp uses a JavaBean object NumberGuessBean.class, which is in …/js/WEB-INF/classes/num. See NumberGuessBean.java for lib routines. Number Guess Congratulations! You got it. And after just tries. Would you try again ? Welcome to the Number Guess game. I‘ve a number between 1 and 100. What's your guess? ….

15 15 Example of JSP Tag Library (taglib) Directive taglib allows user defined tags, facilitating the use of large amount of Java libraries in jsp (separate JSP from Java-code implementations). Three files involved for taglib: 1.JSP file, specifying the use of taglib, e.g., SendMail.jsp: // “send” is prefix you can use 2.Tag Library Descriptor file (.tld) in XML format, taglib.tld, specify format of lib routines, such as routine names, parameters, etc.: Sendmail com.cj.smtp.Sendmail com.cj.smtp.BoolVariable ………… 3.Library routines (java class file), sendmail.jar in …WEB-INF/lib (for you only) or jserv:/usr/jt/lib (for all users). See dir setting in structure.htmlhttp://www.jsptube.com/servlet-tutorials/web-application-directory- structure.html Use “jar –tvf blazix.jar” cmd to see the list of routines in class file.

16 16 JSP JDBC Example JSP JDBC JSP JDBC <% Class.forName("com.mysql.jdbc.Driver"); String url = “ jdbc:mysql://jserv.cs.cityu.edu.hk:3306/db_jdemo” ; Connection con= DriverManager.getConnection(url, "jdemo","apple1"); Statement stmt=con.createStatement(); String sql="select COF_NAME, PRICE from COFFEES"; ResultSet rs=stmt.executeQuery(sql); while(rs.next()) { out.print(" "+ rs.getString("COF_NAME")+" "); out.print(" "+ rs.getString("PRICE")+" "); } %>


Download ppt "CS4273: Distributed System Technologies and Programming I Lecture 11: JavaServer Pages (JSP)"

Similar presentations


Ads by Google