【问题标题】:Eclipse 404 Requested resource not availableEclipse 404 请求的资源不可用
【发布时间】:2018-11-01 01:00:36
【问题描述】:

我已经为这个问题苦苦挣扎了几个小时,但我似乎无法弄清楚出了什么问题。做了很多谷歌搜索和挖掘工作,但似乎没有任何效果。我正在使用 Eclipse 开发 Web 应用程序,但在访问资源时出现以下错误:

HTTP Status 404 - /EditForm
type Status report

message /EditForm

description The requested resource is not available.

一些背景信息:我正在使用 JSP 提交一个表单,该表单旨在使用用 java 编写的 servlet 来处理信息。 它被称为 editProfile.jsp,这里是表单提交信息的部分。

  <form CLASS="form-horizontal" METHOD=POST ACTION="/EditForm">

    <div class="form-group">
            <label class="control-label col-sm-2"><b>New Address</b></label>
            <INPUT TYPE=TEXT PLACEHOLDER="Type new address here." NAME=address SIZE=20><BR>
    </div>
    <div class="submit_btn col-sm-2"><INPUT TYPE=SUBMIT></div> 

  <form>

Servlet如下:

 package user;

import java.io.IOException;
import java.sql.*;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.*;
import database.*;


@WebServlet("/EditForm")
public class EditForm extends HttpServlet{


/**
 * 
 */
private static final long serialVersionUID = 102831973239L;
@Override
public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException,ServletException {

    HttpSession session = request.getSession();
    String currForm =(String) request.getParameter("currForm");
    String user = (String) session.getAttribute("username");



    if(currForm.equals("address")) {

        String sqlStmt = "UPDATE db.User SET address = '"+ currForm+"' WHERE username = '"+user+"';";
        doSQL(sqlStmt);
        response.sendRedirect("profile.jsp");

    }
    else if(currForm.equals("phone")) {

        String sqlStmt = "UPDATE db.User SET phone = '"+ currForm+"' WHERE username = '"+user+"';";
        doSQL(sqlStmt);
        response.sendRedirect("profile.jsp");

    }

    else if(currForm.equals("email")) {

        String sqlStmt = "UPDATE db.User SET email = '"+ currForm+"' WHERE username = '"+user+"';";
        doSQL(sqlStmt);
        response.sendRedirect("profile.jsp");

    }

}

public void doSQL(String stmt) {

    try {
        DatabaseOperation op = new DatabaseOperation();
        op.connect();
        op.executeQuery(stmt);

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

}

}

我的 WEB-INF 文件夹中的 Web.xml 是:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
 xmlns="http://xmlns.jcp.org/xml/ns/javaee" 
 xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee 
 http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" 
 version="3.1">
 <display-name>Website</display-name>
 <welcome-file-list>
     <welcome-file>index.html</welcome-file>
     <welcome-file>index.htm</welcome-file>
     <welcome-file>index.jsp</welcome-file>
     <welcome-file>default.html</welcome-file>
     <welcome-file>default.htm</welcome-file>
     <welcome-file>default.jsp</welcome-file>
 </welcome-file-list>

<servlet>
  <servlet-name>EditForm</servlet-name>
  <servlet-class>user.EditForm</servlet-class>  
</servlet>

</web-app>

我的应用目录结构:

   --Website
        |--Deployment Descriptor: Website
        |--JAX-WS Web Services
        |--Java Resources
        |      |--src
        |      |   |--user(package)
        |      |         |--EditForm.java
        |      |
        |      |--Libraries   
        |
        |--JavaScript Resources
        |--Referenced Libraries
        |--build
        |--WebContent
        |      |--META-INF
               |--WEB-INF
               |    |--lib
               |    |--web.xml
               |
               |--editProfile.jsp

如果有人能指出我正确的方向并告诉我我做错了什么,我的资源无法访问,我将不胜感激。谢谢。

【问题讨论】:

    标签: java eclipse jsp http-status-code-404


    【解决方案1】:

    一个 Servlet 容器可以托管多个 Web 应用程序,因此您映射的 URI“/EditForm”只会匹配相对于您的 Web 应用程序在服务器上部署的上下文路径的请求,而不是“/” EditForm”绝对。 pageContext.getServletContext().getContextPath() 可以为您提供该路径,如果您想使用 JSP 表达式将其添加到 form action 值之前(假设您不想对其进行硬编码)。

    如果您使用javax.servlet.annotation.WebServlet 注释,您也不需要在部署描述符中声明 Servlet,至少除非您打算在部署描述符中使用它做其他事情。

    【讨论】:

      【解决方案2】:

      我认为您没有正确设置 servlet 的路径。而不是 action=/EditForm 尝试使用 action=../EditForm

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-09-06
        • 2013-02-16
        • 2021-11-25
        相关资源
        最近更新 更多