JSP-无法使用JSP Bean插入记录

时间:2016-04-26 16:21:48

标签: java jsp servlets javabeans

我正在尝试使用遵循MVC模式的bean将大学名称插入数据库,但每当我点击插入按钮时,我得到了404 error。这是代码

JSP文件

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

}

}

最后控制器文件servlet处理所有的东西

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

}

1 个答案:

答案 0 :(得分:0)

问题在于控制器中您分配RequestDipatcher的行。

可能是index.html的文件路径错误。检查文件夹结构并相应地插入路径。如果index.html位于发送请求的页面的同一目录中,则在index.html的文件路径中删除前导'/'。

同时将表单操作属性URL更改为控制器注释“/ college / NewServlet”中urlPattrns中定义的相同URL模式