【问题标题】:Trouble running servlet application in Intellij Community Edition with tomcat server 8.5使用 tomcat 服务器 8.5 在 Intellij Community Edition 中运行 servlet 应用程序时出现问题
【发布时间】:2018-02-09 18:07:13
【问题描述】:
// web.xml    
<?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-name>welcome</servlet-name>
            <servlet-class>com.rippleworks.WelcomeServlet</servlet-class>
        </servlet>

        <servlet-mapping>
            <servlet-name>welcome</servlet-name>
            <url-pattern>/welcome</url-pattern>
        </servlet-mapping>
    </web-app>

// WelcomeServlet.java

package com.rippleworks;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintStream;

public class WelcomeServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        super.doGet(req, resp);
        PrintStream out = new PrintStream(resp.getOutputStream());
        out.println("Hello students!");
    }
}

// index.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
    <h1>HEllo world!</h1>
</body>
</html>

我已将 Intellij 配置为使用我的本地 tomcat 安装。当我部署我的项目时,似乎只有 index.jsp 可以工作。对 /welcome 路径的 http 请求给出 405。我做错了什么?

【问题讨论】:

  • 当您访问您的 JSP 时,您必须使用上下文吗?也就是说,通常您会访问像 http://localhost:8080/something/ 这样的 URL,其中 something 是 Web 应用程序上下文。您需要使用 servlet 进入相同的上下文 - 即 http://localhost:8080/something/welcome。您是否考虑过使用注释?在这种情况下,您甚至不需要 web.xml。
  • 在 intellij 中不是可以配置的吗?我已将我的应用程序根设置为 /。

标签: jsp servlets jakarta-ee intellij-idea tomcat8


【解决方案1】:

servlet 映射似乎是错误的。请执行以下操作:

  <servlet>
    <servlet-name>WelcomeServlet</servlet-name>
    <servlet-class>com.rippleworks.WelcomeServlet</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>WelcomeServlet</servlet-name>
    <url-pattern>/welcome</url-pattern>
  </servlet-mapping>

还有你的 servlet:

public class WelcomeServlet extends HttpServlet {

    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

    //do whatever you want here


    //forward request to index
    RequestDispatcher rd=req.getRequestDispatcher("index.jsp");    
    rd.forward(req,resp);  

    }
}

尝试一下,让我知道它是否适合您。请记住在尝试之前重新启动您的服务器。

【讨论】:

    【解决方案2】:

    这是导致问题的 super.doGet(..)。从源代码 HttpServlet,默认实现是这样的,

    public void doGet(..) {
            String protocol = req.getProtocol();
            String msg = lStrings.getString("http.method_get_not_supported");
            if (protocol.endsWith("1.1")) {
                resp.sendError(405, msg);
            } else {
                resp.sendError(400, msg);
            }
    }
    

    它总是会以错误响应。删除了对 super 方法的调用,现在可以正常工作了。

    PS。感谢大家的回复。

    【讨论】:

      猜你喜欢
      • 2017-10-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-04-20
      • 2010-12-23
      • 1970-01-01
      • 2016-12-28
      相关资源
      最近更新 更多