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

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>



5 comments:

  1. how to build web.xml for this,
    how to create war file for this
    give the complete structure asasp

    ReplyDelete
  2. please provide me the exact structure with xml evrything so that i can produce it to the trainee manager so he will not fire me

    ReplyDelete
  3. Hi rahul,
    first sory for delay
    now

    1. create directory(your site's name i.e. master) in webapp folder in apache

    tomcat directory like.

    C:Program FilesApache Software FoundationTomcat 7.0webappsmaster

    2. arrange structure as

    master/WEB-INF/classes/login.java --all java files and class files
    master/WEB-INF/web.xml
    master/index.html

    3. in web.xml write

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

    4. to run open browser type in url bar
    http://localhost:8080/master
    (here 8080 is port assigned to apache tomcat while installing it)

    ReplyDelete
  4. i had just placed a link to download entire program, extract and copy "master" directory in webapp directory of apache tomcat

    Feel free to ask questions :-) Good Luck programer :-)

    ReplyDelete