Login.html
<html>
<head>
<title></title>
</head>
<body>
<form action="cookiedemo"
method="post">
<h3>Person Info</h3>
<p>Person Id:
<input
type="text" name="pid"> </p>
<p>Person Name :
<input type="text"
name="pname"> </p>
<p> <input type="submit"
name="submit" value="submit"> </p>
</form>
</body>
</html>
Cookiedemo.java
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Enumeration;
import java.util.Random;
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(name="cookiedemo",urlPatterns="/cookiedemo")
public class cookiedemo extends HttpServlet{
public void
service(HttpServletRequest request,HttpServletResponse response) throws
IOException {
response.setContentType("text/html");
PrintWriter
prntwriter=response.getWriter();
prntwriter.println("<html><head><title>Cookie
Example</title></head><body>");
Cookie
prsn_id = new Cookie("person_id",
request.getParameter("pid"));
Cookie
prsn_name = new Cookie("person_name",
request.getParameter("pname"));
prsn_id.setMaxAge(60*60*24);
prsn_name.setMaxAge(60*60*24);
prsn_id.setPath("/democookie/cookiedemo1");
prsn_name.setPath("/democookie/cookiedemo1");
response.addCookie(prsn_id);
response.addCookie(prsn_name);
response.sendRedirect("/democookie/cookiedemo1");
prntwriter.println("</body></html>");
}
}
Cookiedemo1.java:-
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.*;
@WebServlet(name="cookiedemo1",urlPatterns="/cookiedemo1")
public class cookiedemo1 extends HttpServlet{
public
void service(HttpServletRequest request,HttpServletResponse response) throws
IOException,ServletException
{
PrintWriter printwriter=response.getWriter();
Cookie[] coki=request.getCookies();
Cookie
ck=null;
response.setContentType("text/html");
printwriter.println("<html>");
printwriter.println("<head>");
printwriter.println("<title>Login</title>");
printwriter.println("</head>");
printwriter.println("<body>");
if(coki!=null){
for(int i=0;i<coki.length;i++){
ck=coki[i];
printwriter.print(ck.getName() +
" : ");
printwriter.print(ck.getValue());
printwriter.println();
} }
else{
printwriter.println("Error : ");
}
printwriter.println("</body>");
printwriter.println("</html>");
}
0 comments:
Post a Comment