使用Servlet将数据存储在mysql中时出错

时间:2018-07-17 11:47:45

标签: java mysql servlets

我是Java的一名新兴工程师。我有一个程序,需要从Servlets将员工详细信息插入MYSQL db。这是我写的程序。

Employee.java

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;

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


@SuppressWarnings("serial")
public class Employee extends HttpServlet {

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException 
    {
        try
        {
        response.setContentType("text/html");
        PrintWriter out=response.getWriter();

        String na=request.getParameter("na");
        String add=request.getParameter("add");

            Class.forName("com.mysql.jdbc.Driver");
            Connection con=DriverManager.getConnection("jdbc:mysql://localhost:8080/servlet","root","root");
            PreparedStatement ps=con.prepareStatement("insert into employee values(?,?,?)");
            ps.setString(1,na);
            ps.setString(2,add);
            System.out.print(na+" "+add);
            int i=ps.executeUpdate();
            System.out.println(i);


        out.println("<html>");
        out.println("<body>");
        out.println("<h1>create employee Succssffuly</h1>");
        out.println("<A HREF=index.html>one more to create</A>");
        out.println("</body></html>");
        out.close();
        }
        catch(Exception e)
            {
            System.out.println(e);
            e.printStackTrace();
            }
    }

    }

login.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<form action="employee" method="Post">
<table>
<tr><td>Name</td><td><input type="text" name="na"></td></tr>
<tr><td>Address</td><td><input type="text" name="add"></td></tr>
<tr><td></td><td><input type="submit" value="save"></td></tr>
</table>
</form>
</body>
</html>

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>Simple</display-name>
  <servlet>
  <servlet-name>simple</servlet-name>
  <servlet-class>Employee</servlet-class>
  </servlet>
  <servlet-mapping>
  <servlet-name>simple</servlet-name>
  <url-pattern>/simple</url-pattern>
  </servlet-mapping>
  <welcome-file-list>
    <welcome-file>login.jsp</welcome-file>
  </welcome-file-list>
</web-app>

当我运行程序时,它会要求输入名称和地址。保存详细信息后,将显示以下错误页面

** HTTP状态404-/ Simple / employee

类型状态报告 消息/ Simple / employee

说明所请求的资源不可用。

Apache Tomcat / 7.0.37 **

我不明白我做错了什么。如有人指导我,我会很高兴。TIA

4 个答案:

答案 0 :(得分:0)

您的URL /链接错误

  

** HTTP状态404-/ Simple / employee

按照web.xml

<url-pattern>/simple</url-pattern>

您的表单action必须指向简单

<form action="simple" method="Post">

或者只是在web.xml中更改

<url-pattern>/employee</url-pattern>

答案 1 :(得分:0)

您要仔细检查servlet映射规则的大小写。您写道:

<servlet-mapping>
<servlet-name>simple</servlet-name>
<url-pattern>/simple</url-pattern>
</servlet-mapping>

您应该写的地方:

<servlet-mapping>
<servlet-name>simple</servlet-name>
<url-pattern>/Simple</url-pattern>
</servlet-mapping>

另外,您没有可导致/ Simple / employee的模式。您可能只想重定向到/ Simple,或将模式编辑为/ Simple / employee

如果只想重定向到/ Simple,请编辑此行:

<form action="employee" method="Post">

收件人:

<form action="/Simple" method="Post">

答案 2 :(得分:0)

在login.jsp中,action =“ employee”通过 action =“ Employee” 进行更改 您无法提供正确的servlet名称,因此无法找到404页面。

答案 3 :(得分:0)

在login.jsp中,action =“ employee”更改为action =“ Employee” 由于Java区分大小写,所以您找不到404页面。