postheadericon 18. Develop a Servlet to authenticate a user, where the loginid and password are available as request parameters. In case the authentication is successful, it should setup a new session and store the user's information in the session before forwarding to home.jsp, which displays the user's information like full name, address, etc.


index.jsp


<html>
<head>
<title>EX-18</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="/Ex18/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="submit" value="Login">
<input type="reset" value="Reset">
</td>
</tr>
</table>
</form>
</div>
</body>
</html>

home.jsp


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

<body>
<%
        HttpSession sessi=request.getSession();
%>
<div class="abc"  id="f1">

<center>
<h1><font color="#FFFFFF">Welcome <%= session.getAttribute("sFName") %> ...!</font></h1>
<table border="1" cellPadding="5px">
<tr>
<td>Name :</td>
<td>
<%= session.getAttribute("sFName") %>&nbsp;
<%= session.getAttribute("sMName") %>&nbsp;
<%= session.getAttribute("sLName") %>
</td>
</tr>
<tr>
<td>Date of Birth :</td>
<td><%= session.getAttribute("sDOB") %></td>
</tr>
<tr>
<td>Adsress :</td>
<td><%= session.getAttribute("sAddress") %></td>
</tr>

</table>
<%session.invalidate();%>
<br>
<a href="index.jsp"> Logout </a>

</div>
</body>
</html>

Ex18.java



import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;

public class Ex18 extends HttpServlet
{
    public void doPost(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");
       
        if( usr.equals("bipin") && pwd.equals("rupadiya") )
        {
   String myDiv;
out.println("<html><body><form>");
myDiv="<div style='background:#ababff; position:absolute; ";
myDiv=myDiv+"top:2in; hieght:200px; width:300px;left:5in;border-style:solid'>";
out.println(myDiv);
out.println("<form><table cellpadding'2px'>");
            out.println("<tr><td>First Name :</td><td><input type='text' name='txtFName' ></td></tr>");
out.println("<tr><td>MiddleName : </td><td><input type='text' name='txtMName' ></td></tr>");
out.println("<tr><td>Last Name : </td><td><input type='text' name='txtLName' ></td></tr>");
            out.println("<tr><td>Date of Birth : </td><td><input type='text' name='txtDOB' ></td></tr>");
out.println("<tr><td>Address : </td><td><input type='text' name='txtAddress'></td></tr>");
            out.println("<tr><td colspan='2'><input type='submit' value='OK'>");
out.println("<input type='reset' value='Clear'></td></tr>");
out.println("</td></form></div></body></html>");
}
        else
        {
            response.sendRedirect("index.jsp");
        }
    }
public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException,ServletException
    {
HttpSession session=request.getSession();
session.setAttribute("sFName",request.getParameter("txtFName"));
session.setAttribute("sMName",request.getParameter("txtMName"));
session.setAttribute("sLName",request.getParameter("txtLName"));
session.setAttribute("sDOB",request.getParameter("txtDOB"));
session.setAttribute("sAddress",request.getParameter("txtAddress"));
response.sendRedirect("home.jsp");
    }  
}


0 comments:

Follow by Email

Total Pageviews

© BipinRupadiya.com. Powered by Blogger.