我是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的
的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>
在您的形式尝试添加方法= “GET” – Austin
@Austin不工作。不知道什么是错的。 –
您是否将servlet添加到您的web.xml – Austin