Structure Query Language, C programming, Java, Servlet, Jsp, Unix

Tuesday, 14 February 2012

Write a Servlet which displays a message and also displays how many times the message has been displayed (how many times the page has been visited).


import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class logincheck extends HttpServlet
{
public void doGet(HttpServletRequest req,HttpServletResponse res)throws ServletException,IOException
{
res.setContentType("text/html");
PrintWriter out=res.getWriter();
String uname=req.getParameter("uname");
String pass=req.getParameter("pass");
String color=req.getParameter("color");
String value=getCookieValue(uname,req);
if(value!=null)
{
out.println("<body bgcolor="+value+">");
out.println("WELCOME AGAIN");
}
else
{
out.println("<body bgcolor="+color+">");
out.println("WELCOME TO OUR SITE FIRST TIME");
Cookie cwrite=new Cookie(uname,color);
res.addCookie(cwrite);
}
out.println("</body></html>");
}
public String getCookieValue(String name,HttpServletRequest req)
{
String value=null;
Cookie carr[]=req.getCookies(); // IT WILL RETURN THE ARRAY OF COOKIE CLASS
if(carr!=null)
{
for(int i=0;i<carr.length;i++)
{
if(carr[i].getName().equals(name))
{
value=carr[i].getValue(); 
 break;
}
}
}
return value;
}
}

No comments:

Post a Comment