Search code examples
mysqlservletssetstring

MySQL registrer data with setString data, but not with getParameter


I had a question regarding this before, but I got something of it to work with help from someone. That is really nice. The code is here:

<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>

Servlet:

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);
            }
        }
    }
}

So when I fx set the values like this the information is gonna be registered correct in the database.

                    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);

But when I use the form from my html site and put in the informations with the:

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");

I get an HTTP Status 404 error. Does anybody have any clue why this is? My guess is that there is something wrong between the JSP and Servlet?

Best Regards Mads


Solution

  • Try adding a leading slash to the action attribute of the form element, e.g.:

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

    instead of

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