运行 使用 tomcat 服务器 8.5 的 Intellij Community Edition 中的 servlet 应用程序出现问题
Trouble running servlet application in Intellij Community Edition with tomcat server 8.5
// 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。我做错了什么?
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);
}
}
试试看,如果它适合你,请告诉我。请记住在尝试之前重新启动您的服务器。
导致问题的是 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。谢谢大家的回复。
// 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。我做错了什么?
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);
}
}
试试看,如果它适合你,请告诉我。请记住在尝试之前重新启动您的服务器。
导致问题的是 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。谢谢大家的回复。