Search This Blog

Thursday, 6 September 2012

Practial-17) 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.


Filename-login.jsp

<html>
<body>
<form action="http://localhost:8090/wtad/def17/home.jsp">
UserName<input type="text"/><br>
Password<input type="text"/>
<br>
<input type="submit" name="sub" value="Login"/>

</form>
</body>
</html>



Filename-Home.jsp

<html>
<body>
Welcome to Home
</body>
</html>

Filename-SettingCookie.java

package def17;
import java.io.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;

public class SettingCookie extends HttpServlet
{
          public void doGet(HttpServletRequest req,HttpServletResponse res) throws IOException,ServletException
          {
                   res.setContentType("text/html");
                   PrintWriter out=res.getWriter();

                   out.println("SettingCookie");

                   Cookie unamec=new Cookie("istar","sai1234");
                   res.addCookie(unamec);
                   out.println("Cookie have been set");

                   RequestDispatcher dis=req.getRequestDispatcher("/CheckingCookie");
                   dis.forward(req,res);
          }
          public void doPost(HttpServletRequest req,HttpServletResponse res) throws IOException,ServletException
          {
                   doGet(req,res);
          }
}



Fileaneme-ChekingCookie.java

package def17;
import java.io.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;

public class CheckingCookie extends HttpServlet
{
          public void doGet(HttpServletRequest req,HttpServletResponse res) throws IOException,ServletException
          {
                   res.setContentType("text/html");
                   PrintWriter out=res.getWriter();
                   boolean flag=false;

                   out.println("CheckingCookie");

                   Cookie myc[]=req.getCookies();

                   if(myc!=null)
                   {
                             for(int i=0;i<myc.length;i++)
                             {
                                      String cnm=myc[i].getName();
                                                if(cnm.equals("istar"))
                                                {
                                                          String cvl=myc[i].getValue();
                                                          if(cvl.equals("sai1234"))
                                                          {
                                                                   flag=true;
                                                          }
                                                }
                             }
                   }
                   else
                   {
                             flag=false;
                   }

                   if(flag=true)
                   {
                             res.sendRedirect("http://localhost:8090/wtad/def17/home.jsp");
                   }
                   else
                   {
                             res.sendRedirect("http://localhost:8090/wtad/def17/login.jsp");
                   }

          }
          public void doPost(HttpServletRequest req,HttpServletResponse res) throws IOException,ServletException
          {
                   doGet(req,res);
          }
}

Output

Welcome to Home

Cookie
Name:
Istar
Content:
sai1234
Domain:
Localhost
Path:
/wtad
Send For:
Any kind of connection
Accessible to Script:
Yes
Created:
Tuesday, April 10, 2012 9:52:23 PM
Expires:
When I close my browser

7 comments:

  1. how to run this program..??

    ReplyDelete
  2. Run this program u need the apache tomcat server and so many thing java related.

    ReplyDelete
  3. This comment has been removed by the author.

    ReplyDelete
  4. which file 1st i shoud run...?? give syntax..

    ReplyDelete
  5. Step to run this program,
    1) SettingCookie.java, this program set the cookie,
    2) ChekingCookie.java, this file check the cookie,

    it the cookie are found and correct then forward to home.jsp else forward to login.jsp.

    ReplyDelete
  6. ohh..so i have tp create web.xml first..am i..??

    ReplyDelete
    Replies
    1. yes follow all thing for running java program.

      Delete