Docsity

Java Server Pages (JSP) and Servlets: Creating Dynamic Web Content with Java, Slides of Computer Science

Partial preview of the text

Download Java Server Pages (JSP) and Servlets: Creating Dynamic Web Content with Java and more Slides Computer Science in PDF only on Docsity!

JSP

Java Server Pages

A “Hello World” servlet

(from the Tomcat installation documentation) public class HelloServlet extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html"); PrintWriter out = response.getWriter(); String docType = "\n"; out.println(docType + "\n" + "Hello\n" + "\n" + "Hello World\n" + ""); } } This is mostly Java with a little HTML mixed inDocsity.com

JSP scripting elements

  • There is more than one type of JSP “tag,” depending on what you want done with the Java
  • <%= expression %>
    • The expression is evaluated and the result is inserted into the HTML page
  • <% code %>
    • The code is inserted into the servlet's service method
    • This construction is called a scriptlet
  • <%! declarations %>
    • The declarations are inserted into the servlet class, not into a method

Example JSP

  • Hello! The time is now <%= new java.util.Date() %>
  • 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 haven’t yet talked about how to do import declarations

Scriptlets

  • Scriptlets are enclosed in <% ... %> tags
    • Scriptlets do not produce a value that is inserted directly into 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); %>
  • Scriptlets are inserted into the servlet exactly as written, and are not compiled until the entire servlet is compiled - Example: <% if (Math.random() < 0.5) { %> Have a nice day! <% } else { %> Have a lousy day! <% } %>

Declarations

  • Use <%! ... %> for declarations to be added to your servlet class, not to any particular method - Caution: Servlets are multithreaded, so nonlocal variables 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:Docsity.com

The include directive

  • The include directive inserts another file into

the file being parsed

  • The included file is treated as just more JSP, hence it can include static HTML, scripting elements, actions, and directives
  • Syntax: <%@ include file=" URL " %>
  • The URL is treated as relative to the JSP page
  • If the URL begins with a slash, it is treated as relative to the home directory of the Web server
  • The include directive is especially useful for

Actions

  • Actions are XML-syntax tags used to control

the servlet engine

    • Inserts the indicated relative URL at execution time (not at compile time, like the include directive does)
    • This is great for rapidly changing data

Docsity.com

The End