Professional Documents
Culture Documents
What is JSP?
A Java Servlet is a Java program that is run on the
server
tuning of the page must be done in a Java program that needs to be re-compiled Java Server Pages (JSP)
use HTML and XML tags to design the page and JSP
scriplet tags to generate dynamic content (Easier for separation between designer & developer) use Java Beans and useful built-in objects for more convenience
JSP page (MyFirstJSP.jsp) -> Translated to Servlet (MyFirstJSP.servlet) -> Compiled to class (MyFirstJSP.class) -> Loaded into memory (Initialization) -> Execution (repeats) -> Destruction
what you want done with the Java <%= expression %>
The expression is evaluated and the result is inserted into the
HTML page
method
5
Example JSP
<HTML>
<BODY> Hello! The time is now <%= new java.util.Date() %> </BODY> </HTML>
Notes: The <%= ... %> tag is used, because we are computing a value and inserting it into the HTML The fully qualified name (java.util.Date) is used, instead of the short name (Date), because we havent yet talked about how to do import declarations 6
Variables
You can declare your own variables, as usual JSP provides several predefined variables request : The HttpServletRequest parameter response : The HttpServletResponse parameter session : The HttpSession associated with the request, or null if there is none out : A JspWriter (like a PrintWriter) used to send output to the client Example: Your hostname: <%= request.getRemoteHost() %>
7
Scriptlets
the HTML (as is done with <%= ... %>) Scriptlets are Java code that may write into the HTML Example: <% String queryData = request.getQueryString(); out.println("Attached GET data: " + queryData); %>
<% if (Math.random() < 0.5) { %> Have a <B>nice</B> day! <% } else { %> Have a <B>lousy</B> day! <% } %>
8
Declarations
Use <%! ... %> for declarations to be added to your servlet
must be handled with extreme care If declared with <% ... %>, variables are local and OK Data can also safely be put in the request or session objects
Example:
<%! private int accessCount = 0; %> Accesses to page since server reboot: <%= ++accessCount %> You can use <%! ... %> to declare methods as easily as to declare variables
9
Directives
<%@ directivename attribute=value attribute=value %> The page directive <%@ page ATTRIBUTES %> language, import, Buffer, errorPage, <%@ page languange=java import=java.rmi.*,java.util.* %> The include directive <%@ include file=Filename %> the static file name to include (included at translation time) The taglib directive <% taglib uri=taglibraryURI prefix=tagPrefix %>
Implicit Objects
These objects do not need to be declared or instantiated by the
JSP author, but are provided by the container (jsp engine) in the implementation class request Object (javax.servlet.ServletRequest) response Object (javax.servlet.ServletResponse) session Object (javax.servlet.http.HttpSession) application Object out Object config Object page Object pageContext Object (javax.servlet.jsp.PageContext) exception