0% found this document useful (0 votes)
20 views6 pages

Exp8 M2

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views6 pages

Exp8 M2

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

Lab Sheet4b

Problem Statement : MVC design pattern with database

AIM:

To implement a web application based on the MVC design pattern with data base.

ALGORITHM

Step 1 : create a database named "employee" in MySQL. Let's create an employee1 table using below DDL script:

CREATETABLEemployee1
(idint,first_namevarchar(20),last_namevarchar(20),usernamevarchar(250),passwordvarchar(20) ,addressvarchar(45
),contactvarchar(45));

Step 2: Create the Employee class which will act as our Model class.

Step 3: EmployeeDao class that contains JDBC code to connect with the MySQL database.

Step 4: create an EmployeeServlet class to process HTTP request parameters and redirect to the
appropriate JSP page after request data stored in the database:

Step 5: design an employee registration HTML form employeeregister.jsp

Step 6:After an employee successfully registered then employeedetails.jsp show a successful message on screen

PROGRAM:

Employee.java

package com.pu;

import java.io.*;

publicclass Employee {
privateintid;
private String firstName;
private String lastName;
private String username;
private String password;
private String address;
private String contact;

public int getId() {


return id;
}
public void setId(intid) {
this.id = id;
}
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public String getContact() {
return contact;
}
public void setContact(String contact) {
this.contact = contact;
}
}

EmployeeServlet.java

package com.pu;

import java.sql.*;
import jakarta.servlet.*;
import java.io.*;

publicclass EmployeeServlet extends HttpServlet {

protected void doPost(HttpServletRequest request, HttpServletResponse


response) throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter out=response.getWriter();
int id = Integer.valueOf(request.getParameter("id"));
String fn=request.getParameter("firstName");
String ln=request.getParameter("lastName");
String un=request.getParameter("username");
String pwd=request.getParameter("password");
String addr=request.getParameter("address");
String cont=request.getParameter("contact");
Employee emp = new Employee();
emp.setId(id);
emp.setFirstName(fn);
emp.setLastName(ln);
emp.setUsername(un);
emp.setPassword(pwd);
emp.setAddress(addr);
emp.setContact(cont);
request.setAttribute("emp",emp);
int result = 0;
try{
Class.forName("com.mysql.cj.jdbc.Driver");
Connection connection =
DriverManager.getConnection("jdbc:mysql://localhost:3306/god","root","adm
in123");
String query = "INSERT INTO employee1 (id, first_name, last_name,
username, password, address, contact) VALUES (?, ?, ?, ?, ?,?,?);";
PreparedStatement preparedStatement =
connection.prepareStatement(query);
preparedStatement.setInt(1, emp.getId());
preparedStatement.setString(2, emp.getFirstName());
preparedStatement.setString(3, emp.getLastName());
preparedStatement.setString(4, emp.getUsername());
preparedStatement.setString(5, emp.getPassword());
preparedStatement.setString(6, emp.getAddress());
preparedStatement.setString(7, emp.getContact());
System.out.println(preparedStatement);
result = preparedStatement.executeUpdate();
RequestDispatcher
rd=request.getRequestDispatcher("employeedetail.jsp");
rd.forward(request,response);

} catch (Exception e) {
System.err.println(e);
}
}

employeeregister.jsp

<%@pagelanguage="java"contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPEhtml>
<html>
<head>
<metacharset="ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<h1>Employee Register Form</h1>
<formaction="EmployeeServlet"method="post">
<tablestyle="with: 50%">
<tr>
<td>Registration Id</td>
<td><inputtype="text"name="id"/></td>
</tr>
<tr>
<td>First Name</td>
<td><inputtype="text"name="firstName"/></td>
</tr>
<tr>
<td>Last Name</td>
<td><inputtype="text"name="lastName"/></td>
</tr>
<tr>
<td>UserName</td>
<td><inputtype="text"name="username"/></td>
</tr>
<tr>
<td>Password</td>
<td><inputtype="password"name="password"/></td>
</tr>
<tr>
<td>Address</td>
<td><inputtype="text"name="address"/></td>
</tr>
<tr>
<td>Contact No</td>
<td><inputtype="text"name="contact"/></td>
</tr>
</table>
<inputtype="submit"value="Submit"/></form>
</body>
</html>

employeedetail.jsp

<%@pagelanguage="java"contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@pageimport="com.pu.*"%>
<!DOCTYPEhtml>
<html>
<head>
<metacharset="ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<%
out.print("You are successfully registered");
%>
</body>
</html>

RESULT:

Thus, to implement a simple MVC web application using JSP and Servlet.

You might also like