使用JSP(Eclipse)将数据插入数据库(XAMPP,MySQL)

问题描述 投票:0回答:1

所以我试图将JSP表单中的(插入)记录注册到数据库(XAMPP,MySQL)。详情如下:

register.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>

    <form action = "RegisterController" method = "post">
        <input type = "text" name = "email" placeholder = "Your e-mail"><br><br>
        <input type = "text" name = "firstname" placeholder = "First name"><br><br>
        <input type = "text" name = "lastname" placeholder = "Last name"><br><br>
        <input type = "text" name = "address" placeholder = "Home address"><br><br>
        <input type = "text" name = "phone" placeholder = "Your phone number"><br><br>
        <input type = "password" name = "password"><br><br>
        <input type = "submit" name = "submit" value = "Register">
    </form>

</body>
</html>

register controller.Java

package cms.com;

import java.io.IOException;


import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class RegisterController
 */
public class RegisterController extends HttpServlet {
    private static final long serialVersionUID = 1L;

    /**
     * @see HttpServlet#HttpServlet()
     */
    public RegisterController() {
        super();
        // TODO Auto-generated constructor stub
    }

    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
    }

    /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        try{

            String email = request.getParameter("email");
            String firstname = request.getParameter("firstname");
            String lastname = request.getParameter("lastname");
            String address = request.getParameter("address");
            String phone = request.getParameter("phone");
            String password = request.getParameter("password");
            String sql = "insert into users(email,firstname,lastname,address,phone,password) values(?,?,?,?,?,?)";
            Class.forName("com.mysql.jdbc.Driver");
            Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/cms","root","");
            PreparedStatement pst = conn.prepareStatement(sql);

            pst.setString(1,email);
            pst.setString(2,firstname);
            pst.setString(3,lastname);
            pst.setString(4,address);
            pst.setString(5, phone);
            pst.setString(6, password);

            pst.executeUpdate();
            PrintWriter out = response.getWriter();
            out.println("You have successfully registered!");

        }
        catch(ClassNotFoundException e){
            e.printStackTrace();
        }
        catch(SQLException e){
            e.printStackTrace();
        }
     }

}

因此,当我在localhost上运行并输入详细信息时,它只显示一个空页面,并且该表没有任何更新.Debugging显示没有错误以及运行时。我正在使用Eclipse Luna和Dynamic Module 2.5以及Apache Tomcat 7.0。

java mysql eclipse jsp servlets
1个回答
0
投票

你必须在项目中添加Mysql jar,你必须在Web.xml文件中映射RegisterController文件(WebContent\WEB-INF\Web.xml

并检查Register.jsp中'form action'的路径和Web.xml中的'url-pattern'两者必须相同。

© www.soinside.com 2019 - 2024. All rights reserved.