Skip to main content

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

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

jar

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