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 とサーブレットの間に何か問題があるのでしょうか?

宜しくお願いします マッツ

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 に答える