J2EE The following simple application stores student name and scores in a databa
ID: 3849962 • Letter: J
Question
J2EE
The following simple application stores student name and scores in a database table. The student enters their name and the program displays the score. The app consists of index.html, a JSP to display out, a servlet and a JPA entity class Student.
<!DOCTYPE html>
<-- index.html -->
<html>
<head>
<title>Exam 1</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
<form action="exam1servlet" method="post">
Enter your name
<input type="text" name="name"/>
<input type="submit" />
</form>
</body
</html>
@WebServlet(name = "exam1servlet", urlPatterns = {"/exam1servlet"})
public class exam1servlet extends HttpServlet {
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
EntityManagerFactory emf = Persistence.createEntityManagerFactory("Exam1PU");
EntityManager em = emf.createEntityManager();
String key_name = request.getParameter("name");
Student student = em.find(Student.class, key_name);
request.setAttribute("score", Double.toString(student.getScore()) );
getServletContext().getRequestDispatcher("/exam1score.jsp")
.forward(request,response);
}
}
@Entity
public class Student implements Serializable {
private static final long serialVersionUID = 1L;
@Id
private String name;
private double score;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getScore() {
return score;
}
public void setScore(double score) {
this.score = score;
}
}
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<-- exam1score.jsp -->
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Exam 1 Score</title>
</head>
<body>
Name <%= request.getParameter("name") %>
<br>
Score <%= request.getAttribute("score") %>
</body>
</html>
a.) Explain how the em.find statement in the servlet is used to retrieve data from the database table. [your answer should include a description of what a JPA entity class is, how an entity class and class properties are related to a database table, column names and primary key.]
b.) We want to change the application so that an integer value (student number) rather than student name is entered in index.html. The database table will now have columns for student id (the new primary key), name (remains a column but not primary key), and score. The output jsp will now report student id, name and score.
If the student id is not found in the database, the output jsp should report the message (“Student ID not found”).
Describe the changes needed to
Index.html
Exam1score.jsp
Student.java
Exam1servlet.java
Explanation / Answer
a.)
The entity class is nothing but a pojo class which has the ability to map to a database table. In an entity class we will define column names as variable names and generate mutators and accessor methods (setters and getters to it). All we need to do is specify the annotations like @Entity, @ID etc and generate setters and getters. The JPA framework automatically takes care of mapping our class to the database table. We need not bother about that. We can directly perform database operations on this entity class.
fiind() is one of the important method provided by JPA framework. We can pass Id or name as a parameter to find(), if it finds a particular id in the table it returns the object else it will return null.
b.)
In order to fulfill your requirement, your have to do 4 major modifications to your application.
First of all, do change the field and label of that field in index.html, make sure that you read id not name
Then drop name as primary key and make id as primary key. For this, I think you have to drop the entire table and have to create a new one.
Then in your servlet read id as request parameter and pass it as an argument to find().
lastly, in your jsp write code to show result message if student id is not found in database display "student id not found".
Please do comment if you still have any doubts. I Will clarify them.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.