如何在按下提交按钮后解决,并显示带有控制器消息的404错误

问题描述 投票:0回答:1

我目前正在使用Java和oracle数据库(MVC)做我有关失物招领处系统的最后一年的项目。

我的问题是我提交表单后页面将显示此the message says it redirect to the FoundRegisterController

它应该注册找到的项目。但事实并非如此。我怎么解决这个问题?下面是jsp,控制器和DAO的代码

这是registerFoundItem.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!doctype html>
<html class="no-js" lang="en">

<head>
<jsp:include page="css.jsp"></jsp:include>
</head>

<body>
    <jsp:include page="header.jsp" />

    <!-- tabs & register form Start -->
    <div class="basic-form-area mg-b-15">
        <div class="container-fluid">
            <div class="row">
                <div class="col-lg-12 col-md-12 col-sm-12 col-xs-12">
                    <div class="sparkline8-list mt-b-30">
                        <div class="sparkline8-graph">
                            <div class="basic-login-form-ad">
                                <div class="row">
                                    <div class="col-lg12 col-md-12 col-sm-12 col-xs-12">
                                        <h2>
                                            <center>Register New Found Item</center>
                                        </h2>
                                        <br> <br>
                                    </div>
                                </div>
                                <%
                                    String userEmail = (String) session.getAttribute("currentSessionUser");
                                %>
                                <%
                                    String userNoPhone = (String) session.getAttribute("currentSessionUserNoPhone");
                                %>
                                <%
                                    String userName = (String) session.getAttribute("currentSessionUserName");
                                %>
                                <div class="row">
                                    <form action="FoundRegisterController" method="post">
                                        <div class="col-lg-4 col-md-4 col-sm-4 col-xs-12">
                                            <div class="basic-login-inner">
                                                <h4>Reporter Details:</h4>

                                                <div class="form-group-inner">
                                                    <label>Post by:</label> <input type="text"
                                                        class="form-control" name="UserName"
                                                        value="<c:out value="<%=userName%>" />" disabled />
                                                </div>
                                                <div class="form-group-inner">
                                                    <label>Contact Number: </label><input type="text"
                                                        class="form-control" name="UserNoPhone"
                                                        value="<c:out value="<%=userNoPhone%>" />" disabled />
                                                </div>
                                                <div class="form-group-inner">
                                                    <label>Email: </label><input type="text"
                                                        class="form-control" name="UserNoPhone"
                                                        value="<c:out value="<%=userEmail%>" />" disabled />
                                                </div>
                                                <br>
                                            </div>
                                        </div>

                                        <div class="col-lg-8 col-md-8 col-sm-8 col-xs-12">
                                        <h4>Found Item Details :</h4></div>
                                        <div class="col-lg-4 col-md-4 col-sm-4 col-xs-12">
                                            <label>Date:</label> <input class="form-control"
                                                name="FItemDate" type="date" required />
                                        </div>
                                        <div class="col-lg-4 col-md-4 col-sm-4 col-xs-12">
                                            <label>Time:</label> <input class="form-control"
                                                name="FItemTime" type="time"
                                                required />
                                        </div>
                                        <div class="col-lg-8 col-md-8 col-sm-8 col-xs-12">
                                            <div class="form-group-inner">
                                                <label><br>Found Item Name:</label> <input type="text"
                                                    class="form-control" name="FItemName"
                                                    placeholder="eg: iPhone 8 Plus | ASUS | GoPro 5 Black"
                                                    title="Please enter the valid name. Do not use shortform"
                                                    required />
                                            </div>
                                            <div class="form-group-inner">
                                                <label>Found Item Category:</label>
                                                <div class="form-select-list">
                                                    <select class="form-control custom-select-value"
                                                        name="account" name="FItemCategory">
                                                        <option disabled selected value>-- Select
                                                            Category --</option>
                                                        <option value="FoundItem">Mobile Phone</option>
                                                        <option value="FoundItem">Laptop/Notebook</option>
                                                        <option value="FoundItem">Keys</option>
                                                        <option value="FoundItem">Cards</option>
                                                        <option value="FoundItem">UiTM Matrics Card</option>
                                                        <option value="FoundItem">Other..</option>
                                                    </select>
                                                </div>
                                            </div>
                                            <div class="form-group-inner">
                                                <label>Location:</label> <input class="form-control"
                                                    name="FItemLocation"
                                                    placeholder="eg: Blok Kuliah 17 | JMK 8 | Cafe Kuliah"
                                                    title="Please enter the valid name. Do not use shortform"
                                                    required />
                                            </div>
                                            <div class="form-group-inner">
                                                <label>Description: </label>
                                                <div class="form-group edit-ta-resize res-mg-t-15">
                                                    <textarea name="description" name="FItemDescription" placeholder="eg: Barang dijumpai di bawah meja/atas lantai/depan pintu/atas meja pensyarah/etc.. anggaran waktu dijumpai 2ptg-6ptg"></textarea>
                                                    </div>
                                            </div>

                                            <div class="login-btn-inner">
                                                <div class="inline-remember-me">
                                                    <button
                                                        class="btn btn-sm btn-primary pull-right login-submit-cs"
                                                        type="submit" name="action">
                                                        <b>Register</b>
                                                    </button>
                                                </div>
                                            </div>
                                        </div>
                                    </form>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
        <br>
    </div>
    <!-- tabs & Register form End-->


    <!-- JS -->
    <jsp:include page="js.jsp"></jsp:include>

</body>

</html>

这是我的FoundRegisterController.java

package lofo.controller;
import java.io.IOException;
import java.io.PrintWriter;
import java.security.NoSuchAlgorithmException;

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;

import lofo.dao.foundDAO;
import lofo.model.FoundItemBean;

/**
 * Servlet implementation class RegisterController
 */
@WebServlet("/FoundRegisterController")
public class FoundRegisterController extends HttpServlet {
    private static final long serialVersionUID = 1L;
    private String VIEW ="/lofo/viewFound.jsp";
    private foundDAO dao;
    String forward=""; 
    String action="";
    /**
     * @see HttpServlet#HttpServlet()
     */
    public FoundRegisterController() {
        super();
        // TODO Auto-generated constructor stub
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String action = request.getParameter("action");

        if(action.equalsIgnoreCase("viewFound")) {
            forward = VIEW;
            String UserEmail = request.getParameter("UserEmail");
            FoundItemBean found = dao.getUserByEmail(UserEmail);
            request.setAttribute("found", found);
        }

        RequestDispatcher view = request.getRequestDispatcher(forward);
        view.forward(request, response);
    }




    /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        action = request.getParameter("action");
        try {

            String FItemID = request.getParameter("FItemID");
            String UserEmail = request.getParameter("UserEmail");
            String FItemName = request.getParameter("FItemName");
            String FItemCategory = request.getParameter("FItemCategory");
            String FItemDate = request.getParameter("FItemDate");
            String FItemTime = request.getParameter("FItemTime");
            String FItemLocation = request.getParameter("FItemLocation");
            String FItemDescription = request.getParameter("FItemDescription");

            FoundItemBean found = new FoundItemBean();

            found.setFItemID(FItemID);
            found.setUserEmail(UserEmail);
            found.setFItemName(FItemName);
            found.setFItemCategory(FItemCategory);
            found.setFItemDate(FItemDate);
            found.setFItemTime(FItemTime);
            found.setFItemLocation(FItemLocation);
            found.setFItemDescription(FItemDescription);


            dao = new foundDAO();
            found = foundDAO.getUser(found);

            try {
                dao.FoundAdd(found);
            } catch (NoSuchAlgorithmException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            response.setContentType("text/html");
            PrintWriter pw = response.getWriter();
            pw.println("<script>");
            pw.println("alert('The item post has been created');");
            pw.println("window.location.href='FoundController?action=listFoundItem';");
            pw.println("</script>");
        }

        catch (Throwable ex) {
            System.out.println(ex);
        }
    }
}

这是我的FoundController.java

package lofo.controller;
import java.io.IOException;
import java.io.PrintWriter;
import java.security.NoSuchAlgorithmException;
import java.util.List;

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;
import javax.servlet.http.HttpSession;

import lofo.dao.foundDAO;
import lofo.model.FoundItemBean;

/**
 * Servlet implementation class UserController
 */
@WebServlet("/FoundController")
public class FoundController extends HttpServlet {
    private static final long serialVersionUID = 1L;
    private String VIEW ="viewFound.jsp";
    private String VIEWALL ="listFoundItem.jsp";
    private static String UPDATE = "updateFound.jsp";
    private static String DELETE = "deleteFound.jsp";
    private static String SEARCH = "registerFoundItem.jsp";

    String forward;
    private foundDAO dao;
    /**
     * @see HttpServlet#HttpServlet()
     */
    public FoundController() {
        super();
        dao = new foundDAO();
        // TODO Auto-generated constructor stub
    }

    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String action = request.getParameter("action");

        if(action.equalsIgnoreCase("viewFound")) {
            forward = VIEW;
            String UserEmail = request.getParameter("UserEmail");
            FoundItemBean found = foundDAO.getUserByEmail(UserEmail);
            List<FoundItemBean> founds = foundDAO.getAllFound();
            request.setAttribute("founds", founds );
            request.setAttribute("found", found);
        }
        else if (action.equalsIgnoreCase("listFoundItem")) {
            forward = VIEWALL;
            request.setAttribute("founds", foundDAO.getAllFound()); 

        }
        else if (action.equalsIgnoreCase("updateFound")){
            forward = UPDATE;
            String UserEmail = request.getParameter("UserEmail");
            FoundItemBean found = foundDAO.getUserByEmail(UserEmail);
            List<FoundItemBean> founds = foundDAO.getAllFound();
            request.setAttribute("founds", founds);
            request.setAttribute("found", found);
        }
        else if (action.equalsIgnoreCase("search")){
            forward = SEARCH;
            List<FoundItemBean> found = foundDAO.getAllFound();
            request.setAttribute("founds", found);
        }
        else if (action.equalsIgnoreCase("deleteFound")){
            forward = DELETE;
            String UserEmail = request.getParameter("UserEmail");
            FoundItemBean found = foundDAO.getUserByEmail(UserEmail);
            request.setAttribute("found", found);
        }
        RequestDispatcher view = request.getRequestDispatcher(forward);
        view.forward(request, response);
    }

    /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        String action = request.getParameter("action");
        if(action.equalsIgnoreCase("Submit")) {

            String UserEmail = request.getParameter("UserEmail");
            String FItemName = request.getParameter("FItemName");
            String FItemCategory = request.getParameter("FItemCategory");
            String FItemDate = request.getParameter("FItemDate");
            String FItemTime = request.getParameter("FItemTime");
            String FItemLocation = request.getParameter("FItemLocation");
            String FItemDescription = request.getParameter("FItemDescription");

            FoundItemBean found = new FoundItemBean();

            found.setUserEmail(UserEmail);
            found.setFItemName(FItemName);
            found.setFItemCategory(FItemCategory);
            found.setFItemDate(FItemDate);
            found.setFItemTime(FItemTime);
            found.setFItemLocation(FItemLocation);
            found.setFItemDescription(FItemDescription);

            dao = new foundDAO();
            try {
                dao.updateFound(found);
                /*forward = VIEW;
            user = UserDAO.getUserByEmail(UserEmail);
            request.setAttribute("user", user); */
                response.setContentType("text/html");
                PrintWriter pw = response.getWriter();
                pw.println("<script>");
                pw.println("alert('The account is updated');");
                pw.println("window.location.href='/lofo/FoundController?action=viewFound&UserEmail="+ UserEmail +"';");
                pw.println("</script>");

            } catch (NoSuchAlgorithmException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            //}
            //else if(action.equalsIgnoreCase("Delete")) {
            //String UserEmail = request.getParameter("UserEmail");
            // = new foundDAO();
            //dao.deleteUser(UserEmail);
            //response.setContentType("text/html");
            //PrintWriter pw = response.getWriter();
            //pw.println("<script>");
            //pw.println("alert('The account has been deleted');");
            //pw.println("window.location.href='/Inventory/UserController?action=listAll';");
            //pw.println("</script>");
            //}
        }
    }
}

这是我的foundDAO.java

package lofo.dao;
import java.sql.Statement;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import lofo.model.FoundItemBean;
import lofo.model.UsersBean;
import lofo.connection.ConnectionManager;

public class foundDAO {
    static Connection currentCon = null;
    static ResultSet rs = null;
    static PreparedStatement ps = null;
    static Statement stmt = null;
    static String FItemID, UserEmail, FItemName, FItemCategory, FItemDate, FItemTime, FItemLocation, FItemDescription;

     public static FoundItemBean getUser(FoundItemBean bean)  {

         UserEmail = bean.getUserEmail();

         String searchQuery = "select * from founditem where useremail='" + UserEmail + "'";

         try {
             currentCon = ConnectionManager.getConnection();
             stmt = currentCon.createStatement();
             rs = stmt.executeQuery(searchQuery);
             boolean more = rs.next();

             // if user exists set the isValid variable to true
             if (more) {
                String UserEmail = rs.getString("UserEmail");

                 bean.setUserEmail(UserEmail);
                 bean.setValid(true);
                }

             else if (!more) {
                System.out.println("Sorry");
                bean.setValid(false);
             }

         }

         catch (Exception ex) {
             System.out.println("Log In failed: An Exception has occurred! " + ex);
         }

         finally {
             if (rs != null) {
                 try {
                     rs.close();
                 } catch (Exception e) {
                 }
                 rs = null;
             }

             if (stmt != null) {
                 try {
                     stmt.close();
                 } catch (Exception e) {
                 }
                 stmt = null;
             }

             if (currentCon != null) {
                 try {
                     currentCon.close();
                 } catch (Exception e) {
                 }

                 currentCon = null;
             }
         }

         return bean;
     }

    //get user by email
    public static FoundItemBean getUserByEmail(String UserEmail) {
        FoundItemBean found = new FoundItemBean();
        try {
            currentCon = ConnectionManager.getConnection();
            ps=currentCon.prepareStatement("select * from founditem where useremail=?");

            ps.setString(1, UserEmail);

            ResultSet rs = ps.executeQuery();

            if (rs.next()) {

                found.setFItemID(rs.getString("FItemID"));
                found.setUserEmail(rs.getString("UserEmail"));
                found.setFItemName(rs.getString("FItemName"));
                found.setFItemCategory(rs.getString("FItemCategory"));
                found.setFItemDate(rs.getString("FItemDate"));
                found.setFItemTime(rs.getString("FItemTime"));
                found.setFItemLocation(rs.getString("FItemLocation"));
                found.setFItemDescription(rs.getString("FItemDescription"));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }

        return found;
    }

    //create Found Item
    public void FoundAdd (FoundItemBean bean) throws NoSuchAlgorithmException {


        FItemID = bean.getFItemID();
        UserEmail = bean.getUserEmail();
        FItemName = bean.getFItemName();
        FItemCategory = bean.getFItemCategory();
        FItemDate = bean.getFItemDate();
        FItemTime = bean.getFItemTime();
        FItemLocation = bean.getFItemLocation();
        FItemDescription = bean.getFItemDescription();

        try {
            currentCon = ConnectionManager.getConnection(); 
            ps=currentCon.prepareStatement("insert into founditem(FItemID, UserEmail, FItemName, FItemCategory, FItemDate, FItemTime, FItemLocation, FItemDescription) values (FOUNDITEM_SEQ.nextval,?,?,?,?,?,?,?)");

            ps.setString(1,UserEmail);
            ps.setString(2,FItemName);
            ps.setString(3,FItemCategory);
            ps.setString(4,FItemDate);
            ps.setString(5,FItemTime);
            ps.setString(6,FItemLocation);
            ps.setString(7,FItemDescription);
            ps.executeUpdate();

            System.out.println("Your FItemName is " + FItemName);

        }

        catch (Exception ex) {
            System.out.println("failed: An Exception has occured!" + ex);
        }

        finally {
            if (ps != null) {
                try {
                    ps.close();
                } catch (Exception e) {
                }
                ps = null;
            }

            if (currentCon != null) {
                try {
                    currentCon.close();
                } catch (Exception e) {
                }
                currentCon = null;

            }
        }
    }

    //update account
    public void updateFound(FoundItemBean bean) throws NoSuchAlgorithmException {

        FItemName = bean.getFItemName();
        FItemCategory = bean.getFItemCategory();
        FItemDate = bean.getFItemDate();
        FItemTime = bean.getFItemTime();
        FItemLocation = bean.getFItemLocation();
        FItemDescription = bean.getFItemDescription();

        String searchQuery = "";


        searchQuery = "UPDATE founditem SET FItemName ='"+ FItemName +"', FItemCategory='" + FItemCategory + "', FItemDate='" + FItemDate + "', FItemTime='" + FItemTime + "', FItemLocation='" + FItemLocation + "', FItemDescription='" + FItemDescription + "' WHERE UserEmail= '" + UserEmail + "'";       
        System.out.println(searchQuery);


        try {

            currentCon = ConnectionManager.getConnection();
            stmt = currentCon.createStatement();
            stmt.executeUpdate(searchQuery);
            System.out.println(searchQuery);

        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    //getAllFound for list table
    public static List<FoundItemBean> getAllFound() {
        List<FoundItemBean> founds = new ArrayList<FoundItemBean>();

        try {
            currentCon = ConnectionManager.getConnection();
            stmt = currentCon.createStatement();

            String q = "select * from founditem";
            ResultSet rs = stmt.executeQuery(q);

            while (rs.next()) {
                FoundItemBean found = new FoundItemBean();

                found.setUserEmail(rs.getString("UserEmail"));
                found.setFItemName(rs.getString("FItemName"));
                found.setFItemCategory(rs.getString("FItemCategory"));
                found.setFItemDate(rs.getString("FItemDate"));
                found.setFItemTime(rs.getString("FItemTime"));
                found.setFItemLocation(rs.getString("FItemLocation"));
                found.setFItemDescription(rs.getString("FItemDescription"));
                founds.add(found);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }

        return founds;
    }
}
java database oracle servlets controller
1个回答
0
投票

附图中显示的错误消息是-

“原始服务器未找到目标资源的当前表示...”

此错误通常不是由于代码中的问题引起的。该错误是由于IDE问题(与Eclipse通用)有关,或者是由于在Web服务器上部署应用程序的方式引起的。

这里是解决此问题的方法:

  1. 备份项目文件。
  2. 从eclipse删除项目。
  3. 确保所有HTML模板文件中的字符编码都设置为'utf-8'] >>>
    <html lang="en">
        <head>
            <meta charset="utf-8"/>
        </head>
    
  1. 重新启动日食。
  2. 将项目重新导入到Eclipse中。

  3. 验证web.xml文件以正确映射servlet

  4. 更多帮助:

https://www.codejava.net/java-ee/servlet/solved-tomcat-error-http-status-404-not-found

The origin server did not find a current representation for the target resource or is not ... error when running jsp page

Tomcat 404 error: The origin server did not find a current representation for the target resource or is not willing to disclose that one exists

© www.soinside.com 2019 - 2024. All rights reserved.