Java Server Page (JSP) Architecture

JSP Architecture

JSP Architecture

Java Server Page (JSP) Model-1 Architecture

jsp model one architecture

jsp model one architecture

Java Server Page (JSP) Model-2 Architecture

Java Server Page (JSP) Model-2 Architecture

Java Server Page (JSP) Model-2 Architecture

useBean

package com.ourownjava.jsp.bean;
/**
* @author ourownjava.com
*/
public class User {

private String firstName;
private String lastName;

public String getFirstName() {
return firstName;
}
public void setFirstName(final String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(final String lastName) {
this.lastName = lastName;
}
}
 
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>jsp.ourownjava.com</title>
</head>
<body>
    <jsp:useBean id="userBean" scope="request">
        <jsp:setProperty name="userBean" property="firstName" value="jsp"/>
        <jsp:setProperty name="userBean" property="lastName" value="ourownjava.com"/>
    </jsp:useBean>         
    First Name: <jsp:getProperty name="userBean" property="firstName" /> <br/>
    Last Name: <jsp:getProperty name="userBean" property="lastName" />
</body>
</html>
 

Actions

Standard action tags are better alternative for the scrptlet.

Name Syntax Description
attribute jsp:attribute Dynamically defined XML element's attribute
body jsp:body Dynamically defined XML element's body
element jsp:element XML elements dynamically
forward jsp:forward Forwards the request to a different page
getProperty jsp:getProperty Insert the value of a property into output stream
include jsp:include On demand file include. (upon page request)
plugin jsp:plugin Browser specific construct
setProperty jsp:setProperty Set value to a property of a Bean
text jsp:text To write template text in JSP pages and documents
useBean jsp:useBean Construct a a JavaBean object
 

Life Cycle

The JSP/Servlet container is responsible for managing the life cycle of a JSP file. In general there are three important stages in JSP life cycle.

  1. Instantiation
  2. Request Processing
  3. Destruction

JSP Life Cycle – Initialization Stage

When a JSP/Servlet container receives request for a JSP it checks if the JSP is initialized. If the JSP is not initialized already then the container would take the JSP file through the following stages.

# Stage Description
1 Translation Container translate the JSP file into a Servlet file
2 Compilation The generated java Servlet file is compiled into a java Servlet class
3 Class Loading The java Servlet class is loaded into JVM
4 Servlet Instantiation An instance of the Servlet class is created
5 Initialization jspInit() method is called right after instance was created.

jspInit() method is called only once during JSP life cycle. Initialization will make ServletContext and ServletConfig objects available for the Servlet.

JSP Life Cycle – Request Processing Stage

Every request from client is served by _jspService() method. The whole initialization stage is done to make the JSP ready to serve the client request.

JSP Life Cycle – Destruction Stage

A Servlet/JSP container may call the _jspDestroy() method any time after the initialization. Once the _jspDestroy() method is called JSP file has go through to initialization process to be consumed by the client. Container may call the _jspDestroy() method as and when the container is gracefully shutdown or the container need to free up some memory to load other resources. Once the _jspDestroy() method is called the Servlet is no longer referred by a reference and the garbage collector can pick it up.

JSP Life Cycle Methods

  1. _jspInit()
  2. _jspService()
  3. _jspDestroy()

JSP Life Cycle

JSP Life Cycle

 

Post Request

HTML page to make a get request (github)

<html>
<body>
<form action="jsp-get-method-query-string.jsp" method="POST">
Who: <input type="text" name="who" />
<input type="submit" value="Submit" />
</form>
</body>
</html>

JSP page to extract get request parameter (github)

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>jsp.ourownjava.com</title>
</head>
<body>
Who : <%=request.getParameter("who")%>
</body>
</html>
 

Get Request – HTML Form

HTML page to make a get request (github)

<html>
<body>
<form action="jsp-get-method-query-string.jsp" method="GET">
Who: <input type="text" name="who" />
<input type="submit" value="Submit" />
</form>
</body>
</html>

JSP page to extract get request parameter (github)

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>jsp.ourownjava.com</title>
</head>
<body>
Who : <%=request.getParameter("who")%>
</body>
</html>

How to run the sample program?

 

Get Request – URL Query String

How to access get request parameter in jsp? (github)

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>jsp.ourownjava.com</title>
</head>
<body>
Who : <%=request.getParameter("who")%>
</body>
</html>

How to run the sample program?

http://localhost:8080/jsp-play-ground/jsp-get-method-query-string.jsp?who=ourownjava.com
 

Form Processing

When you need to pass some data from browser to the web server you may choose to use one of the HTTP request methods.

  1. Get
  2. Post

Get VS Post

Get Post
Only ASCII allowed ASCII and Binary
Data is visible in the URL Data is NOT visible in URL
Maximum URL length is 2048 characters Practically no limit
Idempotent Repeated request would change the state
Browser can cache a get request Post request data is NOT cached in browser
 

session

The JSP implicit object ‘session’ is used to track user session between user requests.

Name Interface Tomcat Implementation
session javax.servlet.http.HttpSession org.apache.catalina.session.StandardSessionFacade

JSP implicit object ‘session’ example program (github)

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>jsp.ourownjava.com</title>
</head>
<body>

<!-- jsp implicit object request example -->
Tomcat implementation of jsp implicit object s :  <% out.println(session.getClass().getCanonicalName()); %> <br/>

<!-- how to add an attribute to session -->
<%session.setAttribute("who", "ourownjava.com"); %>

<!-- how to get an attribute from session -->
<%out.println(session.getAttribute("who"));%>  <br/>

<!-- how to access servlet context using session object -->
<%session.getServletContext().setAttribute("attribute1", "attribute1Value"); %>

<%out.println(session.getServletContext().getAttribute("attribute1")); %>

</body>
</html>
 

response

The JSP implicit response object contains the output to the client. The JSP implicit response object can be used for following.

  1. To add HTTP headers
  2. To create cookies
  3. To setting content type
  4. To redirect
Name Interface Tomcat Implementation
response javax.servlet.http.HttpServletResponse org.apache.catalina.connector.ResponseFacade

JSP implicit object ‘response’ example program (github)

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>jsp.ourownjava.com</title>
</head>
<body>

<!-- jsp implicit object request example -->
Tomcat implementation of jsp implicit object request :  <% out.println(response.getClass().getCanonicalName()); %> <br/>

<!-- how to add header entries into response -->
<%response.addHeader("argument1", "argument1Value");%>

<!-- how to add cookie into response -->
<%final Cookie cookie = new Cookie("token", "tokenValue");
response.addCookie(cookie); %>

<!-- how to add an attribute to session -->
<%session.setAttribute("who", "ourownjava.com"); %>

<!-- how to instruct the browser to redirect -->
<% response.sendRedirect("redirected.jsp");%>

</body>
</html>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>jsp.ourownjava.com</title>
</head>
<body>

<% out.println(session.getAttribute("who")); %>

</body>
</html>

How to run the sample program?

 

request

Name Interface Tomcat Implementation
request javax.servlet.http.HttpServletRequest org.apache.catalina.connector.RequestFacade

how to access jsp implicit object request in jsp page?

<% out.println(request.getParameter("who")); %>

JSP implicit object pageContext example program (github)

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>jsp.ourownjava.com</title>
</head>
<body>
    <!-- jsp implicit object request example -->
    Tomcat implementation of jsp implicit object request :  <% out.println(request.getClass().getCanonicalName()); %> <br/>
    <!-- how to access jsp implicit object request in jsp page -->
    Request parameter who : <% out.println(request.getParameter("who")); %> <br/>
    <!-- how to access session object request in jsp page -->
    Access Session object from request : <%out.println(request.getSession().getId()); %> <br/>
    <!-- how to set attribute in request object -->
    <%request.setAttribute("attribute1", "attribute1Value"); %>
    Access request attribute : <%out.println(request.getAttribute("attribute1")); %>
</body>
</html>

How to run the JSP request context sample program

http://localhost:8080/jsp-play-ground/jsp-implicit-object-request-example.jsp?who=ourownjava.com