Download presentation
Presentation is loading. Please wait.
2
1 CS6320 – JSP L. Grewe
3
2 Java Server Pages Servlets require you to write out entire page delivered with print statements Servlets require you to write out entire page delivered with print statements JSP embedded in static html content JSP embedded in static html content File extension.jsp File extension.jsp Not compiled Not compiled Deploy as part of webapp but, location of jsp files variable (see deployment and your server information) Deploy as part of webapp but, location of jsp files variable (see deployment and your server information) Language of tags, can use Java Language of tags, can use Java JSP run as servlets when envoked JSP run as servlets when envoked Invokes special _jspService() method …do not have the doGet (do*) methods. Invokes special _jspService() method …do not have the doGet (do*) methods.
4
3 How it works. JSP engine on server receives request for a.jsp page it: 1. Reads in the page, and transforms the contents to a Servlet 2. Even the static HTML is converted to print statements, printing to the output stream associated with the JSP’s _jspService() method. 3. This translation is done the first time the page is requested. 4. Then the server runs the resulting created Servlet and returns the results to the client.
5
4 Elements of a Java Server Page Expressions: Expressions: expression is evaluated and inserted into Servlet’s output.expression is evaluated and inserted into Servlet’s output. Scriptlets: Scriptlets: This code is directly inserted into the produced Servlet’s _jspService() method.This code is directly inserted into the produced Servlet’s _jspService() method. This method is automatically called by the Servlet’s service() method.This method is automatically called by the Servlet’s service() method. Comments: Comments: User readable comments, not parsed by the JSP CompilerUser 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.Difference between this an HTML comment is this is inserted into the Servlet being produced by evaluating the.jsp file.
6
5 Elements of a Java Server Page Directives: Directives: Provide global information to the page Provide global information to the page Import statements Import statements Scripting language Scripting language Declarations: Declarations: For page-wide variable and method declarationFor page-wide variable and method declaration The code is inserted into the body of the produced Servlet, outside of any existing method (like _jspService())The code is inserted into the body of the produced Servlet, outside of any existing method (like _jspService())
7
6 2 forms of tags OR the XML compliant version <jsp:expression> your_expression your_expression</jsp:expression>
8
7 JSP Expression Example <html> HelloWorld HelloWorld The time is now. The time is now. </html>
9
8 JSP Scriptlets JSP scriptlets are defined as block of Java code embedded between a pair of JSP scriptlets are defined as block of Java code embedded between a pair of tags,. tags,. Example: Example:<% java.util.Date d = new java.util.Date(); out.println(d);%>
10
9 JSP Directives General syntax: Possible values for directives are: Possible values for directives are: Page - Information for the pagePage - Information for the page Include - Specifies the files whose contents are to be included in the output Include - Specifies the files whose contents are to be included in the output e.g., e.g., Taglib Taglib The URI for a library of custom tags that may be used in the pageThe URI for a library of custom tags that may be used in the page
11
10 Directive Examples Imports classesImports classes Includes filesIncludes files Zero or more tags Zero or more tags</jsp:forward> Forwards with parameters to another resourceForwards with parameters to another resource See JSP documentation for more
12
11 Directive Examples (Contd.) true indicates that session data is available to the pagetrue indicates that session data is available to the page By default, this is set to trueBy default, this is set to true Determines the size of the output stream bufferDetermines the size of the output stream buffer Defaults to 8kbDefaults to 8kb Use with autoFlushUse with autoFlush When set to true, flushes the output buffer when it is full, rather than raising an exceptionWhen set to true, flushes the output buffer when it is full, rather than raising an exception
13
12 JSP Declarations Class and instance variables (of the generated servlet class) may be specified using the JSP Declaration tag: 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: Methods may also be specified:<%! private int getNextIndex() {return index ++;} %>
14
13 JSP PreDefined Objects When writing scriptlets and expressions, the following objects (called implicit objects) are available by default: When writing scriptlets and expressions, the following objects (called implicit objects) are available by default: request javax.servlet.http.HttpServletRequest request javax.servlet.http.HttpServletRequest response javax.servlet.http.HttpServletResponse response javax.servlet.http.HttpServletResponse out javax.servlet.jsp.JspWriter out javax.servlet.jsp.JspWriter session javax.servlet.http.HttpSession session javax.servlet.http.HttpSession application javax.servlet.ServletContext application javax.servlet.ServletContext exception java.lang.Throwable exception java.lang.Throwable
15
14 JSP Session Example <html><head><title> Visitor Count -- JSP Session Visitor Count -- JSP Session Visitor Count Visitor Count <p> This JSP page demonstrates session management by incrementing a counter each time a user accesses a page. <p><%! private int totalHits = 0; %>
16
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); %>
17
16 JSP Session Example <center> You have hit this page You have hit this page time, out of a total of page hit ! </font></center><p></font></body></html>
18
17 More….. SEE the course website, and the current JSP api for more details. SEE the course website, and the current JSP api for more details.
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.