Servlet Technology Is Used To Create A Web Application (Resides at Server Side and Generates A Dynamic Web Page)
Servlet Technology Is Used To Create A Web Application (Resides at Server Side and Generates A Dynamic Web Page)
Servlet Technology Is Used To Create A Web Application (Resides at Server Side and Generates A Dynamic Web Page)
• The HTTP information that is sent from the browser to the server in
the form of request headers.
• HTTP request headers are distinct from the form data.
• Form data results directly from user input and is sent as part of the
URL for GET requests and on a separate line for POST requests.
• Request headers, are indirectly set by the browser and are sent
immediately following the initial GET or POST request line.
• Reading Request Headers from Servlets
Header names are not case sensitive
The list of headers that are generally used
Header names Description
getCookies The getCookies method returns the contents of the Cookie
header, parsed and stored in an array of Cookie objects.
getAuthType() and break the Authorization header into its component pieces.
getRemoteUser()
getContentLength() returns int value of the Content-Length header
getContentType() Returns string value of the Content-Type header
getDateHeader() read the specified header and then convert them to Date and int values
getIntHeader()
getHeaderNames() to get an Enumeration of the values of all occurrences of the header names received
on this particular request.
getHeaders() If a header name is repeated in the request,
• version 2.1 servlets cannot access: returns the value of the
first occurrence of the header only
• version 2.2, returns an Enumeration of the values of all
occurrences of the header.
getMethod() returns the main request method(Get, Post . . . . )
getRequestURI() returns the part of the URL that comes after the host and port but before the form
data
getProtocol() returns the third part of the request line, [HTTP/1.0 or HTTP/1.1]
• Example1:
public class mca extends HttpServlet
{
public void doGet(HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException
{
res.setContentType("text/html");
PrintWriter out = response.getWriter();
String title = "Servlet Example: Showing Request Headers";
out.println(ServletUtilities.headWithTitle(title) + "<BODY>\n" +
"<H1>" + title + "</H1>\n" +
"<B>Request Method: </B>"+request.getMethod() +"<BR>\n" +
“<B>Request URI: </B>"+request.getRequestURI()+"<BR>\n" +
“<B>Request Protocol: </B>" +request.getProtocol());
Enumeration <String> headerNames= request.getHeaderNames();
while(headerNames.hasMoreElements())
{
String headerName = headerNames.nextElement();
out.println(“Header Name: <em>”+headerName);
String headerValue = request.getHeader(headerName);
out.print(“</em>, Header Value: <em>”+ headerValue);
out.println(“</em><br/>”);
out.println("</BODY></HTML>");
}
}}
• Example2:
public class rnsit extends HttpServlet
{
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
{
response.setContentType("text/html");
PrintWriter out = response.getWriter();
String title = "HTTP Header Request Example";
String docType =
"<!doctype html public \"-//w3c//dtd html 4.0 " + "transitional//en\">\n";
out.println(docType +
"<html>\n" +
"<head><title>" + title + "</title></head>\n"+
"<body bgcolor = \"#f0f0f0\">\n" +
"<h1 align = \"center\">" + title + "</h1>\n" +
"<table width = \"100%\" border = \"1\" align = \"center\">\n" +
"<tr bgcolor = \"#949494\">\n" +
"<th>Header Name</th><th>Header Value(s)</th>\n"+
"</tr>\n"
);
Enumeration headerNames = request.getHeaderNames();
while(headerNames.hasMoreElements())
{
String paramName = (String)headerNames.nextElement();
out.print("<tr><td>" + paramName + "</td>\n");
String paramValue = request.getHeader(paramName);
out.println("<td> " + paramValue + "</td></tr>\n");
}
out.println("</table>\n</body></html>");
}
}
HTTP 1.1 Request Headers
• HTTP 1.1 also allows you to have persistent(always) connections
which means that you can have more than one request/response on
the same HTTP connection.
• Cookies and session both store information about the user but the difference is
that cookies store information on the client-side (browser) and sessions store
information on the server-side.
• the time between the user logging in and logging out is a session.
Session Tracking
• Session Tracking is a way to maintain state (data) of an user. It is also known as session
management in servlet.
• There are four different techniques used by Servlet application for session
management. They are as follows:
• Cookies
• URL-rewriting
• Hidden form fields
• HttpSession
URL Rewriting
• In URL rewriting, we append a token or identifier to the URL of the
next Servlet or the next resource.
• We can send parameter name/value pairs using the following format:
url?name1=value1&name2=value2&??
• A name and a value is separated using an equal = sign, a parameter
name/value pair is separated from another parameter using the
ampersand(&).
• When the user clicks the hyperlink, the parameter name/value
pairs will be passed to the server.
• From a Servlet, we can use getParameter() method to obtain a
parameter value.
• In this example, we are maintaning the state of the user using link.
• For this purpose, we are appending the name of the user in the query
string and getting the value from the query string in another page.
• Example see below program
index.html
<form action="servlet1">
Name:<input type="text" name="userName"/><br/>
<input type="submit" value="go"/>
</form>
servlet1
public class servlet1 extends HttpServlet
{
public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException
{
response.setContentType("text/html");
PrintWriter out = response.getWriter();
String n=request.getParameter("userName");
out.print("Welcome "+n);
out.print("<a href='servlet2?uname="+n+"'>visit</a>");
}
}
servlet2
public class servlet2 extends HttpServlet
{
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws IOException
{
response.setContentType("text/html");
PrintWriter out = response.getWriter();
String n=request.getParameter("uname");
out.println("Hello "+n);
out.println(“This site is under construction”);
}
}
Hidden Form Field
• in case of Hidden Form Field a hidden (invisible) textfield is used for
maintaining the state of an user.
• In such case, we store the information in the hidden field and get it from
another servlet.
• This approach is better if we have to submit form in all the pages and we
don't want to depend on the browser.
• Let's see the code to store value in hidden field.
• <input type="hidden" name="uname" value=“raghu">
• Here, uname is the hidden field name and raghu is the hidden field
value.
• Example see below program
index.html
<form action="servlet1">
Name:<input type="text" name="userName"/><br/>
<input type="submit" value="go"/>
</form>
servlet1
public class servlet1 extends HttpServlet
{
public void doGet(HttpServletRequest request, HttpServletResponse response) throws
IOException
{
response.setContentType("text/html");
PrintWriter out = response.getWriter();
String n=request.getParameter("userName");
out.print("Welcome "+n);
contd……
out.print("<form action='servlet2'>");
out.print("<input type='hidden' name='uname' value='"+n+"'>");
out.print("<input type='submit' value='go'>");
out.print("</form>");
}}
servlet2
public class servlet2 extends HttpServlet
{
public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException
{
response.setContentType("text/html");
PrintWriter out = response.getWriter();
String n=request.getParameter("uname");
out.print("Hello "+n);
out.println(“This site is under construction”);
}
}
• HttpSession
• Creating a new session
HttpSession session =request.getSession();
• // getsession() method returns a session. If the session already exists, it
returns the existing session else create a new session