Download Servlet Example to authenticate from here
<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>
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);
}
}
}
<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-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>
[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>
how to run this
ReplyDeletehow to build web.xml for this,
ReplyDeletehow to create war file for this
give the complete structure asasp
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
ReplyDeleteHi rahul,
ReplyDeletefirst 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)
i had just placed a link to download entire program, extract and copy "master" directory in webapp directory of apache tomcat
ReplyDeleteFeel free to ask questions :-) Good Luck programer :-)