Kevin Nilson
http://www.javaclimber.com
Abstract
Interview Presentation for: Part-time - Computer and Information
Science Instructor, Monday December 19th, 2005
JSP to Servlet Source Code Generation (Translation)
- Container Generates Servlet Source Code from
JSP
- Source is often generated & compiled once upon first
request (Tomcat)
- Source is generated & compiled at
deployment
- Source is often generated (Translation) & compiled before
deployment(JSPC)
<html>
<head><title>Hello World</title><head>
<body>
<h1>Hello World!!!</h1>
<br/>
Today is <%=new java.util.Date()%>
</body>
</html>
*** public abstract class HttpJspBase extends
javax.servlet.http.HttpServlet implements
javax.servlet.jsp.HttpJspPage
HelloWorld_jsp.java
|
01 package org.apache.jsp;
02
03 import javax.servlet.*;
04 import javax.servlet.http.*;
05 import javax.servlet.jsp.*;
06
07 public final class HelloWorld_jsp extends org.apache.jasper.runtime.HttpJspBase
08 implements org.apache.jasper.runtime.JspSourceDependent {
09
10 private static java.util.Vector _jspx_dependants;
11
12 public java.util.List getDependants() {
13 return _jspx_dependants;
14 }
15
16 public void _jspService(HttpServletRequest request, HttpServletResponse response)
17 throws java.io.IOException, ServletException {
18
19 JspFactory _jspxFactory = null;
20 PageContext pageContext = null;
21 HttpSession session = null;
22 ServletContext application = null;
23 ServletConfig config = null;
24 JspWriter out = null;
25 Object page = this;
26 JspWriter _jspx_out = null;
27 PageContext _jspx_page_context = null;
28
29
30 try {
31 _jspxFactory = JspFactory.getDefaultFactory();
32 response.setContentType("text/html");
33 pageContext = _jspxFactory.getPageContext(this, request, response,
34 null, true, 8192, true);
35 _jspx_page_context = pageContext;
36 application = pageContext.getServletContext();
37 config = pageContext.getServletConfig();
38 session = pageContext.getSession();
39 out = pageContext.getOut();
40 _jspx_out = out;
41
42 out.write("<html>\r\n");
43 out.write(" <head><title>Hello World</title><head>\r\n");
44 out.write(" <body>\r\n");
45 out.write(" <h1>Hello World!!!</h1>\r\n");
46 out.write(" <br/>\r\n");
47 out.write(" Today is ");
48 out.print(new java.util.Date());
49 out.write("\r\n");
50 out.write(" </body>\r\n");
51 out.write("</html>");
52 } catch (Throwable t) {
53 if (!(t instanceof SkipPageException)){
54 out = _jspx_out;
55 if (out != null && out.getBufferSize() != 0)
56 out.clearBuffer();
57 if (_jspx_page_context != null) _jspx_page_context.handlePageException(t);
58 }
59 } finally {
60 if (_jspxFactory != null) _jspxFactory.releasePageContext(_jspx_page_context);
61 }
62 }
63 }
|
- constructor
- init
- service
- destroy
HelloWorldServlet.java
|
01 import java.io.IOException;
02 import java.io.PrintWriter;
03
04 import javax.servlet.ServletException;
05 import javax.servlet.http.HttpServlet;
06 import javax.servlet.http.HttpServletRequest;
07 import javax.servlet.http.HttpServletResponse;
08
09
10 public class HelloWorldServlet extends HttpServlet {
11 public HelloWorldServlet() {
12 }
13
14 public void init() throws ServletException {
15 }
16
17 public void doGet(HttpServletRequest request, HttpServletResponse response)
18 throws ServletException, IOException {
19 // HttpSession session = request.getSession();
20 response.setContentType("text/html");
21
22 PrintWriter out = response.getWriter();
23
24 out.println("<html><head><title>HelloWorld</title></head>");
25 out.println("<body>HelloWorld!!!</body>");
26
27 out.close();
28 }
29
30 public void doPost(HttpServletRequest request, HttpServletResponse response)
31 throws ServletException, IOException {
32 doGet(request, response);
33 }
34
35 public void destroy() {
36 }
37 }
|
- Typically not used
- Container may call on startup or with first
request
- One Servlet instance or a pool handles all
requests
- Called once when finished using Servlet
- Used to clean up resources
- jspDestroy( ) in JSP
JSP
- On request it will create an instance of
HTTPSession for the user if one does not exist
- If you do not want an instance of HTTPSession to be
created use:
<%@ page session="false" %>
Servlet
- On request it will not create an instance of
HTTPSession
- HTTPSession can be created with
request.getSession( ) or
request.getSession(true)
Essay Questions
- What is the purpose of init and destroy?
- When is a JSP converted to a Servlet?
- When are HTTPSession Instances created?
- When are the steps in the Servlet Life Cycle?
Any Questions?
Feel Free to contact me anytime at
http://www.javaclimber.com
How was this presentation made?
DocBook
According to the official DocBook web site,
| ?? | DocBook is a general purpose XML and SGML document type
particularly well suited to books and papers about computer
hardware and software (though it is by no means limited to these
applications). | ?? |
| ?? | --DocBook.org |