Transcript No Slide Title
CS6320 – JSP
L. Grewe
1
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.
2
How it works.
JSP engine on server receives request for a .jsp page it: 1.
2.
3.
4.
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.
3
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
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()) 5
2 forms of tags
<%= your_expression %>
OR the XML compliant version
6
JSP Expression Example
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
The time is now <%= new Date() %>.
7
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); %> 8
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 9
Directive Examples
<%@ page language="java“ import =“java.util.*” %> • Imports classes <%@ include file="relative-URL" %> • Includes files
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 11
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 ++;} %> 12
JSP PreDefined Objects
When writing scriptlets and expressions, the following objects (called
implicit object
s) 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
13
JSP Session Example
Visitor Count
This JSP page demonstrates session management by incrementing a counter each time a user accesses a page.
<%!
private int totalHits = 0; %> 14
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); %> 15
JSP Session Example
16
More…..
SEE the course website, and the current JSP api for more details.
17