gpt4 book ai didi

java - 按下提交按钮后显示 404 错误并带有 Controller 消息如何解决

转载 作者:行者123 更新时间:2023-12-01 17:27:14 26 4
gpt4 key购买 nike

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

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

它应该注册找到的项目。但事实并非如此。我怎么解决这个问题?下面是jsp、 Controller 和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;
}
}

最佳答案

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

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

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

以下是此问题的解决方法:

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

  • 验证 web.xml 文件是否正确映射 servlet

  • 更多帮助:

    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

    关于java - 按下提交按钮后显示 404 错误并带有 Controller 消息如何解决,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61195053/

    26 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com