【问题标题】:tomcat is unable to display the JSP, error : The requested resource is not available [duplicate]tomcat 无法显示 JSP,错误:请求的资源不可用 [重复]
【发布时间】:2016-04-14 06:01:35
【问题描述】:

我创建了一个示例项目来测试jspProject looks like this

我正在使用tomcat 9Java SE 8_73。该项目的 Web 模块版本为 3.1,但未生成 web.xml

java代码是这样的

package pac;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class JSPProject
 */
@WebServlet("/jspproject")
public class JSPProject extends HttpServlet {
    private static final long serialVersionUID = 1L;

    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        PrintWriter cut = response.getWriter();
        cut.println("hello world");
    }
}

但在服务器上运行JSPProject.java 时,会显示以下错误消息。

HTTP Status 404 - /DynamicJSP/jspproject
type Status report`
message /DynamicJSP/jspproject
description The requested resource is not available.
The requested resource is not available.

【问题讨论】:

  • 代替service方法使用protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {}
  • 不工作。我遇到了同样的错误
  • 尝试清理项目并重启tomcat。
  • JSP 是文本文件,不写入响应。 Tomcat 中已经有一个 Jsp servlet。只需将一个文本文件命名为 abc.jsp 并将其放入 webcontent(WEB-INF 上方的目录)。 404 表示您传递的网址,无法提供。所以要么你的应用程序没有被称为“DynamicJSP”,要么你的 serlvet 没有被初始化。检查组件扫描,或添加 web.xml 并设置 metadata-complete=false。
  • 你的问题与JSP无关。它只是一个普通的 webservlet。

标签: java tomcat servlets jakarta-ee


【解决方案1】:
    if you have changed your method to doGet and after that also it doesn't works then you need to make changes in your web.xml file. Pls look at below example :

<servlet>
<servlet-name>HelloServlet</servlet-name> (this is any name of your choice)
<servlet-class>examples.Hello</servlet-class>(This is Java class implements the servlet)
</servlet>
<servlet-mapping>
<servlet-name>HelloServlet</servlet-name>(this name should exactly the same name as above , used for mapping)
<url-pattern>/hello</url-pattern> (This is URL pattern that invokes the servlet in your browser)
</servlet-mapping>

【讨论】:

    【解决方案2】:

    您应该从您扩展的HttpServlet 类中重写doGet 方法,如下所示:

      @Override
      protected void doGet(HttpServletRequest req, HttpServletResponse resp)
              throws ServletException, IOException {
        try (ServletOutputStream cut = resp.getOutputStream()) {
          cut.println("hello world");
          cut.flush();
        }
    

    【讨论】:

      猜你喜欢
      • 2015-12-09
      • 2015-03-16
      • 1970-01-01
      • 2023-03-26
      • 2013-01-06
      • 1970-01-01
      • 1970-01-01
      • 2016-09-21
      • 2015-10-30
      相关资源
      最近更新 更多