Wednesday 3 September 2014

getComment() : get the comment related to this Cookie

public String getComment()
Returns the comment describing the purpose of this cookie, or null if the cookie has no comment. Comments are not supported by Netscape Version 0 cookies.

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
   
     <title>Filter Example</title>
    </head>
    <body>
        <form method="post" action="/servlet/Login">
            Enter First Name<input type="text" name="firstName" /><br />
            Enter Last Name<input type="text" name="lastName" />
            <input type="submit" value ="login">
        </form>
    </body>
</html>

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.Cookie;

@WebServlet(urlPatterns = {"/Login"})
public class Login extends HttpServlet {
    @Override
    public void doPost(HttpServletRequest req, HttpServletResponse res)throws ServletException, IOException{
        String firstName = req.getParameter("firstName");
        String lastName = req.getParameter("lastName");
        
        if(firstName.length()>4 && lastName.length()>4){
            Cookie first = new Cookie("firstName", firstName);
            Cookie last = new Cookie("lastName", lastName);
            
            first.setComment("Cookie to store first name");
            last.setComment("Cookie to store last name");
            
            res.addCookie(first);
            res.addCookie(last);
            
            res.sendRedirect("http://localhost:8080/servlet/Welcome");
        }
        else{
            Cookie name = new Cookie("userName", "username/password is not valid");
            res.addCookie(name);
            res.sendRedirect("http://localhost:8080/servlet/Error");
        }

    }   
}

import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.Cookie;

@WebServlet(urlPatterns = {"/Welcome"})
public class Welcome extends HttpServlet {
  @Override
  public void doPost(HttpServletRequest req, HttpServletResponse res)throws IOException, ServletException{
      try(PrintWriter out = res.getWriter()){
         Cookie[] cookie = req.getCookies();
         
         out.println("Cookie \t Purpose");
        
         for(Cookie c : cookie){
             String name = c.getName();
             if(name.equals("firstName")){
                out.println(name +"\t" + c.getComment() +"\t");
             }
             if(name.equals("lastName")){
                 out.println(name +"\t" + c.getComment());
             }
                 
         }
      }
  }
  
  public void doGet(HttpServletRequest req, HttpServletResponse res)throws IOException, ServletException{
      doPost(req, res);
  }
}

import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@WebServlet(urlPatterns = {"/Error"})
public class Error extends HttpServlet {
  @Override
  public void doPost(HttpServletRequest req, HttpServletResponse res)throws IOException, ServletException{
      try(PrintWriter out = res.getWriter()){
         Cookie[] cookie = req.getCookies();
         
         for(Cookie c : cookie){
             String name = c.getName();
             if(name.equals("userName"))
             out.println(c.getValue());
         }
      }
  }
  
  @Override
  public void doGet(HttpServletRequest req, HttpServletResponse res)throws IOException, ServletException{
      doPost(req, res);
  }
}




Prevoius                                                 Next                                                 Home

No comments:

Post a Comment