Java Servlet Programing Laungage

Java Servlet Projects

Servlet Project 3

Java Servlet :Example of UserRegistration and Login
Previous Home Next

In this example,first the user need to register yourself and after that login using the servlet and JDBC.

index.html

<html>
	<head>
		<title>Login Test web Application</title>
	</head>
	<body>
		<form method="post" action="loginServlet">
		Name<input type="text" name="txtName"><br>
		Password<input type="password" name="password"><br>
		<input type="submit" value="submit">
		</form>
		<b>New User!</b><br>
		<a href="register.html">Register Yourself</a>
	</body>
</html>

 

register.html

<html>
	<head>
		<title>Login Test web Application</title>
	</head>
	<body>
		<form method="post" action="loginServlet">
		Name<input type="text" name="txtName"><br>
		Password<input type="password" name="password"><br>
		<input type="submit" value="submit">
		</form>
		<b>New User!</b><br>
		<a href="register.html">Register Yourself</a>
	</body>
</html>

web.xml

<web-app>
	<servlet>
		<servlet-name>gau</servlet-name>
		<servlet-class>LoginServlet</servlet-class>
	</servlet>

	<servlet-mapping>
		<servlet-name>gau</servlet-name>
		<url-pattern>loginServlet</url-pattern>
	</servlet-mapping>

	<servlet>
		<servlet-name>gauri</servlet-name>
		<servlet-class>RegisterServlet</servlet-class>
	</servlet>

	<servlet-mapping>
		<servlet-name>gauri</servlet-name>
		<url-pattern>registerServlet</url-pattern>
	</servlet-mapping>

</web-app>

LoginServlet.java

import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.sql.*;
public class LoginServlet extends HttpServlet
  {
   public void doPost(HttpServletRequest request,
      HttpServletResponse response)throws ServletException,IOException
    {
	try
	{
	  String name=request.getParameter("txtName");
	  String password=request.getParameter("password");
	  response.setContentType("text/html");
	  PrintWriter out=response.getWriter();
	  Class.forName("oracle.jdbc.driver.OracleDriver");
	  Connection con=DriverManager.getConnection
		  ("jdbc:oracle:thin:@localhost:1521:xe","system","gaurav");
	  PreparedStatement stmt=con.prepareStatement
		        ("select * from logindetails where name=? and password=?");
	  stmt.setString(1,name);
	  stmt.setString(2,password);
	  ResultSet rset=stmt.executeQuery();
	  if(rset.next())
	  out.println("Welcome," +name);
	  else
	  {
	    out.println("<b>Invalid username and password !</b><br>");
	    RequestDispatcher rd=request.getRequestDispatcher("index.html");
	    rd.include(request,response);
	    con.close();
	   }
	}
	catch(Exception e)
	{
		System.out.println(e);
	}
	}
}

RegisterServlet.java

import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.sql.*;
public class RegisterServlet extends HttpServlet
{
 public void doGet(HttpServletRequest request,
	 HttpServletResponse response)throws ServletException,IOException
{
   response.setContentType("text/html");
   PrintWriter out=response.getWriter();
   String name=request.getParameter("txtName");
   String password=request.getParameter("password");
   try
   {
	 Class.forName("oracle.jdbc.driver.OracleDriver");
	 Connection con=DriverManager.getConnection
			("jdbc:oracle:thin:@localhost:1521:xe","system","gaurav");
	 PreparedStatement stmt=con.prepareStatement
			("insert into LoginDetails values(?,?)");
	 stmt.setString(1, name);
	 stmt.setString(2, password);
	 int i=stmt.executeUpdate();
	 out.println("<b>You are successfully Registered</b>");
	 con.close();
	}
	catch(Exception e)
	{
	 out.println("<b>Registration failed</b>");
	 out.println("<b>Error:</b>" +e);
	}
	}
}

Tolal:0 Click:

Show All Comments

Did not find what you were looking for leave your name and message. We will revert within 24 hours
Name:
eMail:
Comment / Feedback:
Previous Home Next