java - 如何使用 servlet 将路径“/”重定向到任意 jsp 文件?
问题描述
我已将我的应用程序 ( todo.war
) 部署到 Tomcat v10.0.5 中。我预计这http://localhost:8080/todo/
会带我到/jsp/index.jsp
,但我得到的是 404 响应。
这是我的应用程序的布局:
todo.war
├── META-INF
│ └── MANIFEST.MF
├── WEB-INF
│ ├── classes
│ │ └── com
│ │ └── ciaoshen
│ │ └── sia
│ │ └── demo
│ │ └── gradle_demo
│ │ └── todo
│ │ ├── model
│ │ │ └── ToDoItem.class
│ │ └── web
│ │ └── ToDoServlet.class
│ ├── lib
│ │ └── jstl-1.2.jar
│ └── web.xml
├── css
└── jsp
├── all.jsp
└── index.jsp
所以在web.xml
文件中,servlet-mapping 模式/
被绑定到ToDoServlet
,
<servlet-mapping>
<servlet-name>ToDoServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
并发送文件ToDoServlet.java
的路径。/
/jsp/index.jsp
private String processRequest(String servletPath, HttpServletRequest request) {
if (servletPath.equals("/")) {
return "/jsp/index.jsp";
} else if (servletPath.equals("/all")) {
// ... other direction
} else {
// ... other direction
}
}
我index.jsp
的很简单,
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!doctype html>
<html>
<head><title>First JSP</title></head>
<body>
<h2>--- To Do Application ---</h2>
<h2>Please make a choice:</h2>
<h2><a href="/all"> (a)ll items</a></h2>
</body>
</html>
有人可以解释我错过了什么吗?
解决方案
推荐阅读
- typescript - TypeScript:如何委托接口方法
- android - 无法在 android oreo(API 级别 26)上以设备管理员身份激活应用
- airflow - Airflow DAG 每 30 秒自动刷新一次
- ansible - AnsibleUndefinedVariable:未找到具有此名称的变量:region1a"}
- azure - 在 Azure 数据工厂中获取历史数据复制的日期
- sql - Presto - 联合和限制
- c - 如何修复 C 中的错误“表达式不是函数定位器”?
- flutter - Dart:如何强制对变量进行字符串插值
- c# - 获取 HTTP 标头值
- google-data-studio - 在数据工作室中按两个或多个日期过滤不同的日期列