JSP表单未与Servlet 404连接错误



我在连接servlet时遇到了一个小问题,因此我可以将一些数据传递到mysql数据库。我在这里读了很多帖子,但在向其他成员提出建议时运气不佳。

我有一个名为"insertData.jsp"的jsp页面。在该页面上,有一个表单,其中的操作指向名为"UpdateData"的servlet。当我在网页上点击提交时,我会收到一个404错误,说明请求的资源不可用。我还更新了我的web xml文件,试图指向正确的方向。

下面是我的文件夹设置:UpdateData.java位于源软件包文件夹的控制器软件包中。该项目的名称是"RukertContainerTracker"。

这是我的jsp页面:

<%@taglib prefix="sql" uri="http://java.sun.com/jsp/jstl/sql"%>
<%@page import="java.sql.ResultSet"%>
<%@page import="java.sql.Statement"%>
<%@page import="java.sql.DriverManager"%>
<%@page import="java.sql.Connection"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>Insert Data</title>
    </head>
    <body>
        <h1>Insert Data Into Container Records</h1>
        <H1>The Rukert Terminals Container Tracker </H1>
        <form name="Insert Record" action="/UpdateData" method="Post">
            Container Number: <input type="text" name="containerNumber"> <br /> 
            Full Out: <input type="date" name="fullOut" /> <br/>
            Empty In: <input type="date" name="emptyIn" /> <br/>
            Empty Out <input type="date" name="emptyOut" /> <br/>
            Full In: <input type="date" name="fullIn" /> <br/>
            Comments: <input type="text" name="comments" /> <br/>           
            <input type="submit" value="Submit" />        
        </form>

               <div>
            <a href="javascript:history.back();">
            <span class="categoryLabelText">HOME</span>
        </a>
        </div>

    </body>
</html>

我的servlet:

    package controller;
import java.io.IOException; 
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import javax.servlet.ServletException; 
import javax.servlet.http.HttpServlet; 
import javax.servlet.http.HttpServletRequest; 
import javax.servlet.http.HttpServletResponse; 
public class UpdateData extends HttpServlet {    
    protected void processRequest(HttpServletRequest request, HttpServletResponse     response) 
            throws ServletException, IOException, SQLException { 
       //Get container data from the JSP page 
        String container=request.getParameter("containerNumber"); 
        String fullOutDate=request.getParameter("fullOut"); 
        String emptyInDate=request.getParameter("emptyIn");
        String emptyOutDate=request.getParameter("emptyOut");
        String fullInDate=request.getParameter("fullIn");
        String comments=request.getParameter("comments");
        //Print the above got values in console 
        System.out.println("The username is" +container); 
        System.out.println("nand the password is" +fullOutDate);
        String connectionparams="jdbc:mysql://localhost:3306/rukerttracker";
        String db="rukerttracker";
        String uname="root";
        String psword="Colorado1982";
        Connection connection=null;
        ResultSet rs; 
        try { 
          // Loading the available driver for a Database communication 
            Class.forName("com.mysql.jdbc.Driver"); 
//Creating a connection to the required database 
            connection = DriverManager.getConnection 
             (connectionparams, uname, psword); 
       //Add the data into the database 
            String sql = "insert into containerinventory values (?,?,?,?,?,?)";
            try (PreparedStatement prep = connection.prepareStatement(sql)) {
                prep.setString(1, container);
                prep.setString(2, fullOutDate);
                prep.setString(3, emptyInDate);
                prep.setString(4, emptyOutDate);
                prep.setString(5, fullInDate);
                prep.setString(6, comments);
                prep.executeUpdate();
            } 
           }catch(Exception E){
               System.out.println("The error is=="+E.getMessage()); 

           }

finally{
            connection.close();
        }

    }
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        String userPath = request.getServletPath();
        // if category page is requested
        if (userPath.equals("/insertData")) {
            // TODO: Implement category request

            // use RequestDispatcher to forward request internally
        String url = "/WEB-INF/view" + userPath + ".jsp";
        try {
            request.getRequestDispatcher(url).forward(request, response);
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }
    }
}

最后是我的web.xml页面:

 <servlet>
        <servlet-name>ControllerServlet</servlet-name>
        <servlet-class>controller.ControllerServlet</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>ControllerServlet</servlet-name>
        <url-pattern>/chooseLanguage</url-pattern>
        <url-pattern>/viewTracker</url-pattern>
        <url-pattern>/editTracker</url-pattern>
        <url-pattern>/addToCart</url-pattern>
        <url-pattern>/viewCompany</url-pattern>
        <url-pattern>/category</url-pattern>
        <url-pattern>/updateCart</url-pattern>
        <url-pattern>/purchase</url-pattern>
        <url-pattern>/viewCart</url-pattern>
        <url-pattern>/checkout</url-pattern>
    </servlet-mapping>
    <servlet>
        <servlet-name>UpdateData</servlet-name>
        <servlet-class>controller.UpdateData</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>UpdateData</servlet-name>
        <url-pattern>/insertData</url-pattern>
    </servlet-mapping>

我有两个servlet,我不知道这是否重要,但我无法让应用程序在控制器servlet中工作,所以我创建了更新数据servlet。

任何关于我为什么不断出现这个404错误的帮助都将不胜感激。谢谢你花时间看这个。

我认为在表单中您使用的是POST方法,而servlet没有POST方法。请检查一下。protected void doPost(HttpServlet请求,HttpServlet响应)throws Servlet异常,IOException{…}不可用。

相关内容

  • 没有找到相关文章

最新更新