MCQs Test!! VB Basic Test !! Essentials of VB !! Creating User Services !! function !! Anatomy !! GK March-2019 !! DATA TYPES IN JAVA !! Classes ,Constructor ,Methods,Inheritance !! Ecology-I !! Biology 2019 !! Gk april 2019 !! Biology (living world) !! Plant anatomy !! CELL STRUCTURES AND FUNCTION !! General biology !! Human physiology !! Data structure !! Ecology-II !! GK TEST !! Reproduction in plants !! Human reproduction !! The ABC of C !! Operating system set1 !! Operating system set2 !! Gk and current affairs !! Animal kingdom !! Number system !! Animal kingdom-II !! Gk TEST-III !! Number series !! Microorganisms !! Interview question in c !! Time and Work !! Percentage !! General history !! Average !! Cells !! Locomotion and Movement !! paging !! Bacteriology !! Page replacement policy !! Secondary memory and Disk scheduling !! Introduction of Nervous System and neurons !! Plant harmones !! Transport and Mineral Nutrition in Plants !! Neurotransmitters !! Modern Indian History !! Photosynthesis in Plants !! Modern Indian History-II !! Plant respiration !! Environment & Biodiversity !! Environment & Biodiversity - II !! Algae, Bryophytes, Pteridophytes !! Area !! Cricket !! Days of the Year (India and the World) !! Plant Pathology - I !! Indian Transport System !! Plant Pathology - II !! Aptitude !! Biology !! Botany !! C language !! Computer Science !! Core Java !! Gate !! GK !! Mysql !! Operating system !! Physics !! VB-Visual Basic !! Zoology

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);
	}
	}
}
Previous Home Next