Java Servlets
Java Servlets
Topics
• Introduction
• Overview Servlet Life Cycle
• A Simple Servlet
• Reading data from client
• Reading HTTP request headers
• Sending data to client
• Writing HTTP response headers
• Session
• cookies
Introduction
GenericServlet
Filter
ServletContextAttributeEvent
FilterChain
ServletContextEvent
FilterConfig
ServletInputStream
RequestDispatcher
ServletOutputStream
Servlet
ServletRequestAttributeEvent
ServletConfig
ServletRequestEvent
ServletContext
ServletRequestWrapper
ServletContextAttributeListener
ServletResponseWrapper
ServletContextListener
ServletRequest
ServletRequestAttributeListener
ServletRequestListener
ServletResponse
SingleThreadModel
Classes and interfaces in
javax.servlet.http
Class Interfaces
Cookie HttpServletRequest
HttpServlet HttpServletResponse
HttpServletRequestWrapper HttpSession
HttpServletResponseWrapper HttpSessionActivationListener
HttpSessionBindingEvent HttpSessionAttributeListener
HttpSessionEvent HttpSessionBindingListener
HttpUtils HttpSessionContext
HttpSessionListener
The init() method :
• 3 ways
<!DOCTYPE web-app
PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd">
<web-app>
<servlet>
<servlet-name>FirstServlet</servlet-name>
<servlet-class>FirstServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>FisrtServlet</servlet-name>
<url-pattern>logon.html</url-pattern>
</servlet-mapping>
</web-app>
Reading data from client
• ServletRequest Interface
– An object of ServletRequest is used to provide the
client request information to a servlet such as
• Parameter names & values
• Content type &Content length
• Header information
• Attributes etc
• Methods of ServletRequest interface
• Methods of ServletRequest interface
– String getParameter( )
Returns the value of a request parameter as
a String, or null if the parameter does not exist.
– String[] getParameterValues( )
Returns an array of String objects containing
all of the values the given request parameter . It is
mainly used to obtain values of Multi select list
box.
– String getContentType()
Returns the MIME type of the body of the
request, or null if the type is not known.
Example to display name of user
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
406 The server can only generate a response that is not accepted by the
Not Acceptable
client.
407 Proxy Authentication You must authenticate with a proxy server before this request can be
Required served.
408 Request Timeout The request took longer than the server was prepared to wait.
500 The request was not completed. The server met an unexpected
Internal Server Error condition
501 The request was not completed. The server did not support the
Not Implemented functionality required.
502 The request was not completed. The server received an invalid
Bad Gateway response from the upstream server
Session tracking
• Session means particular
interval of time.
• Session tracking is a way to
maintain state of a user.
• Http is a stateless protocol,
each request to server, server
treats the request as a new
request.
• So we need to maintain the
state of the user to recognize
a particular user
• Techniques
– Cookies
– Http Sessions
1. Cookies
• A cookie is a small piece of information
that are sent in response from web
server to client
• Cookie are stored on client computer.
• They have lifespan and are destroyed by
the client browser at end of lifespan
• A cookie has name, value, attributes like
comment, path max age, version
number
• Advantage
– Simplest technique for maintain state
– Cookies are maintained at client side
• Disadvantage
– It will not work if cookie is disabled from
browser
– Only textual info can be set in a cookie
object
Cookies
First.html
• <form action="servlet1" method="post">
• Name:<input type="text" name="userName"/><br/>
• <input type="submit" value="go"/>
• </form>
FirstServelt.java
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
<servlet>
<servlet-name>s2</servlet-name>
<servlet-class>SecondServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>s2</servlet-name>
<url-pattern>/servlet2</url-pattern>
</servlet-mapping>
</web-app>
Setting cookies with servlet involves :
</form>
</body>
</html>
import java.io.*;
import java.sql.*;
import javax.servlet.ServletException;
import javax.servlet.http.*;
out.close();
}
• <web-app>
•
• <servlet>
• <servlet-name>Register</servlet-name>
• <servlet-class>Register</servlet-class>
• </servlet>
•
• <servlet-mapping>
• <servlet-name>Register</servlet-name>
• <url-pattern>/servlet/Register</url-pattern>
• </servlet-mapping>
•
• <welcome-file-list>
• <welcome-file>register.html</welcome-file>
• </welcome-file-list>
•
• </web-app>