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

Sunday 1 April 2012

Develop a Servlet which looks for cookies for username and password, and forwards to a home.jsp in case the cookies are valid and forwards to login.jsp, in case the cookies are not found or the cookies are not valid.

[login.java]


import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class login extends HttpServlet
{
public void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException,IOException
{
res.setContentType("text/html");
PrintWriter out=res.getWriter();
String name=req.getParameter("username");
String pass=req.getParameter("password");
String cpass=getValue(req,name);
if(pass.equals(cpass))
{
// RequestDispatcher dispatcher=req.getRequestDispatcher("\home.jsp");
// dispatcher.forword();
res.sendRedirect("jsp_pages/home.jsp");
}
else
{
res.sendRedirect("jsp_pages/login.jsp");
}
}
String getValue(HttpServletRequest req,String name)
{
String value=null;
Cookie ck[]=req.getCookies();
if(ck!=null)
{
for(int i=0;i<ck.length;i++)
{
if(ck[i].getName().equals(name))
{
value=ck[i].getValue();
break;
}
}
}
return value;
}
}

[addcookie.java]



import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class addcookie extends HttpServlet
{
public void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException,IOException
{
res.setContentType("text/html");
Cookie ck;
ck=new Cookie("pankaj","master");
res.addCookie(ck);
ck=new Cookie("Vijay","chavda");
res.addCookie(ck);
}
}

[login.jsp]



<html>
<body>
HIIIIIII THIS IS LOGIN.JSP PAGE
<form action="login" method="GET">
<table>
<tr>
<th>USERNAME:
<td><input type="text" name="username">
<tr>
<th>PASSWORD:
<td><input type="password" name="password">
<tr>
<td><input type="submit" value="LOGIN">
<td><input type="reset" value="CLEAR">
</table>
</form>
</body>
</html>

[home.jsp]

<html>
<body>

HIIIIIII THIS IS HOME.JSP PAGE
</body>
</html>









No comments:

Post a Comment