As an enterprise system developer, you need to develop the registration page of the employee management system. The administrator of the system registers a new employee using the GUI developed in JSP. The required table ‘Employee’ is provided in mysql database with columns as employee id, name, address, city, state and zip code as shown in figure.
Write a GUI snippet/JSP code with GUI components to take the user’s input. Separately, write the servlet code to insert the input records from the JSP page into the ‘employee’ table using JDBC (Java Database Connectivity).
Note: You may assume the username and password for the database connectivity.
***Please upvote if you liked the answer***
Do the following to configure the application in Eclipse IDE:-
Screenshot of the Java code for indentation and the structure of the web application:-
Output:-
JSP and Servlet code to copy:-
<%@ page language="java" contentType="text/html;
charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html>
<html>
<head>
<title>Registration Page</title>
</head>
<body>
<div align="center" style="margin-top: 50px;">
<!--This is a HTML form
which submit all the values of its controls to the RegisterServlet
using a GET request-->
<form action="RegisterServlet">
Name: <input type="text" name="name" size="20px">
<br/>
Address: <input type="text" name="address" size="20px">
<br/>
Designation: <input type="text" name="designation"
size="20px"> <br/>
State:<input type="text" name="state" size="20px">
<br/>
ZipCode:<input type="text" name="zipcode" size="20px">
<br/>
<input type="submit" value="submit">
</form>
</div>
</body>
</html>
package com.university.servlet;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* Servlet implementation class RegisterServlet
*/
@WebServlet("/RegisterServlet")
public class RegisterServlet extends HttpServlet {
/**
* @see HttpServlet#doGet(HttpServletRequest request,
HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request,
HttpServletResponse response) throws ServletException,IOException
{
int result = -1;
// The below lines may be required
to register the JDBC driver if you are not
//using JDBC 4.0.The Servlet
container(Tomcat) may not find the driver if you do not
//include this code
try {
Class.forName("com.mysql.jdbc.Driver");
} catch (ClassNotFoundException e)
{
e.printStackTrace();
}
// End
//This is a try-with-resources
which automatically closes the Connection
//object here after use.
try (
Connection conn =
DriverManager.getConnection("jdbc:mysql://localhost:3306/office?allowPublicKeyRetrieval=true&useSSL=false&serverTimezone=UTC",
"root", "root");//Username is root
and password is root to the MYSQL database
)
{
//This is a
statement made from an insert query to insert records into database
tables using JDBC.The wildcards characters are placeholders
for
//dynamically
populating the statement
PreparedStatement stmt = conn.prepareStatement("INSERT INTO
employee(employee_name,address,designation,state,zipcode) "
+ "values
(?,?,?,?,?)");
stmt.setString(1, request.getParameter("name"));
stmt.setString(2, request.getParameter("address"));
stmt.setString(3, request.getParameter("designation"));
stmt.setString(4, request.getParameter("state"));
stmt.setString(5, request.getParameter("zipcode"));
//As the above
is a DML statement,it returns row count after successful entry
which is a positive number
result =
stmt.executeUpdate();
} catch(SQLException ex) {
ex.printStackTrace();
}
//If insertion is successful,show
this message in the browser
if (result > 0)
response.getWriter().append("Successfully Registered!");
}
}
Get Answers For Free
Most questions answered within 1 hours.