0

I am trying to insert college name into database using beans following MVC pattern but whenever I click on insert button I got 404 error. Here is the code

JSP File

 <%@page contentType="text/html" pageEncoding="UTF-8"%>
 <!DOCTYPE html>
   <html>
     <head>
       <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>Practical 5</title>
      </head>
       <body>
         <h1>Insert College</h1>
          <form action="NewServlet">
           Enter Name<input type="text" name="collegeName"><br>
            Enter City<input type="text" name="collegeCity"><br>
          Enter Year<input type="number" name="collegeYear"><br>
          Enter Fees<input type="number" name="collegeFees"><br>
         <input type="submit" name="Insert" value="Insert">

    </form>
     <%
    String msg=(String)request.getAttribute("msg");

    %>
    <h2><%=msg%></h2>
</body>
</html>

CollegeBean.java

 package college;

public class CollegeBean {

public String cname;
public String ccity;
public int year;
public float fees;

public String getCname() {
    return cname;
}

public void setCname(String cname) {
    this.cname = cname;
}

public String getCcity() {
    return ccity;
}

public void setCcity(String ccity) {
    this.ccity = ccity;
}

public int getYear() {
    return year;
}

public void setYear(int year) {
    this.year = year;
}

public float getFees() {
    return fees;
}

public void setFees(float fees) {
    this.fees = fees;
}


}

CollegeDB.java

 package college;
  import java.sql.*;
  public class CollegeDB {
public String insertOperation(CollegeBean collegeBeanObj) throws ClassNotFoundException,SQLException
{
    Class.forName("com.mysql.jdbc.Driver");
    Connection cn= DriverManager.getConnection("jdbc:mysql://localhost/jspractical5", "root", "");
    Statement st= cn.createStatement();
    int flag= st.executeUpdate("INSERT INTO college (c_name, c_city, c_year, c_fees) VALUES('"+collegeBeanObj.getCname()+"','"+collegeBeanObj.getCcity()+"','"+collegeBeanObj.getYear()+"','"+collegeBeanObj.getFees()+"')");
    if (flag!=0)
        return "Record Inserted";
    else
        return "Record not inserted";

}

}

Finally the controller file the servlet to handle all the stuff

NewServlet.java

 package college;

 import java.io.IOException;
 import java.io.PrintWriter;
 import java.sql.*;
 import javax.servlet.RequestDispatcher;
 import javax.servlet.ServletException;
 import javax.servlet.annotation.WebServlet;
 import javax.servlet.http.HttpServlet;
 import javax.servlet.http.HttpServletRequest;
 import javax.servlet.http.HttpServletResponse;

  @WebServlet(name = "NewServlet", urlPatterns = {"/college/NewServlet"})
  public class NewServlet extends HttpServlet {
   protected void processRequest(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    response.setContentType("text/html;charset=UTF-8");
   PrintWriter out = response.getWriter();
     try {
        String name=request.getParameter("name");
        String city=request.getParameter("city");
        int year=Integer.parseInt(request.getParameter("year"));
        float fees=Integer.parseInt(request.getParameter("fees"));

        CollegeBean collegeBeanObj= new CollegeBean();
        collegeBeanObj.setCname(name);
        collegeBeanObj.setCcity(city);
        collegeBeanObj.setYear(year);
        collegeBeanObj.setFees(fees);

        CollegeDB cd= new CollegeDB();
        String msg= cd.insertOperation(collegeBeanObj);
        request.setAttribute("msg", msg);
        RequestDispatcher rd= getServletContext().getRequestDispatcher("/index.jsp");
        rd.forward(request, response);
     }
     catch(Exception e)
     {
         out.println(e);
     }
}
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    processRequest(request, response);
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    processRequest(request, response);
}

@Override
public String getServletInfo() {
    return "Short description";
}

}
Bacteria
  • 8,406
  • 10
  • 50
  • 67
Mr. Pyramid
  • 3,855
  • 5
  • 32
  • 56

1 Answers1

0

The problem is with the line in the controller where you assign the RequestDipatcher.

May be the file path of index.html is wrong. Check the folder structure and insert the path accordingly. If the index.html is in same directory of the page where request is sent then in the file path of index.html, remove the leading '/'.

Also change the form action attribute URL to same URL pattern defined in urlPattrns in the controller annotation "/college/NewServlet"

Kaustav
  • 741
  • 1
  • 9
  • 18