I am writing these quick reference blogs for those who wants to brushup the ideas of each topic. This one will lead you through the basic concepts of JAVA Servlets.
How this quick reference guideline is different from the numerous other docs available ? , please read on to get an idea :
Servlet Lifecycle :
- The container will identify the servlet based on the URL
- Servlet class loading
- Servlet instantaition
- Call init method
- Create a servlet thread for the current request.
- Before calling service(), will create the request and response objects
- Call service method
- Service method will identify whether to call doGet or doPost and call it
- Call destroy method
Different servlet objects :
- A sinlge servlet instance per JVM ( except for SingleTheadModel)
- A sinlge HttpSession per web application ( session activation and passivation)
- A sinlge ServletContext per JVM
- A sinlge ServletConfig per servlet
- A sinlge ServletRequest per servlet request
Important Servlet rq and rs methods :
javax.servlet.Servlet<<Interface>>
<-- javax.servlet.GenericServlet
<-- javax.servlet.HttpServlet
javax.servlet.ServletRequest<<Interface>>
<-- javax.servlet.HttpServletRequest<<Interface>>
getParameter getCookies getRequestDispatcher
getParameterValues getHeader getSession getMethod
getParameterNames getIntHeader getAttribute
getRemotePort --> remote to servlet is client. So this is client port.
getServerPort --> port to which request send ( eg:8080)
getLocalPort --> port for each thread to handle request
javax.servlet.ServletResponse<<Interface>>
<-- javax.servlet.HttpServletResponse<<Interface>
setContentType sendRedirect setHeader
addCookie encodeURL addHeader
setIntHeader
getWriter --> PrintWriter --> .print method
getOutputStream --> ServletOutputStream --> .write method
Simple Servlet class to get one parameter and a colection of parameters :
public class ServletOne extends HttpServlet{
public void doGet(HttpServletRequest request,HttpServletResponse response)
throws IOException, ServletException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
String userName = request.getParameter("userName");
String[] paramValues = request.getParameterValues("userNames");
Enumeration parameterNames = request.getParameterNames();
while(parameterNames.hasMoreElements()){
out.println("<br>request.parameterNames="+parameterNames.nextElement());
}
out.print("<br>request.getRemotePort()="+request.getRemotePort());
}
}
General purpose of Servlet methods and objects :
public void init() --> usually to handle the ServletConfig.
public void destroy () --> commonly used for activities like close DB connections opened by the servlet.
ServletContext.InitParameter --> like database lookup name, EJB bean JNDI names
ServletContext.attribute --> like databaseConnection, concurrentUsers
HttpSession.attribute --> like shopping cart
Sample web.xml :
<web-app>
<context-param>
<param-name>bankURL</param-name>
<param-value>www.myBank.com</param-value>
</context-param>
<servlet>
<servlet-name>SampleServlet</servlet-name>
<servlet-class>jspAndServlet.SampleServlet</servlet-class>
<init-param>
<param-name>bankName</param-name>
<param-value>MyBank</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>SampleServlet</servlet-name>
<url-pattern>/Serv1</url-pattern> --> possible to use wildcards
</servlet-mapping>
<session-config>
<session-timeout>5</session-timeout>
</session-config>
<listener>
<listener-class>listeners.ContextListener</listener-class>
</listener>
<welcome-file-list>
<welcome-file>TestLogIn.html</welcome-file>
<welcome-file>index.html</welcome-file>
</welcome-file-list>
</web-app>
RequestDispatcher rd = request.getRequestDispatcher("FirstJSP.jsp"); --> Relative or absolute path
rd.forward(request,response);
or
RequestDispatcher rd = getServletContext().getRequestDispatcher("FirstJSP.jsp"); --> Absolute path
rd.forward(request,response);
- When we use "rd.include", control is retained by the calling resource.
- When we use "rd.include", not full HttpServletResponse functionality is passed to the called resource.
- Remember that you cannot forward the request if you have already committed a reponse like "out.print" or "os.flush"
- To pass some data to JSP you can use any of HttpServletRequest or HttpSession or ServletContext objects
Servlet and HttpSession
Http is a stateless protocol. To get session we can use javax.servlet.http.HttpSession
The HttpSession interface is used with cookie and URL rewriting technology.
header send in response --> Set-Cookie:JSESSIONID=0AABB2345ED2
header send in request --> Cookie:JSESSIONID=0AABB2345ED2
- HttpSession session = request.getSesion();
- session.isNew()
- request.getSession(false) --> only already created session
- session.getId();
- session.getCreationTime();
- session.getLastAccessedTime();
- String userName = (String)session.getAttribute("userName");
- session.setAttribute("userName",request.getParameter("userName")); --> Cannot store primitive data types
- session.setMaxInactiveInterval(int interval);
- int interval = session.getMaxInactiveInterval();
- session.invalidate();
<session-timeout>-1</session-timeout> --> will never expire
</session-config>
response.encodeURL("/BeerTest.do"); --> URL rewriting -->possible only with dynamically generated urls
response.encodeRedirectURL("/BeerTest.do");
Cookie cookie = new Cookie(userName,password);
cookie.setMaxAge(10*60);
response.addCookie(cookie);
Cookie[] cookies = request.getCookies();
for(Cookie cookie:cookies){
String cookieName = cookie.getName();
String cookieValue = cookie.getValue();
}
Servlet and Thread safety
Web components share information with other web components using objects that are maintained as attributes of four types of scope object
- web context --> ServletContext
- session --> HttpSession
- request --> ServletRequest
- page --> PageContext
1. You can ensure that each request works independently and doesn't access the servlet instance concurrently by implementing SingleThreadModel
public class myServlet extends HttpServlet implements SingleThreadModel
<%@ page isThreadSafe="true" %> --> The default value of the isThreadSafe attribute is "true".
--> Will not implement SingleThreadModel.
2. You can allow concurrent access while protecting the shared data in the same way as you do in other multithreaded Java applications. For high-traffic servlets, you should use explicit synchronized blocks rather than implementing the SingleThreadModel interface like
synchronized(getServletContext()){ ... } or
synchronized(session){ ... }
Only local variables and request parameters are thread safe.
Servlet and Filter
Filters are commonly used for :
- authentication
- localization
- encryption
- data compression
- caching
- logging --> given below
javax.servlet.FilterChain
public void init(FilterConfig config)
public void doFilter(ServletRequest request, --> Please note that this is not HttpServletRequest
ServletResponse response,
FilterChain chain) throws IOException,ServletException
<filter>
<filter-name>myFilter</filter-name>
<filter-class>jspAndServlet.filter.CompressionFilter</filter-class>
<init-param>
<param-name>enableCompression</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>myFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
or
<filter-mapping>
<filter-name>myFilter</filter-name>
<servlet-name>SampleServlet</servlet-name>
</filter-mapping>
or
<filter-mapping>
<filter-name>myFilter</filter-name>
<url-pattern>/*</url-pattern>
<dispatcher>REQUEST</dispatcher> or INCLUDE or FORWARD or ERROR
</filter-mapping>
--> from 2.4 version onwards --> REQUEST is the default value --> All/any of thesre can be included
Container's rule for ordering the filters:
1. All filters with "<url-pattern>" is taken in the order in which they are declared in DD.
2. All filters with "<servlet-name>" is taken in the order in which they are declared in DD.
FilterChain.doFilter() --> Will invoke the next filter.
String filterName = filterConfig.getFilterName();
Logging using the filter:
ServletContext context = filterConfig.getServletContext();
context.log(new Date() + ": " + "Access to: " + req.getRequestURL()
+ " requested by: " + req.getRemoteHost()
+ ". (Reported by " + filterConfig.getFilterName() + ".)");
Compression using the filter:
It can be used after the FilterChain.doFilter() call
Eg:
public void doFilter(request,response,chain) {
//request handling
chain.doFilter()
//response handling --> compression.
}
Issue --> After the call "chain.doFilter()", response will already be flushed to client from servlet.
Solution --> Create a new instance of "response" and copy all values to it and pass it.
Issue --> The "response" has n values to copy --> need to code all ?
Solution --> use Wrappers
ServletRequestWrapper ServletResponseWrapper
HttpServletRequestWrapper HttpServletResponseWrappers
Servlet and Listener
Use of Listener: If you wanted to keep a count of the total number of users on a system, you could update a global userNumber variable upon receiving each session creation event(sessionCreated). Similarly contextInitialized can be used to create a connection common to all the servlets.
<listener>
<listener-class>listeners.ContextListener</listener-class>
</listener>
Changes that trigger events -->
1. Initialization and destruction of servletContext.
2. Initialization and destruction of ServletRequest.
3. Initialization and destruction of session objects.
4. Addition/Alteration/Removal of attributes associated with servletContext, ServletRequest or session objects.
java.util.EventListener ---> java.util.EventObject
javax.servlet.ServletContextListener ---> javax.servlet.ServletContextEvent
contextInitialized getServletContext
contextDestroyed
javax.servlet.ServletContextAttributeListener ---> javax.servlet.ServletContextAttributeEvent
attributeAdded getName
attributeRemoved getValue --> OLD value
attributeReplaced getServletContext
javax.servlet.ServletRequestListener ---> javax.servlet.ServletRequestEvent
requestInitialized getServletContext
requestDestroyed
javax.servlet.ServletRequestAttributeListener ---> javax.servlet.ServletRequestAttributeEvent
attributeAdded getName
attributeRemoved getValue
attributeReplaced getServletContext
javax.servlet.HttpSessionListener ---> javax.servlet.HttpSessionEvent
sessionCreated getSession
sessionDestroyed
javax.servlet.HttpSessionAttributeListener ---> javax.servlet.HttpSessionBindingEvent
attributeAdded getName
attributeRemoved getValue --> OLD value
attributeReplaced getSession
javax.servlet.HttpSessionBindingListener ---> javax.servlet.HttpSessionBindingEvent
valueBound getName
valueUnbound getValue
(no need to configure this in DD) getSession
javax.servlet.HttpSessionActivationListener ---> javax.servlet.HttpSessionEvent
sessionDidActivate getSession
sessionWillPassivate
HttpSessionBindingEvent extends HttpSessionEvent
Simpe Tips in servlet :
- WEB-INF --> The folder name should be in capital letter to work with Tomcat5.
- <form action="/urlOne" > --> If you have a folder like "Tomcat 5.5\webapps\test\urlOne", this will not work as this is an absolute path, this will search for "webapps/urlOne" and you have "webapps/test/urlOne". So need to use <form action="urlOne"> to get "webapps/test/urlOne".
- You can overrride the init() method and do the one time operations like create a DB connection.
- War files make it easy to transfer the web applications from one server to another.
Comments
Post a Comment