1 / 17

CS6320 – JSP

CS6320 – JSP. L. Grewe. Java Server Pages. Servlets require you to write out entire page delivered with print statements JSP embedded in static html content File extension .jsp Not compiled

ginata
Download Presentation

CS6320 – 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. CS6320 – JSP L. Grewe

  2. Java Server Pages • Servlets require you to write out entire page delivered with print statements • JSP embedded in static html content • File extension .jsp • Not compiled • Deploy as part of webapp but, location of jsp files variable (see deployment and your server information) • Language of tags, can use Java • JSP run as servlets when envoked • Invokes special _jspService() method …do not have the doGet (do*) methods.

  3. How it works. JSP engine on server receives request for a .jsp page it: • Reads in the page, and transforms the contents to a Servlet • Even the static HTML is converted to print statements, printing to the output stream associated with the JSP’s _jspService() method. • This translation is done the first time the page is requested. • Then the server runs the resulting created Servlet and returns the results to the client.

  4. Elements of a Java Server Page • Expressions: <%= %> • expression is evaluated and inserted into Servlet’s output. • Scriptlets: <% %> • This code is directly inserted into the produced Servlet’s _jspService() method. • This method is automatically called by the Servlet’s service() method. • Comments: <%-- --%> • User readable comments, not parsed by the JSP Compiler • Difference between this an HTML comment is this is inserted into the Servlet being produced by evaluating the .jsp file.

  5. Elements of a Java Server Page • Directives: <%@ %> • Provide global information to the page • Import statements • Scripting language • Declarations: <%! %> • For page-wide variable and method declaration • The code is inserted into the body of the produced Servlet, outside of any existing method (like _jspService())

  6. 2 forms of tags <%= your_expression %> OR the XML compliant version <jsp:expression> your_expression </jsp:expression>

  7. JSP Expression Example <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title>HelloWorld</title> </head> <body> <p> The time is now <%= new Date() %>. </body> </html>

  8. JSP Scriptlets • JSP scriptlets are defined as block of Java code embedded between a pair of • tags, <% and %>. • Example: <% java.util.Date d = new java.util.Date(); out.println(d); %>

  9. JSP Directives • General syntax: • <%@ directive {attribute = "value"} %> • Possible values for directives are: • Page - Information for the page • Include - Specifies the files whose contents are to be included in the output • e.g., <%@ include file="header.html" %> • Taglib • The URI for a library of custom tags that may be used in the page

  10. Directive Examples <%@ page language="java“ import =“java.util.*” %> • Imports classes <%@ include file="relative-URL" %> • Includes files <jsp:forward page="next.jsp">    Zero or more <jsp:param name="x" value="y"> tags </jsp:forward> • Forwards with parameters to another resource See JSP documentation for more

  11. Directive Examples (Contd.) <%@ page session = "true | false" %> • true indicates that session data is available to the page • By default, this is set to true <%@ page buffer = "none | 16kb | sizekb" %> • Determines the size of the output stream buffer • Defaults to 8kb • Use with autoFlush <%@ page autoFlush = "true | false" %> • When set to true, flushes the output buffer when it is full, rather than raising an exception

  12. JSP Declarations • Class and instance variables (of the generated servlet class) may be specified using the JSP Declaration tag: <%! String name = “Web Applications"; int index = 10; int count = 0; %> • Methods may also be specified: <%! private int getNextIndex() {return index ++;} %>

  13. JSP PreDefined Objects • When writing scriptlets and expressions, the following objects (called implicit objects) are available by default: • request javax.servlet.http.HttpServletRequest • response javax.servlet.http.HttpServletResponse • out javax.servlet.jsp.JspWriter • session javax.servlet.http.HttpSession • application javax.servlet.ServletContext • exception java.lang.Throwable

  14. JSP Session Example <html><head><title> Visitor Count -- JSP Session </title> </head> <body bgcolor="#FFFFFF"> <font face="Helvetica"> <h2> Visitor Count </h2> <p> This JSP page demonstrates session management by incrementing a counter each time a user accesses a page. <p> <%! private int totalHits = 0; %>

  15. JSP Session Example <% session = request.getSession(true); Integer ival = (Integer)session.getValue("jspsession.counter"); if (ival == null) { ival = new Integer(1);} else {ival = new Integer(ival.intValue() + 1);} session.putValue("jspsession.counter", ival); %>

  16. JSP Session Example <center> <font size=+2> You have hit this page <%= ival %> time<%= (ival.intValue() == 1) ? "" : "s" %>, out of a total of <%= ++totalHits %> page hit<%= (totalHits == 1) ? "" : "s" %>! </font> </center> <p> </font> </body> </html>

  17. More….. • SEE the course website, and the current JSP api for more details.

More Related