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
|
how to run this program..??
ReplyDeleteRun this program u need the apache tomcat server and so many thing java related.
ReplyDeleteThis comment has been removed by the author.
ReplyDeletewhich file 1st i shoud run...?? give syntax..
ReplyDeleteStep to run this program,
ReplyDelete1) 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.
ohh..so i have tp create web.xml first..am i..??
ReplyDeleteyes follow all thing for running java program.
Delete