postheadericon 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





index.jsp


<html>
<head>
<title>EX-17</title>
<style>
.abc
{
background:#ababff;
position:absolute;
top:2in;
hieght:200px;
width:300px;
left:5in;
border-style:solid;
}
</style>
</head>

<body>

<div class="abc"  id="f1">
<br>
<form method="POST" action="/Ex17/bipinrupadiya.blogspot.com">
<table align="center">
<tr>
<td>User Name : </td>
<td><input type="text" name="txtUsr"></td>
</tr>
<tr>
<td>Password :</td>
<td><input type="password" name="txtPwd"></td>
</tr>
<tr>
<td colspan=2 align=center>
<input type="checkbox" name="chkRM">
Remember Me
</td>
</tr>

<tr>
<td colspan=2 align=center>
<input type="submit" value="Login">
<input type="reset" value="Reset">
</td>
</tr>
</table>
</form>
</div>
</body>
</html>

home.jsp


<html>
<head>
<title>EX-17</title>
<style>
.abc
{
background:#ababff;
position:absolute;
top:2in;
hieght:200px;
width:300px;
left:5in;
border-style:solid;
}
</style>
</head>
<body>
<div class="abc"  id="f1">
<br>
<center>
<h1>WELCOME
<br>
</div>
</body>
</html>



Ex17.java


import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class Ex17 extends HttpServlet
{
public void doGet(HttpServletRequest request,HttpServletResponse response)throws IOException,ServletException
{
response.setContentType("text/html");
PrintWriter out = response.getWriter();
String usr = request.getParameter("txtUsr");
String pwd = request.getParameter("txtPwd");
String RM  = request.getParameter("chkRM");

if (RM != null)
{
Cookie cookie = new Cookie(usr,pwd);
cookie.setMaxAge(60*60*24*365);// 1 Year
response.addCookie(cookie);
}
Cookie[] cookies = request.getCookies();
if (cookies != null)
{
//Cookie("bipin","rupadiya");
//Cookie(usr,pwd);
String cPwd="x";
for(int i=0; i<cookies.length; i++) //user check
{
Cookie cookie = cookies[i];
if (usr.equals(cookie.getName()))
{
cPwd=cookie.getValue();
break;
}
}
if(cPwd.equals(pwd))//password check
{
response.sendRedirect("home.jsp");
}
else
{
//out.println("<script>alert('User Name or Password is incorrect. Please Try Again');location.href='login.jsp';</script>");
response.sendRedirect("index.jsp");
}
}
}
public void doPost(HttpServletRequest request,HttpServletResponse response)throws IOException,ServletException
{
doGet(request,response);
}
}


5 comments:

velraj said...


I read like this type of article only. You mentioned more important points in this article. Thankyou for interesting article.
core java training in chennai
C Language Training in Chennai
c++ class
c c++ courses in chennai
C Language Training
javascript course in chennai
Appium Training in Chennai
JMeter Training in Chennai

yuvaprithika said...

Nice blog!! I hope you will share more info like this. I will use this for my studies and research.
Angularjs Training in Chennai
Angularjs Course in Chennai
Web Designing Course in Chennai
PHP Training in Chennai
Angularjs Courses in Chennai
Angular Training in Chennai
Best Angularjs Training in Chennai
gst training in chennai
Angularjs Training in Chennai
Angularjs Course in Chennai

kiruthika said...

Very interesting post! Thanks for sharing your experience suggestions.
Aviation Academy in Chennai
Air hostess training in Chennai
Airport management courses in Chennai
Ground staff training in Chennai
Aviation Courses in Chennai
air hostess academy in Chennai
Airport Management Training in Chennai
airport ground staff training in Chennai

Anbarasan14 said...

Fabulous post. Irrespective of the age groups your post is liked by all. Thanks for sharing.

IELTS Coaching in Mulund
IELTS Training in Mulund West
IELTS Courses in Mulund
IELTS Coaching Centres in Mulund
Spoken English Class in Chennai
Spoken English in Chennai
IELTS Coaching in Chennai

Christoper stalin said...

Thanks for sharing this information.
web designing course with placement
php mysql course in chennai
magento 2 course | magento 2 training

Follow by Email

Total Pageviews

© BipinRupadiya.com. Powered by Blogger.