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>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>
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 |
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.
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.
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.
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.
<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>
<%@ 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>ourownjava.com</title> </head> <body> Who : <%=request.getParameter("who")%> </body> </html>
<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>
<%@ 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>ourownjava.com</title> </head> <body> Who : <%=request.getParameter("who")%> </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>ourownjava.com</title> </head> <body> Who : <%=request.getParameter("who")%> </body> </html>
http://localhost:8080/jsp-play-ground/jsp-get-method-query-string.jsp?who=ourownjava.com
When you need to pass some data from browser to the web server you may choose to use one of the HTTP request methods.
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 |
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 |
<%@ 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>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>
The JSP implicit response object contains the output to the client. The JSP implicit response object can be used for following.
Name | Interface | Tomcat Implementation |
---|---|---|
response | javax.servlet.http.HttpServletResponse | org.apache.catalina.connector.ResponseFacade |
<%@ 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>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>ourownjava.com</title> </head> <body> <% out.println(session.getAttribute("who")); %> </body> </html>
Name | Interface | Tomcat Implementation |
---|---|---|
request | javax.servlet.http.HttpServletRequest | org.apache.catalina.connector.RequestFacade |
<% out.println(request.getParameter("who")); %>
<%@ 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>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>
http://localhost:8080/jsp-play-ground/jsp-implicit-object-request-example.jsp?who=ourownjava.com