请求的资源不可用。 JSP 的 Servlet

The requested resource is not available. Servlet to JSP

当我 运行 我在 index.jsp 上的 Eclipse 中的 Mavenproject 时,它会打开它。比起我尝试通过 /EnterAddress 打开我的 AdressServlet,我得到了错误。

Index.jsp:

<html>
<body>
<h2>Welcome</h2>
<p> 
We are going to get started with some question. 
First we will need some information about you.
</p>
<a href="/EnterAddress">Start</a>
</body>
</html>

AddressServlet:

package Servlet;

import java.io.IOException;

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

import Bean.SurveyBean;
import Service.SurveyService;
@WebServlet(value = "/EnterAddress", initParams = {
          @WebInitParam(name = "addressPage",
                value = "/WEB-INF/pages/Address.jsp"),
          @WebInitParam(name = "QuestionURL", value = "Question") })
public class AddressServlet extends HttpServlet {
    /**
     * 
     */
    private static final long serialVersionUID = 1L;

    @Override
    public void init() throws ServletException{
        super.init();
        }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        SurveyBean bean = new SurveyBean();
           HttpSession sess = req.getSession();
           sess.setAttribute("surveyBean", bean);
           resp.sendRedirect("/pages/Address.jsp");
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        HttpSession sess = req.getSession();
           SurveyBean bean = (SurveyBean) sess.getAttribute("surveyBean");
           bean.setName(req.getParameter("name"));
           bean.setStreet(req.getParameter("street"));
           bean.setNumber(req.getParameter("number"));
           bean.setZipcode(req.getParameter("zipcode"));
           bean.setCity(req.getParameter("city"));
           bean.setEmail(req.getParameter("email"));
           sess.setAttribute("surveyBean", bean);
    }





}

web.xml:

<!DOCTYPE web-app PUBLIC
 "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
 "http://java.sun.com/dtd/web-app_2_3.dtd" >

<web-app>
  <display-name>Archetype Created Web Application</display-name>
</web-app>

Address.jsp:

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>

<form method="POST">
Name: <input type="text" name="name"/>
Street: <input type="text" name="street"/>
Number: <input type="text" name="number"/>
Zipcode: <input type="text" name="zipcode"/>
email: <input type="text" name="email"/>
<input type="submit" value="OK"/>
</form>

</body>
</html>

我不知道为什么会出现错误。我在 index.jsp 上没有收到错误。 但是当我在 de jsp 上按 Start 时,我得到了错误

当我执行由 BalusC 编辑的内容时,它仍然不起作用我不知道我做错了什么或如何解决它

首先,这里有几点要记住:
WEB-INF 下的任何 jsp 文件都无法通过在浏览器上键入类似 localhost:8080/WEB-INF/foo.jsp 的内容直接访问。
servlet 可以访问它们。

这是您需要做的事情

  • 用以下内容替换您的 web.xml Doctype 标签:

    <?xml version="1.0" encoding="UTF-8"?>
    <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
         version="3.1">
    

    以上更改确保注释适用于您的 servlet。这可能不是一个直接的问题,因为我不知道你是否可以访问 servlet,返回 404 也可能是由于找不到 jsp 造成的。

  • 使用response.sendRedirect()等同于在浏览器中输入jspurl,因为资源在WEB-INF下,所以无法获取资源。此外,路径 WEB-INF 甚至不包含在您的 url.
    中 您需要像这样转发到 servlet 中的 jsp:

    RequestDispatcher dispatcher = getServletContext().getRequestDispatcher("/WEB-INF/pages/Address.jsp");
    dispatcher.forward(request, response);