ADVANCED JAVA ET25MTCA058
/*Enrollment No –
Name – Singh Arohikumari Bimalkumar
Q29. Create a small project to demonstrate the MVC implementation using
RequestDispatcher as explained below: 1. Create a registration page to take student
details input. 2. Click on Submit, insert the details in the student table in the database,
and redirect the user to the home page, which displays a welcome message. 3. Home page
should display a link to fetch student details. On clicking this link, the student details
should be fetched from the database and be displayed on the page. Use the Model-View-
Controller pattern to implement the above. Also, use DAO classes to insert and fetch data
from the database. */
Code:
MyConnection:
package [Link];
import [Link];
import [Link];
public class MyConnection {
public static Connection getConnection() {
Connection con = null;
try {
[Link]("[Link]");
con =
[Link]("jdbc:mysql://localhost:3307/student?useSSL=false&serverTi
mezone=UTC",
"root", "");
} catch (Exception e) {
[Link]();
}
66
ADVANCED JAVA ET25MTCA058
return con;
}
}
[Link]
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Register</title>
</head>
<body>
<center>
<%
String err = (String) [Link]("error");
if(err!=null){
%>
<p style="color:red"><%= err %></p>
<% } %>
<br>
<h2>Register Student</h2>
<form action="RegisterServlet" method="post">
Name: <input name="name"><br>
Age: <input name="age"><br>
Gender: <input name="gender"><br>
DOJ: <input type="date" name="doj"><br>
<input type="submit" value = "Submit">
</form>
</center>
</body>
</html>
67
ADVANCED JAVA ET25MTCA058
RegisterServlet
String name = [Link]("name");
String ageStr = [Link]("age");
String gender = [Link]("gender");
String doj = [Link]("doj");
if ([Link]() || [Link]()) {
[Link]("error", "All fields required");
[Link]("[Link]").forward(request, response);
return;
}
int age = [Link](ageStr);
Student s = new Student();
[Link](name);
[Link](age);
[Link](gender);
[Link]([Link](doj));
int rows = [Link](s);
[Link]("Inserted rows = " + rows);
HttpSession session = [Link]();
[Link]("user", name);
Cookie c = new Cookie("username", name);
[Link](c);
[Link]("[Link]")
68
ADVANCED JAVA ET25MTCA058
InsertStudentDAO
package [Link];
import [Link];
import [Link];
import [Link];
public class InsertStudentDAO {
public static int insert(Student s) {
int rows = 0;
try {
Connection con = [Link]();
Statement st = [Link]();
String q = "INSERT INTO student(name,age,gender,doj) VALUES('" +
[Link]() + "'," + [Link]() + ",'"
+ [Link]() + "','" + [Link]() + "')";
rows = [Link](q);
} catch (Exception e) {
[Link]();
}
return rows;
}
}
[Link]
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
69
ADVANCED JAVA ET25MTCA058
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<%
String user = (String) [Link]("user");
%>
<h2>Welcome <%= user %></h2>
<a href="ViewServlet">click here to View Students</a>
</body>
</html>
ViewServlet
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
[Link]("list", [Link]());
[Link]("[Link]").forward(request, response);
}
FetchAllStudentDAO
package [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
public class FetchAllStudentDAO {
public static ArrayList<Student> getAll() {
ArrayList<Student> list = new ArrayList<>();
70
ADVANCED JAVA ET25MTCA058
try {
Connection con = [Link]();
Statement st = [Link]();
ResultSet rs = [Link]("SELECT * FROM student");
while ([Link]()) {
Student s = new Student();
[Link]([Link]("id"));
[Link]([Link]("name"));
[Link]([Link]("age"));
[Link]([Link]("gender"));
[Link]([Link]("doj"));
[Link](s);
}
} catch (Exception e) {
[Link]();
}
return list;
}
}
[Link]
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ page import="[Link].*,[Link]" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
71
ADVANCED JAVA ET25MTCA058
<%
List<Student> list = (List<Student>) [Link]("list");
%>
<h2>Student Records</h2>
<table border="1">
<tr>
<th>ID</th><th>Name</th><th>Age</th><th>Gender</th><th>DOJ</th>
</tr>
<%
for(Student s:list){
%>
<tr>
<td><%= [Link]() %></td>
<td><%= [Link]() %></td>
<td><%= [Link]() %></td>
<td><%= [Link]() %></td>
<td><%= [Link]() %></td>
</tr>
<% } %>
</table>
</body>
</html>
Output:
72
ADVANCED JAVA ET25MTCA058
73