关于将数据保存到sql数据库的问题

时间:2011-01-26 19:04:35

标签: java sql html hibernate

我试图通过在文本框中输入信息并单击提交按钮来将数据添加到sql数据库。但是,我无法让提交按钮正常工作。我对html并不熟悉,所以我可能做错了。这是一些代码。这是我的jsp页面,带有提交按钮。

<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>&nbsp;</td><td>&nbsp;</td><td><a href="/myweb">Go To Home Page</a></td><td>&nbsp;</td><td>&nbsp;</td></tr>
</table>

    <form name="student" action="process" method="post">
    <input type="hidden" name="view" value="addStudent" />
    <table>
    <tr><td>&nbsp;</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>&nbsp;</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>

这是我的servlet,其中进行查询以及将数据保存到数据库的位置。

@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.commit();
                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>

}

有什么建议吗?

2 个答案:

答案 0 :(得分:2)

您的表单的操作设置为“处理”。您的servlet配置为作为“注册器”调用,因此永远不会被调用。

答案 1 :(得分:0)

HTML表单中的操作是“进程”。 servlet映射到URL“registrar” 这两个应该匹配。否则,应用程序服务器不知道将HTTP请求发送到何处。

首先搜索简单的Servlet教程。它需要15分钟才能阅读,成为servlet的世界级专家。祝你好运!