【问题标题】:The requested resource is not available. Servlet to JSP [duplicate]请求的资源不可用。 Servlet 到 JSP [重复]
【发布时间】:2016-09-21 22:46:43
【问题描述】:

当我在 Eclipse 中的 index.jsp 上运行我的 Maven 项目时,它会打开它。比我试图在这里打开我的 AdressServlet 槽 /EnterAddress 我得到错误。

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>

地址.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 编辑的事情时,它仍然不起作用,我不知道我做错了什么或如何解决它

【问题讨论】:

  • 你试过value = "/pages/Address.jsp")吗?
  • 是的,已经试过了
  • @BalusC 当我在您发布的链接上执行操作时,我仍然无法打开 myn /EnterAddress
  • 修好了,但我不知道我是怎么做的或做了什么

标签: jsp servlets http-status-code-404


【解决方案1】:

首先,请记住以下几点:
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() 和在浏览器上输入jsp url 一样,因为资源在WEB-INF 下,所以无法获取资源。此外,路径 WEB-INF 甚至不包含在您的网址中。
    您需要像这样转发到 servlet 中的 jsp:

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

【讨论】:

  • 我知道你不能通过键入它来访问它,但是当我按下链接时,或者当我在我的 Eclipse 中运行地址 jsp 时。当我替换 web.xml 时,我什至无法运行我的 index.jsp,而不是当我放回正常位置时,我仍然可以运行它。
  • 在“myown.jsp”之前添加“/WEB-INF/something/”也对我有用。
猜你喜欢
  • 2013-01-06
  • 2016-03-14
  • 1970-01-01
  • 2015-05-13
  • 1970-01-01
  • 2015-12-09
  • 2021-01-06
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多