Professional Documents
Culture Documents
import
import
import
import
import
import
import
import
java.io.IOException;
java.io.PrintWriter;
java.util.Date;
javax.servlet.ServletException;
javax.servlet.http.HttpServlet;
javax.servlet.http.HttpServletRequest;
javax.servlet.http.HttpServletResponse;
javax.servlet.http.HttpSession;
Q5. What are cookies? Write a servlet to demonstrate the use of cookies.
(page 291)
Ans.
When a session is created, the container sends the cookie with
session identifier to the client.
Some other useful information, such as surname and password is
also sent with the cookies, less than 4kb.
The cookie names JSESSIONID, is sent by the container as a
response in the HTTP response header.
Then, whenever any subsequent request is received from the same
web client session (assuming the client supports cookies), the
cookie is sent back by the client to the server as part of the request.
In this case, the cookie value is used by the server to look for the
session state information to be passed to the servlet.
Finally, in the subsequent responses, server sends the updated
cookie back to the client.
As the container handles the process of sending a cookie, the
servlet code is not required while using the cookie. A web browser
automatically handles the process of sending cookies back to the
server unless the client disables the cookies.
Cookie is used by the server to maintain a session.
Example:
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Random;
import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class CookieServlet extends HttpServlet {
protected void processRequest(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
Cookie[] coki = request.getCookies();
Cookie tokencookie = null;
if(coki!=null)
{
for(int i=0;i<coki.length;i++)
{
if(coki[i].getName().equals("token"))
{
tokencookie = coki[i];
break;
}
}
}
response.setContentType("text/html;charset=UTF-8");
try (PrintWriter out = response.getWriter()) {
out.println("<!DOCTYPE html>");
out.println("<html>");
out.println("<head>");
out.println("<title>Extracting the token cookie </title>");
out.println("</head>");
out.println("<body>");
out.println("style=\"font-family:arial;font-size:12pt\">");
String resetParam = request.getParameter("resetParam");
if(tokencookie==null || (resetParam!=null &&
resetParam.equals("yes")) )
{
Random rnd = new Random();
long cookieid = rnd.nextLong();
out.println("<p>Welcome. A new tone "+cookieid+" is now
established</p>");
tokencookie= new Cookie("token",Long.toString(cookieid));
tokencookie.setComment("A cookie named token to identify
user");
tokencookie.setMaxAge(-1);
tokencookie.setPath("/HandleSession/CookieServlet");
response.addCookie(tokencookie);
}
else
{
out.println("Welcome back.... your token is
"+tokencookie.getValue()+"</p> ");
}
String requestURLSame = request.getRequestURL().toString();
String requestURLNew = request.getRequestURL() + "?
resetParam=yes";
out.println("<p>Click <a href="+requestURLSame+"> here
</a> to continue browsing with same identity </p> ");
out.println("<p> Otherwise, click <a
href="+requestURLNew+"> here </a> to browse with new identity
</p>");
out.println("</body>");
out.println("</html>");
}
}
@Override
Description
ServletResponse response)
ServletResponse response)
OR
RequestDispatcher rs = request.getRequestDispatcher("hello.html");
rs.include(request,response);
index.html
<form method="post" action="Validate">
Name:<input type="text" name="user" /><br/>
Password:<input type="password" name="pass" ><br/>
<input type="submit" value="submit">
</form>
Validate.java
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class Validate extends HttpServlet {
protected void doPost(HttpServletRequest request,
HttpServletResponse response)
<servlet-class>Validate</servlet-class>
</servlet>
<servlet>
<servlet-name>Welcome</servlet-name>
<servlet-class>Welcome</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Validate</servlet-name>
<url-pattern>/Validate</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>Welcome</servlet-name>
<url-pattern>/Welcome</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
</welcome-file-list>
</web-app>
This will be the first screen. You can enter your Username and Password
here.
<servlet>
<servlet-name>ServletName</servlet-name>
<servlet-class>com.mkyong.ServletDemo</servlet-class>
<init-param>
<param-name>email</param-name>
<param-value>admin@email.com</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>ServletName</servlet-name>
<url-pattern>/Demo</url-pattern>
</servlet-mapping>
Servlet code
public void doGet(HttpServletRequest request, HttpServletResponse
response)
throws IOException{
PrintWriter pw = response.getWriter();
pw.println(getServletConfig().getInitParameter("email"));