0

我之前对此有过疑问,但在某人的帮助下我得到了一些帮助。那真是太好了。代码在这里:

<body>
    <form action="dataExchange" method="POST">
        Date:           <input type="text" name="Date"><br>
        Name:           <input type="text" name="Name"><br>
        Address:        <input type="text" name="Address"><br>
        Allday Hours:   <input type="text" name="Allday_hours"><br>
        Day Hours:      <input type="text" name="Day_hours"><br>
        Day Minutes:    <input type="text" name="Day_minutes"><br>
        Km To Address:  <input type="text" name="Km_to_address"><br>
        Time To Address:<input type="text" name="Time_to_address"><br>
                        <input type="submit" value="submit">
    </form>
</body>

小服务程序:

package WorkPackage;

import java.io.*;
import java.sql.*;
import javax.servlet.*;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.*;

@WebServlet("/dataExchange")
public class dataExchange extends HttpServlet{

    private static final long serialVersionUID = 1L;

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        doPost(request, response);
    }

    public void init(ServletConfig config) throws ServletException{
        super.init(config);
    }

    public void doPost(HttpServletRequest req, HttpServletResponse res) 
        throws ServletException, IOException{

        String connectionURL = "jdbc:mysql://localhost/NekiWork";
        Connection connection=null;

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

        String Date = req.getParameter("Date");
        String Name = req.getParameter("Name");
        String Address = req.getParameter("Address");
        String Allday_hours = req.getParameter("Allday_hours");
        String Day_hours = req.getParameter("Day_hours");
        String Day_minutes = req.getParameter("Day_minutes");
        String Km_to_address = req.getParameter("Km_to_address");
        String Time_to_address = req.getParameter("Time_to_address");

        try {

            Class.forName("com.mysql.jdbc.Driver");
            connection = DriverManager.getConnection(connectionURL, "root", ""); 
            String sql = "INSERT INTO workdata VALUES (?,?, ?, ?, ?, ?, ?, ?)";
            PreparedStatement pst = connection.prepareStatement(sql);
            pst.setString(1, Date);
            pst.setString(2, Name);
            pst.setString(3, Address);
            pst.setString(4, Allday_hours);
            pst.setString(5, Day_hours);
            pst.setString(6, Day_minutes);
            pst.setString(7, Km_to_address);
            pst.setString(8,  Time_to_address);

            pst.executeUpdate();
            pst.close();
        }
        catch(ClassNotFoundException e){

            out.println("Couldn't load database driver: " + e.getMessage());
        }
        catch(SQLException e){
            out.println("SQLException caught: " + e.getMessage());
        }
        catch (Exception e){
            out.println(e);
        }
        finally {

        try {
            if (connection != null) connection.close();
        }
            catch (SQLException ignored){
                out.println(ignored);
            }
        }
    }
}

因此,当我设置这样的值时,信息将在数据库中正确注册。

                    pst.setString(1, 1999-01-01);
        pst.setString(2, Mads);
        pst.setString(3, Skolevej);
        pst.setString(4, 23);
        pst.setString(5, 12);
        pst.setString(6, 49);
        pst.setString(7, 56);
        pst.setString(8,  32);

但是,当我使用我的 html 站点中的表单并输入以下信息时:

String Date = req.getParameter("Date");
        String Name = req.getParameter("Name");
        String Address = req.getParameter("Address");
        String Allday_hours = req.getParameter("Allday_hours");
        String Day_hours = req.getParameter("Day_hours");
        String Day_minutes = req.getParameter("Day_minutes");
        String Km_to_address = req.getParameter("Km_to_address");
        String Time_to_address = req.getParameter("Time_to_address");

我收到 HTTP 状态 404 错误。有人知道这是为什么吗?我的猜测是 JSP 和 Servlet 之间有问题?

最好的问候

4

2 回答 2

0

正确的代码在这里:

<%@ page language="java" contentType="text/html; charset=US-ASCII"
    pageEncoding="US-ASCII"%>
<!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=US-ASCII">
<title>Working Hours</title>
</head>
<body>
    <form action="../dataExchange" method="POST">
        Date:           <input type="text" name="Date"><br>
        Name:           <input type="text" name="Name"><br>
        Address:        <input type="text" name="Address"><br>
        Allday Hours:   <input type="text" name="Allday_hours"><br>
        Day Hours:      <input type="text" name="Day_hours"><br>
        Day Minutes:    <input type="text" name="Day_minutes"><br>
        Km To Address:  <input type="text" name="Km_to_address"><br>
        Time To Address:<input type="text" name="Time_to_address"><br>
                        <input type="submit" value="submit">
    </form>
</body>
</html>

小服务程序:

package WorkPackage;

import java.io.*;
import java.sql.*;
import javax.servlet.*;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.*;

@WebServlet("/dataExchange")
public class dataExchange extends HttpServlet{

    private static final long serialVersionUID = 1L;

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        doPost(request, response);
    }

    public void init(ServletConfig config) throws ServletException{
        super.init(config);
    }

    public void doPost(HttpServletRequest req, HttpServletResponse res) 
        throws ServletException, IOException{

        String connectionURL = "jdbc:mysql://localhost/NekiWork";
        Connection connection=null;

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

        String Date = req.getParameter("Date");
        String Name = req.getParameter("Name");
        String Address = req.getParameter("Address");
        String Allday_hours = req.getParameter("Allday_hours");
        String Day_hours = req.getParameter("Day_hours");
        String Day_minutes = req.getParameter("Day_minutes");
        String Km_to_address = req.getParameter("Km_to_address");
        String Time_to_address = req.getParameter("Time_to_address");

        try {

            Class.forName("com.mysql.jdbc.Driver");
            connection = DriverManager.getConnection(connectionURL, "root", ""); 
            String sql = "INSERT INTO Workdata VALUES (?,?, ?, ?, ?, ?, ?, ?)";
            PreparedStatement pst = connection.prepareStatement(sql);
            pst.setString(1, Date);
            pst.setString(2, Name);
            pst.setString(3, Address);
            pst.setString(4, Allday_hours);
            pst.setString(5, Day_hours);
            pst.setString(6, Day_minutes);
            pst.setString(7, Km_to_address);
            pst.setString(8,  Time_to_address);

            pst.executeUpdate();
            pst.close();
        }
        catch(ClassNotFoundException e){

            out.println("Couldn't load database driver: " + e.getMessage());
        }
        catch(SQLException e){
            out.println("SQLException caught: " + e.getMessage());
        }
        catch (Exception e){
            out.println(e);
        }
        finally {

        try {
            if (connection != null) connection.close();
        }
            catch (SQLException ignored){
                out.println(ignored);
            }
        }
    }
}
于 2014-02-28T16:29:26.123 回答
0

尝试在元素的action属性中添加前导斜杠,例如:form

<form action="/dataExchange" method="POST">

代替

<form action="dataExchange" method="POST">
于 2014-02-28T16:22:05.093 回答