JSP表单数据到MySQL DataBase?

时间:2014-05-04 14:32:00

标签: jsp servlets

请问我想知道如何使用JSP和Servlet将值插入MySQL数据库。这是我的代码:

如何将值输入数据库?

的index.jsp:

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>JSP Page</title>
    </head>
    <body>
        <form name=”registration” action="SaveUser" method="post">
First Name: <input type="text" name="user_name">
<br />
Password: <input type="password" name="pass_word" />
<input type="submit" value="Submit" />
</form>
    </body>
</html>

这是我的Servet - SaveUser.java:

import java.io.IOException; 
import java.sql.Connection;
import java.sql.DriverManager; 
import java.sql.PreparedStatement; 
import javax.servlet.ServletException; 
import javax.servlet.http.HttpServlet; 
import javax.servlet.http.HttpServletRequest; 
import javax.servlet.http.HttpServletResponse; 

public class SaveUser extends HttpServlet {

    protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String username = request.getParameter("user_name");
        String password = request.getParameter("pass_word");

        System.out.println("The username is" + username);
        System.out.println("\nand the password is" + password);

        try {
            Class.forName("com.mysql.jdbc.Driver");
            Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/userdb", "root", "root");

            String sql = "insert into usertable values (?,?)";
            PreparedStatement prep = con.prepareStatement(sql);

            prep.setString(1, username);
            prep.setString(2, password);
            prep.executeUpdate();
            prep.close();
        } catch (Exception E) {
            System.out.println("The error is an error");
        }
    }
}

我创建了我的表,我添加了库。它仍然给我这个错误:

  

HTTP状态404 - 未找到

     

输入状态报告

     

消息未找到

     

description请求的资源不可用。

     

GlassFish Server开源版4.0

我正在使用netbeans。这是一个服务器问题,我真的没有得到错误,我会感谢每一个对这个问题的回应。

2 个答案:

答案 0 :(得分:1)

试试这个:

添加一个doPost方法来检索表单发送的数据+你需要添加一个servlet UrlPattern,这样你就可以从浏览器访问servlet,为此你可以在web.xml文件或只是注释中进行比如下面的例子。

@WebServlet(description = "SaveUser", urlPatterns = {"/SaveUser"})
public class SaveUser extends HttpServlet {

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        System.out.println("Hello From deGet|SaveUser");
    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String username = request.getParameter("user_name");
        String password = request.getParameter("pass_word");

        try {
            Class.forName("com.mysql.jdbc.Driver");
            Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/userdb", "root", "root");

            String sql = "insert into usertable values (?,?)";
            PreparedStatement prep = con.prepareStatement(sql);

            prep.setString(1, username);
            prep.setString(2, password);
            prep.executeUpdate();
            prep.close();

            System.out.println("Debug: data inserted!");
        } catch (Exception E) {
            System.out.println("The error is an error");
        }
    }
}

答案 1 :(得分:1)

将您的Servlet更改为此

public class ExampleServlet extends HttpServlet {
private static final long serialVersionUID = 1L;

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    RequestDispatcher rd = request.getRequestDispatcher("NewFile.jsp");
    rd.forward(request, response);
}

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
     String username = request.getParameter("user_name");
        String password = request.getParameter("pass_word");

        try {
            Class.forName("com.mysql.jdbc.Driver");
            Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/newdb", "root", "root");

            String sql = "insert into employee(ename,password) values (?,?)";
            PreparedStatement prep = con.prepareStatement(sql);

            prep.setString(1, username);
            prep.setString(2, password);
            prep.executeUpdate();
            prep.close();

            System.out.println("Debug: data inserted!");
        } catch (Exception E) {
            System.out.println("The error is an error "  +E.getMessage()+ " "+E.getStackTrace());
        }
}

}

web.xml

中添加此内容
<servlet>
 <servlet-name>Submission</servlet-name>
 <servlet-class>com.example.test.ExampleServlet</servlet-class>
</servlet>
<servlet-mapping>
 <servlet-name>Submission</servlet-name>
 <url-pattern>/SaveUser</url-pattern>
</servlet-mapping>

运行Servlet,因为它应该决定应该加载哪个JSP页面。

另外,我建议您使用DAO进行数据库访问。