2017-08-26 195 views
2

我是Java中servlet这样的概念的初学者,最近我一直在尝试着解决它们。我试图插入一行到customerid是自动生成的客户(customerid,用户名,密码)表,但不幸的是我总是面临着一个错误500.这是我的servlet,我的HTML文件和错误信息显示在我的控制台。错误500:通过servlet将数据插入到数据库中

newUserLoginServelet.java

package loginPackage; 

import java.io.IOException; 
import java.io.PrintWriter; 
import java.sql.Connection; 
import java.sql.PreparedStatement; 
import java.sql.SQLException; 
import java.sql.Statement; 

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

import resources.MyUtil; 

/** 
* Servlet implementation class NewUserLoginServlet 
*/ 
@WebServlet(description = "Here a new/first time user sets up his/her login credentials.", urlPatterns = { "/NewUserLoginServlet" }) 
public class NewUserLoginServlet extends HttpServlet { 
    private static final long serialVersionUID = 1L; 

    /** 
    * @see HttpServlet#HttpServlet() 
    */ 
    public NewUserLoginServlet() { 
     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 

     String username = request.getParameter("username"); 
     String password = request.getParameter("password"); 

     String SQL = "insert into table customer (username, password) values (" + "'" + username + "','" + password + "');" ; 

     Connection connection = MyUtil.getConnection(); /*Import resources.MyUtil */ 

     PrintWriter out = response.getWriter(); 
     response.setContentType("text/html"); 

     try { 
      /*Throws SQLException*/ 
      Statement statement = connection.createStatement(); 
      statement.executeQuery(SQL); 
      out.println("New user credentials injected into the database."); 
     } catch (SQLException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 
    } 

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

} 

的index.html

<!DOCTYPE html> 
<html> 
<head> 
<meta charset="UTF-8"> 
<title>Welcome to the SG Supermarket.</title> 
</head> 
<body> 

<h1>Welcome to the SG supermarket.</h1> 

<a href="signIn.html">In case you are not a new user.</a></br> 
<a href="signUp.html">In case you are a new user.</a> 
< 
/body> 
</html> 

signUp.html

<!DOCTYPE html> 
<html> 
<head> 
<meta charset="UTF-8"> 
<title>User sign up (First time user)</title> 
</head> 
<body> 
    <form action="NewUserLoginServlet"> 
    Please enter the credentials you wish to be.    </br> 
    Username : <input type="text" value="username">   </br> 
    Password : <input type="password" value="password">  </br> 
    <input type="submit" value="Submit your credentials."> </br> 
    </form> 
</body> 
</html> 

MyUtil.java

package resources; 

import java.sql.Connection; 
import java.sql.DriverManager; 
import java.sql.SQLException; 

public class MyUtil { 
    private static Connection connection = null; 
    private static String url = "jdbc:mysql://localhost:3306/mysql"; 
    private static String password = "*****"; 
    private static String user = "root"; 

    public static Connection getConnection() { 
     if (connection == null) { 
      try { 
       Class.forName("com.mysql.jdbc.Driver"); // ClassNotFoundException 
       connection = DriverManager.getConnection(url, user, password); 
      } catch (SQLException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } catch (ClassNotFoundException e) { 
       e.printStackTrace(); 
      } 
     } 

     return (connection); 
    } 
} 

错误消息:

Type Exception Report 

Description The server encountered an unexpected condition that prevented it from fulfilling the request. 

Exception 

java.lang.NullPointerException 
    loginPackage.NewUserLoginServlet.doGet(NewUserLoginServlet.java:51) 
    javax.servlet.http.HttpServlet.service(HttpServlet.java:635) 
    javax.servlet.http.HttpServlet.service(HttpServlet.java:742) 
    org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52) 
Note The full stack trace of the root cause is available in the server logs. 

是不是有什么毛病我的数据插入到数据库,或者如果我没有创造我的HTML和servlet之间的连接?任何帮助将不胜感激。我的数据库URL的

屏幕截图 enter image description here

的web.xml

<?xml version="1.0" encoding="UTF-8"?> 
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0"> 
    <display-name>SGCart2</display-name> 
    <welcome-file-list> 
    <welcome-file>index.html</welcome-file>  
    </welcome-file-list> 

    <servlet> 
    <description>NewUserLoginServlet</description> 
    <display-name>NewUserLoginServlet</display-name> 
    <servlet-name>NewUserLoginServlet</servlet-name> 
    <servlet-class>loginPackage.NewUserLoginServlet</servlet-class> 
    </servlet> 
    <servlet-mapping> 
    <servlet-name>NewUserLoginServlet</servlet-name> 
    <url-pattern>/NewUserLoginServlet</url-pattern> 
    </servlet-mapping> 

</web-app> 
+0

在您的形式尝试添加方法= “GET” – Austin

+0

@Austin不工作。不知道什么是错的。 –

+0

您是否将servlet添加到您的web.xml – Austin

回答

1

这条SQL语句是不正确的:

String SQL = "insert into table customer (username, password) values (" + "'" + username + "','" + password + "');" ; 

它应该是:

String SQL = "insert into customer (username, password) values ('" + username + "','" + password + "');" ; 

你也应该考虑做一个PreparedStatement相反,它是更简单,你不必惹采用双+单引号在一起,就像是它可以非常混乱左右,如果你的INSERT语句要大得多:

String username = request.getParameter("username"); 
    String password = request.getParameter("password"); 

try(Connection conn= MyUtil.getConnection()){ 

       PreparedStatement pst = conn.prepareStatement("insert into customer (username,password) values (?,?);"); 
       pst.setString(1, username); 
       pst.setString(2, password); 

       pst.executeUpdate();  

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

无论何时插入,更新,删除,您必须使用.executeUpdate();而不是.executeQuery();

另外我建议查找MVC体系结构。 (模型视图控制器)...在servlet中执行数据库命令不是一个好习惯。出于安全和组织的原因,你应该分开这个逻辑。

您得到的错误是来自NewUserLoginServlet中第51行的nullpointerexeption。这意味着这一行是错误的:

Statement statement = connection.createStatement(); 

删除解决此括号中的DBConnection的类(MyUtil):

return (connection); 

应该只是:

return connection; 

编辑:尝试本作您的DBConnection类别:

public class MyUtil { 
     private static Connection conn = null; 
     private static String url = "jdbc:mysql://localhost:3306/mysql"; 
     public static Connection getConnection() { 
       try { 
        Class.forName("com.mysql.jdbc.Driver"); 
        conn = DriverManager.getConnection(url, "root", "*****"); 
       } catch (Exception e) { 
        e.printStackTrace(); 
       } 
      return conn; 
     } 

编辑2:在你的web.xml,你应该有这样的事情:

<servlet> 
    <description>NewUserLoginServlet</description> 
    <display-name>NewUserLoginServlet</display-name> 
    <servlet-name>NewUserLoginServlet</servlet-name> 
    <servlet-class>loginPackage.NewUserLoginServlet</servlet-class> 
    </servlet> 
    <servlet-mapping> 
    <servlet-name>NewUserLoginServlet</servlet-name> 
    <url-pattern>/NewUserLoginServlet</url-pattern> 
    </servlet-mapping> 
+0

我做了更改,因为你问我,但我得到一个空白的屏幕,我的数据库也没有更新。我不会再有任何错误。 –

+0

@abhijeet在你的web.xml文件中,你的newuserlogin servlet映射到了什么地方? –

+0

没什么。它从index.html开始@Jonathan –

相关问题