Search This Blog

Thursday, 6 September 2012

Practical-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.


Filename-UserInput.jsp

<html>
<body>
<form action="http://127.0.0.1:8090/wtad/AuthUser">
<h3>Enter uname=istar and psw=istar123</h3>
Enter UserName
<input type="text>" name="nm"/>
<br>
Enter Password
<input type="password" name="psw"/>
<br>
Enter Address
<input type="text" name="add"/>
<br>
<input type="submit" value="submit"/>
</form>
</body>
</html>

Filename-AuthUser.java

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

public class AuthUser extends HttpServlet
{
          public void doGet(HttpServletRequest req,HttpServletResponse res) throws IOException,ServletException
          {

                   String myuser="istar";
                   String mypsw="istar123";
                   res.setContentType("text/html");
                   PrintWriter out=res.getWriter();

                   out.println("AuthUser is call");
                   String unm=req.getParameter("nm");
                   String pw=req.getParameter("psw");
                   String ad=req.getParameter("add");

                   out.println("User name="+unm);
                   out.println("User name="+pw);
                   out.println("User name="+ad);

                   if(myuser.equals(unm) && mypsw.equals(pw))
                   {

                                      HttpSession mysess=req.getSession();
                                      mysess.setAttribute("sname",unm);
                                      mysess.setAttribute("sadd",ad);
                                      res.sendRedirect("http://127.0.0.1:8090/wtad/def18/home.jsp");
                   }
                   else
                   {
                             res.sendRedirect("http://127.0.0.1:8090/wtad/def18/UserInput.jsp");
                   }
          }
}

Filename-home.jsp

<html>
<body>
<h1>
Wel-Come to Home Page
</h1>

Username=<%= session.getAttribute("sname") %>
<br>
Address=<%= session.getAttribute("sadd") %>

<%--
<%
          HttpSession mysess=request.getSession();
          String un=(String)mysess.getAttribute("sname");
          String ad=(String)mysess.getAttribute("sadd");       
%>


<br>
User Name=<%= un %>
<br>
User Add=<%= ad %>
--%>

</body>
</html>

Output

Enter uname=istar and psw=istar123

Enter UserName  
Enter Password 
 
Enter Address  

Wel-Come to Home Page

Username=istar 
Address=Ahmedabad

No comments:

Post a Comment