【发布时间】: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