0

So I am doing this project where I go to a page first and I have to select from a drop down list. The first option is for adding to the database. The second if for viewing the database. The second option is just a forward to a jsp page which works fine. The first option is giving me error. So initialluy we are on index.jsp (where we select from option 1 and 2)-> then we get optionselect.java servlet, if option 1 was chosen we get forwarded to listnew.jsp. The listnew.jsp has a nice table to enter data for pid, content etc. When I submit this form its supposed to POST to newrecord.java servlet this is where I get the 404 error.

listnew.jsp

<?xml version = "1.0"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
   "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<%-- beans used in this JSP --%>
<jsp:useBean id = "prob" scope = "page" 
   class = "edu.umsl.math.beans.Problem" />
<jsp:useBean id = "probData" scope = "request" 
   class = "edu.umsl.math.dao.newrecord" />

<html xmlns = "http://www.w3.org/1999/xhtml">

<head>
   <title>Adding new question to question bank</title>

   <style type = "text/css">
      body { 
         font-family: tahoma, helvetica, arial, sans-serif;
      }

      table, tr, td { 
         font-size: .9em;
         border: 3px groove;
         padding: 5px;
         background-color: #dddddd;
      }
   </style>
</head>

<body>
<jsp:setProperty name = "prob" property = "*" />

   <% // start scriptlet

      //if (prob.getContent() == null) {

   %> <%-- end scriptlet to insert fixed template data --%>
   <form method="post" action="newrecord">
            <p>Enter the problem id,Content and order number</p>

            <table>

               <tr>
                  <td>Problem id</td>

                  <td>
                     <input type = "text" name = "pid" />
                  </td>
               </tr>

               <tr>
                  <td>Order number</td>

                  <td>
                     <input type = "text" name = "order_num" />
                  </td>
               </tr>
               <tr>
                  <td>Content</td>

                  <td>
                     <input type = "text" name = "content" />
                  </td>
               </tr>

               <tr>
                  <td colspan = "2">
                     <input type = "submit" 
                        value = "Submit" />
                  </td>
               </tr>
            </table>
            </form>
            <% // continue scriptlet
     //       }  // end if
     // else {
       //   probData.addProblem( prob );

    //      <jsp:forward page = "listafteradd.jsp" />

    // continue scriptlet
//      }  // end else

   %> <%-- end scriptlet --%>

</body>
</html>

newrecord.java

package edu.umsl.math.dao;

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

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

import edu.umsl.math.beans.Problem;

/**
 * Servlet implementation class newrecord
 */
@WebServlet("/newrecord")
public class newrecord extends HttpServlet {
    private static final long serialVersionUID = 1L;
    private PreparedStatement addRecord;
    private Connection connection;

    public void init(ServletConfig config) throws ServletException {
        // attempt database connection and create PreparedStatements

        try {
            Class.forName("com.mysql.jdbc.Driver");
            connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/mathprobdb", "root", "");

            addRecord = connection.prepareStatement("INSERT INTO problem ( "
                    + "pid, content, order_num ) " + "VALUES ( ?, ?, ? )");
        } catch (Exception exception) {
            exception.printStackTrace();
         throw new UnavailableException(exception.getMessage());
        }

    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        try {
            // set up response to client
            response.setContentType("text/html");
            PrintWriter out = response.getWriter();
        String p = request.getParameter("pid");
        String c= request.getParameter("content");
        String o= request.getParameter("order_num");
        if(p==null || c==null ||o==null)
        {

            out.print("Please enter all the entries");
        }
        else
        {
            out.print("pid: "+p);
            out.print("content:"+c);
            out.print("order"+o);

            int pid = Integer.parseInt(p);
            int order=Integer.parseInt(o);

            addRecord.setInt(1, pid);
            addRecord.setString(2, c);
            addRecord.setInt(3, order);

            addRecord.executeUpdate();
            RequestDispatcher view = request.getRequestDispatcher("listafteradd.jsp");

            view.forward(request, response);
            }
        }catch(Exception exception){
                exception.printStackTrace();
                 throw new UnavailableException(exception.getMessage());
                }

        }

    /*public void addProblem(Problem prob) throws SQLException {
        addRecord.setInt(1, prob.getPid());
        addRecord.setString(2, prob.getContent());
        addRecord.setInt(3, prob.getOrder_num());

        addRecord.executeUpdate();
    }*/
}
Fairy
  • 101
  • 2
  • 12

1 Answers1

0
  1. Did you check if you are inside your doPost in the servlet?

  2. You are redirecting to listafteradd.jsp after the insert. Does the page exist?

I created a listafteradd.jsp page and tested without your database connection/insert. It works fine.

rhenesys
  • 174
  • 1
  • 11