Servlet to create user profile

Steps


  • Create Html form to get data from user
  • Create database table to store user data
  • Create Servlet to accept request from user and Save User data into database
  • Run Servlet
Prerequisite:


  • Eclipse IDE
  • Apache tomcat
  • Required jar file MySQL connector and Servlet.jar
  • Make sure this both jar file present in webcontent/lib folder

jar

Step 1:Create HTML file in WebContent folder of Dynamic Web project


Createprofile.html
<html>
<head>
 
<style type="text/css">
 
{
 box-sizing: border-box;
}
 
html {
 font-family: 'Lato', sans-serif;
}
 
.section-form {
 width: 37em;
 margin: 3em auto;
 background-color: #f3efe7;
 padding: 2em;
}
.section-form .intro 
{
 margin-bottom: 2em;
}
.section-form h1 
{
 text-align: center;
 font-size: 1.6em;
 margin-bottom: 0.6em;
}
.section-form h2 
{
 text-align: center;
 font-size: 1.23077em;
}
 
input[type=text] 
{
 height: 45px;
}
 
input[type=text], textarea
 {
 
 width: 100%;
 margin-bottom: 1em;
 font-size: .9em;
 color: #cfcece;
 font-weight: 300;
 padding: .5em 1em;
 border: none;
 color: #555555;
 font-weight: 300;
}
 
input[type=password], textarea
 {
 
 width: 100%;
 margin-bottom: 1em;
 font-size: .9em;
 color: #cfcece;
 font-weight: 300;
 padding: .5em 1em;
 border: none;
 color: #555555;
 font-weight: 300;
}
 
input[type=Submit] 
{
 cursor: pointer;
 margin: 0;
 padding: .5em 2em;
 background: #636363;
 border: none;
 color: #fff;
 font-size: 1em;
 font-weight: 400;
}
 
</style>
 
</head>
</html>
<body>
<div class="section-form">
 <form action="http://localhost:8980/Javaforlearn/CreateProfile" method="post" >
 <div class="intro"> 
 <h1>Sign up today and get touch with us</h1>
 <h2></h2>
 </div>
 <input type="text" placeholder="Name" name="Name" tabindex="1"/>
 <input type="text" placeholder="User name" name="Username" tabindex="3"/>
 <input type="password" placeholder="Password" name="Pass" tabindex="2"/>
 
 <input type="Submit" value="Sign up">
 </form>
</div>
</body>
</html>

Create database table in MySQL.


Query:

Create table User_detail
( name varchar(20),
  UserName varchar(20),
  password varchar(20));

create-table

Create Servlet by extending Httpservlet in src folder of Dynamic web project.


package com.spy.javaforlearn;
 
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
 
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
 
 
public class CreateProfile extends HttpServlet 
{
    @Override
	protected void doPost(HttpServletRequest request, 
			              HttpServletResponse response)
			              throws ServletException, IOException 
 
	{
	       response.setContentType("text/html");
 
	       PrintWriter out=response.getWriter();
 
	       String name=request.getParameter("Name");
 
	       String Username=request.getParameter("Username");
 
	       String Password=request.getParameter("Pass");
 
 
	    Connection con = null;
		PreparedStatement pstmt = null;
 
		ResultSet rs = null;
 
		try 
		{
		   //1. Load the Driver
		  Class.forName("com.mysql.jdbc.Driver").newInstance();
 
		  //2. Get the DB Connection via Driver
		 String dbUrl = "jdbc:mysql://localhost:3306/test?user=root&password=root";
		 con = DriverManager.getConnection(dbUrl);
 
 
		 //3. Issue SQL Queries via Connection
		 String query = "insert into User_detail values(?,?,?) ";
 
 
		pstmt = con.prepareStatement(query);
		pstmt.setString(1,name);
		pstmt.setString(3,Password);	
		int count = pstmt.executeUpdate();
 
		if(count>0)
		{
 
		  out.print("Thanks for Sign up, Your profile successfully created");	
 
		}
	     }
 
	     catch(Exception e)  
		 {
 
	    	 out.print("Something goes wrong, Please try again");
 
		 }
 
		finally
		{
			//5. Close all JDBC Objects
			try 
			{
			   if(con != null)
                                {
			        con.close();
				}
			if(pstmt != null)
                                {
				   pstmt.close();
				}
			if(rs != null)
                                {
				  rs.close();
				}
			 } 
			catch (SQLException e)
			{
				e.printStackTrace();
			}
 
	         }	
	 }
  }
Configuring Web URL in web.xml

web.xml

 
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xmlns="http://java.sun.com/xml/ns/javaee" 
 xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
 http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
 <display-name>Javaforlearn</display-name>
 <welcome-file-list>
     <welcome-file>index.html</welcome-file>
     <welcome-file>index.htm</welcome-file>
     <welcome-file>index.jsp</welcome-file>
 </welcome-file-list>
 <servlet>
      <servlet-name>CreateProfile</servlet-name>
      <servlet-class>com.spy.javaforlearn.CreateProfile</servlet-class>
 </servlet>
 <servlet-mapping>
        <servlet-name>CreateProfile</servlet-name>
        <url-pattern>/CreateProfile</url-pattern>
 </servlet-mapping>
 
</web-app>
Accessing static Html page

123
Enter username and password and click on Sign upprofile
When you click Submit it calls the post method of servlet

Leave a Reply

Your email address will not be published. Required fields are marked *