Structure Query Language, C programming, Java, Servlet, Jsp, Unix

Showing posts with label how to Redirecting to jsp page. Show all posts
Showing posts with label how to Redirecting to jsp page. Show all posts

Sunday, 1 April 2012

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.

Download Servlet Example to authenticate from here

[index.html]



<html>
<body>
<center><h1>www.master-gtu.blogspot.com</h1></center>
<form action="logincheck" method="GET">
<table>
<tr>
<th>USERNAME:
<td><input type="text" name="username">
<tr>
<th>PASSWORD:
<td><input type="password" name="password">
<tr>
<td><input type="submit" value="LOGIN">
<td><input type="reset" value="CLEAR">
</table>
</form>
</body>
</html>



[login.java]

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class login extends HttpServlet
{
public void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException,IOException
{
/* www.master-gtu.blogspot.com
* pankaj sharma(8460479175)
* vijay chavda(8460420769)
*/
res.setContentType("text/html");
PrintWriter out=res.getWriter();
String name=req.getParameter("username");
String pass=req.getParameter("password");

if(name.equals("pankaj") && pass.equals("panku"))
{
HttpSession session=req.getSession(true);
session.setAttribute("name","pankaj");
session.setAttribute("contact","9016115831");
session.setAttribute("address","c/o mahendra bhai plot 25, jamnagar");

RequestDispatcher dispatcher=req.getRequestDispatcher("home.jsp");
dispatcher.forward(req,res);
}
else
{
RequestDispatcher dispatcher=req.getRequestDispatcher("/index.html");
dispatcher.forward(req,res);
}
}
}


[home.jsp]



<html>
<body>

<table align=center border=1>
<tr>
<th colspan=2 bgcolor="orange">
<center><h3>
Welcome to Welcome Page
</h3></center>
<tr>
<th>Name
<td><%=session.getAttribute("name")%>
<tr>
<th>Contact
<td><%=session.getAttribute("contact")%>
<tr>
<th>Address
<td><%=session.getAttribute("address")%>
</table>
</body>
</html>



[web.xml]



<web-app>
<servlet>
<servlet-name>HELLOWORLD</servlet-name>
<servlet-class>login</servlet-class>
</servlet>

<servlet-mapping>
<servlet-name>HELLOWORLD</servlet-name>
<url-pattern>/logincheck</url-pattern>
</servlet-mapping>
</web>