Java Servlet Programing Laungage

Java Servlet Projects

Servlet Project 3

Create an application for access servlet cookie
Previous Home Next

In this servlet program, Cookie (a small text file which hold important information about user) can access into servlet and set cookie property.

Web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0" 
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
 http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
 <servlet>
  <servlet-name>CookieServlet</servlet-name>
  <servlet-class>r4r.servlet.CookieServlet</servlet-class>
 </servlet>
 <servlet-mapping>
  <servlet-name>CookieServlet</servlet-name>
  <url-pattern>/CookieServlet</url-pattern>
 </servlet-mapping>
 <session-config>
  <session-timeout>
   30
  </session-timeout>
 </session-config>
</web-app>

 

Index.jsp

<%-- 
 Document   : index.jsp
--%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
 <head>
  <meta http-equiv="Content-Type" 
         content="text/html; charset=UTF-8">
  <title>r4r.co.in-servlet</title>
 </head> 
 <body>
  <form>
  <h1>Access Servlet by servlet's Name!</h1>
  <a href="CookieServlet">CookieServlet</a>
  </form>
</body>
</html>

Servlet Program

/*
 * Save as a CookieServlet.java
 */
package r4r.servlet;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
 *
 * @author R4R
 */
public class CookieServlet extends HttpServlet {
private Cookie cookie;

protected void processRequest(HttpServletRequest request,
	  HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
try {
/* TODO output your page here */
out.println("<html>");
out.println("<head>");
out.println("<title>" + getServletInfo() + "</title>");
out.println("</head>");
out.println("<body>");
out.println("<h1>Servlet CookieServlet at " 
	       + request.getContextPath() + "</h1>");
out.println("<form name=\"form\"action=\
            "CookieServlet\" method=\"POST\">");
out.println("<BR>Username:");
out.println("<input type=\"text\" name=\
                   "user\" value=\"\" size=\"20\" />");
out.println("<BR>Password :");
out.println("<input type=\"password\" name=\
            "pass\" value=\"\" size=\"20\" />");
out.println("<BR> <input type=\"submit\" value=\
            "Submit Form\" name=\"submit\" />");
out.println("<input type=\"reset\" value=\"Reset \" />");
out.println("</form>");
out.println("</body>");
out.println("</html>");
} finally {
out.close();
}
}

@Override
protected void doGet(HttpServletRequest request,
  HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}

@Override
protected void doPost(HttpServletRequest request,
 HttpServletResponse response)
throws ServletException, IOException {
// processRequest(request, response);
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
// get value form TextField
String username = request.getParameter("user");
String password = request.getParameter("pass");
// User don't allow to leave any textfield empty
if ((!username.equals("") && (!password.equals("")))) {
String cookieValue = username + " " + password;
cookie = new Cookie("cookiee", cookieValue); // initialize cookie
cookie.setMaxAge(100);                                             
// set cookie max age
cookie.setVersion(1);                                               
// set cookie version
response.addCookie(cookie);
out.println("<html>");
out.println("<head>");
out.println("<title>" + getServletInfo() + "</title>");
out.println("</head>");
out.println("<body>");
// retrive cookiee value and display cookiee information
out.println("<b>Data successfully add into cookiee</b>");
out.println("<br>Cookiee Name: " + cookie.getName());
out.println("<br>Cookiee Value : " + cookie.getValue());
out.println("<br>Cookiee Version : " + cookie.getVersion());
out.println("</body>");
out.println("</html>");
} else {
out.println("Please don't leave any textfield empty!");
out.println("<BR><a href=\"CookieServlet\">Return to home Page</a>");
}
out.close(); //close text-output stream
}

@Override
public String getServletInfo() {
return "r4r.co.in-CookieServlet";
}
}
Out of Program
Previous Home Next

Tolal:0 Click:

Show All Comments

Did not find what you were looking for leave your name and message. We will revert within 24 hours
Name:
eMail:
Comment / Feedback: