Question about saving data to sql database
I am trying to add data to a sql database by entering info in text boxes and clicking a submit button. However, I cant get the submit button to work right. I am not real familiar with html so I may be doing something wrong. Here is some code. This is my jsp page with the submit button.
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Student Page</title>
</head>
<body>
<table border="1" align="center">
<tr><td>Student ID</td><td>First Name</td><td>Last Name</td><td>Degree Program</td><td>Gender</td></tr>
<%
List<Student> students = (List) request.getAttribute("student");
for (Student student : students) { %>
<tr><td><%=student.getStudentId()%></td><td><%=student.getFirstname()%></td><td><%=student.getLastname()%></td><td><%=student.getDegreeprogram()%></td><td><%=student.getGender()%></td></tr>
<% }
%>
<tr><td> </td><td> </td><td><a href="/myweb">Go To Home Page</a></td><td> </td><td> </td></tr>
</table>
<form name="student" action="process" method="post">
<input type="hidden" name="view" value="addStudent" />
<table>
<tr><td> </td><td><Strong>Add a Student</Strong></td></tr>
<tr><td>Student ID: </td><td><input type="text" name="studentid" id="studentid"/></td></tr>
<tr><td>First Name: </td><td><input type="text" name="firstname" id="firstname"/></td></tr>
<tr><td>Last Name: </td><td><input type="text" name="lastname" id="lastname"/></td></tr>
<tr><td>Degree Program: </td><td><input type="text" name="degreeprogram" id="degreeprogram"/></td></tr>
<tr><td>Gender: </td><td><input type="text" name="gender" id="gender"/></td></tr>
<tr><td> </td><td><input type="submit"/></td></tr>
</table>
</form>
<form name="exit" action="process" method="post">
<input type="hidden" name="view" value="home" />
<p align="center"><input type="button" value="Exit"/></p>
</form>
</body>
</html>
This is my servlet where the query is made and where the data should be saved to the database.
@WebServlet(name="regservlet", urlPatterns={"/registrar"})
public class regservlet extends HttpServlet {
/**
* Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
try {
String view = request.getParameter("view");
if (view != null) {
String jspName="/index.jsp";
HibernateUtil util = new HibernateUtil();
Session hibernateSession = (Session) util.getHibernateSession();
String tableName = "";
if (view.equalsIgnoreCase("Student")) {
tableName="Student";
Query q = hibernateSession.createQuery("from " + tableName);
List<Student> students = q.list();
jspName="/student.jsp";
request.setAttribute("student", students);
} else if (view.equalsIgnoreCase("Course")) {
tableName="Course";
Query q = hibernateSession.createQuery("from " + tableName);
List<Course> courses = q.list();
request.setAttribute("course", courses);
jspName="/course.jsp";
}
else if (view.equalsIgnoreCase("Enrollment")) {
tableName="Enrollment";
Query q = (Query) hibernateSession.createQuery("from " + tableName);
List<Enrollment> enrollment = q.list();
request.setAttribute("enrollment", enrollment);
jspName="/enrollment.jsp";
}
else if (view.equalsIgnoreCase("addStudent")) {
String studentid = request.getParameter("studentid");
String firstname = request.getParameter("firstname");
String lastname = request.getParameter("lastname");
String degreeprogram = request.getParameter("degreeprogram");
String gender = request.getParameter("gender");
Student myStudent = new Student(studentid, firstname, lastname, degreeprogram, gender);
hibernateSession.saveOrUpdate(myStudent);
Transaction t = hibernateSession.beginTransaction();
t.commit();
jspName="/index.jsp";
}
else if (view.equalsIgnoreCase("addCourse")) {
String courseid = request.getParameter("courseid");
String coursename = request.getParameter("course");
String coursesection = request.getParameter("coursesection");
String instructorId= request.getParameter("instructorid");
Course myCourse = new Course (courseid, coursename, coursesection, instructorId);
hibernateSession.saveOrUpdate(myCourse);
Transaction t = hibernateSession.beginTransaction();
t.commi开发者_JS百科t();
jspName="/index.jsp";
}
else if (view.equalsIgnoreCase("addEnrollment")) {
String enrollmentid = request.getParameter("enrollmentid");
String course = request.getParameter("course");
String student = request.getParameter("student");
String semester= request.getParameter("semester");
Enrollment myEnrollment = new Enrollment(enrollmentid, course, student, semester);
hibernateSession.saveOrUpdate(myEnrollment);
Transaction t = hibernateSession.beginTransaction();
t.commit();
jspName="/index.jsp";
}
else if (view.equalsIgnoreCase("home")) {
jspName="/index.jsp";
}
hibernateSession.close();
System.out.println("JSP NAME "+jspName);
this.getServletContext().getRequestDispatcher(jspName).forward(request, response);
}
}catch (Exception ex) {
ex.printStackTrace();
System.err.println("Initial SessionFactory creation failed." + ex);
}
}
// <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
/**
* Handles the HTTP <code>GET</code> method.
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}
/**
* Handles the HTTP <code>POST</code> method.
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}
/**
* Returns a short description of the servlet.
* @return a String containing servlet description
*/
@Override
public String getServletInfo() {
return "Short description";
}// </editor-fold>
}
Any suggestions?
Your form has its action set to "process". Your servlet is configured to be invoked as "registrar", so it never gets invoked.
Action in HTML form is "process". The servlet is mapped to URL "registrar" These two should match. Otherwise application server does not know where to send your HTTP request.
Search for simple Servlet tutorial first. It will take you 15 minutes to read it and become the world level specialist in servlets. Good luck!
精彩评论