我遇到内部服务器错误500

时间:2019-02-14 09:37:40

标签: eclipse servlets

我正在尝试使用jdbc在servlet中使用html页面,该页面显示正常,但是当我单击添加botton时,它显示以下错误:

  

HTTP状态500 –内部服务器错误

类型异常报告

说明:The server encountered an unexpected condition that prevented it from fulfilling the request.

我正在使用tomcat 9.0.4mysql server

这是我的代码:

 package com.teejay.userservlet;

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

 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 CreateUserServlet
   */
 @WebServlet("/addServlet")
 public class CreateUserServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
private Connection con;

/**
 * @see Servlet#init(ServletConfig)
 */
public void init(ServletConfig config) throws ServletException {
    try {
        con =            DriverManager.getConnection("jdbc:mysql://localhost:3306/mydb", "root", "Olamilekan1$%");

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

}

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

    String firstname=request.getParameter("firstName");
    String lastname=request.getParameter("lastName");
    String email=request.getParameter("email");
    String password=request.getParameter("password");

    try {
        Statement stmt = con.createStatement();
        int result = stmt.executeUpdate("insert into user values('"+firstname+"','"+lastname+"','"+email+"','"+password+"'); ");
        PrintWriter out = response.getWriter();

        if (result>0) 
        {
        out.println(result + "value(s) inserted");
        }

        else {

            out.println("Error inserting values... ");
        }

    } catch (SQLException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

}

 /**
 * @see Servlet#destroy()
 */
   public void destroy() {
    try {
        con.close();
    } catch (SQLException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    }


     }






here the html ...

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Add User</title>
</head>
<body>
<h1>User Registration:</h1>
<form method="post" action="addServlet">
    <table>
        <tr>
            <td>First Name:</td>
            <td><input name="firstName" /></td>
        </tr>
        <tr>
            <td>Last Name :</td>
            <td><input name="lastName" /></td>
        </tr>
        <tr>
            <td>Email:</td>
            <td><input name="email" /></td>
        </tr>
        <tr>
            <td>Password:</td>
            <td><input name="password" type="password" /></td>
        </tr>
        <tr>
            <td />
            <td><input type="submit" value="Add" /></td>
    </table>
 </form>
 </body>
 </html>

还有

我希望这些值将从html页面获取到数据库。

1 个答案:

答案 0 :(得分:0)

您需要添加以下行以init方法获得连接。

Class.forName("com.mysql.jdbc.Driver");  

它应该可以解决您的问题