2017-08-26 8 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 –

相關問題