首页 > 解决方案 > 如何使用 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
    }
} 

404在浏览器中找不到资源, 在此处输入图像描述

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>

有人可以解释我错过了什么吗?

标签: javajsptomcatservlets

解决方案


推荐阅读