0

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

McDuck4
  • 662
  • 1
  • 10
  • 33

2 Answers2

0

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">
David Levesque
  • 22,181
  • 8
  • 67
  • 82
  • YYYYEEEESSSSS. Now it works :-) It didn't use with the:
    But if you didn't suggested this, I wouldn't think of putting the
    in front of it. So cool. thanks a lot. I am gonna change the code so other people can see the correct one
    – McDuck4 Feb 28 '14 at 16:28
0

Correct code is here:

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

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);
            }
        }
    }
}
McDuck4
  • 662
  • 1
  • 10
  • 33