WebServlet 将多个路径映射到 /

发布于 2025-01-11 23:49:28 字数 1620 浏览 0 评论 0原文

我正在开发一个学校项目,其中使用 Tomcat7、Thymeleaf 和 javax.servlet,并使用 MAven 作为包管理器。

即使我不调用路径,也会调用我的文件 IndexServlet.java

package com.barnacle.travel.web;

import com.barnacle.travel.config.TemplateEngineUtil;
import java.io.IOException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.thymeleaf.TemplateEngine;
import org.thymeleaf.context.WebContext;

@WebServlet("/")
public class IndexServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String name = request.getParameter("name");
        System.out.println(request.getRequestURI());

        TemplateEngine engine = TemplateEngineUtil.getTemplateEngine(request.getServletContext());
        WebContext context = new WebContext(request, response, request.getServletContext());
        context.setVariable("recipient", name);
        engine.process("index.html", context, response.getWriter());
    }

}

tomcat7 插件的配置部分是:

        <plugin>
          <groupId>org.apache.tomcat.maven</groupId>
          <artifactId>tomcat7-maven-plugin</artifactId>
          <version>2.2</version>
          <configuration>
            <path>/</path>
          </configuration>
        </plugin>
   

即使我想要索引或 ,每个请求都会映射到 servlet / 仅被映射。应该改变什么?

I am developing a school project where I am using Tomcat7, Thymeleaf and javax.servlet with MAven as package manager.

My file IndexServlet.java is called even when I don't call the path

package com.barnacle.travel.web;

import com.barnacle.travel.config.TemplateEngineUtil;
import java.io.IOException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.thymeleaf.TemplateEngine;
import org.thymeleaf.context.WebContext;

@WebServlet("/")
public class IndexServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String name = request.getParameter("name");
        System.out.println(request.getRequestURI());

        TemplateEngine engine = TemplateEngineUtil.getTemplateEngine(request.getServletContext());
        WebContext context = new WebContext(request, response, request.getServletContext());
        context.setVariable("recipient", name);
        engine.process("index.html", context, response.getWriter());
    }

}

The configuration section for the tomcat7 plugin is:

        <plugin>
          <groupId>org.apache.tomcat.maven</groupId>
          <artifactId>tomcat7-maven-plugin</artifactId>
          <version>2.2</version>
          <configuration>
            <path>/</path>
          </configuration>
        </plugin>
   

Every request gets mapped to the the servlet even though I want the index or / to be mapped only. What should be changed?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

只为守护你 2025-01-18 23:49:28

正如 @rickz 在评论中和 更改默认主页的建议在 servlet 的根路径中使用 doGet 将代码注释编辑为:

@WebServlet("")       //empty string

解决了问题。这是由于此处提到的事实 Servlet 映射 url 模式中 / 和 /* 之间的差异 关于 /< Web servlet 中的 /code> 模式。

As suggested by @rickz in comment and from Change default homepage in root path to servlet with doGet editing the code annotation to:

@WebServlet("")       //empty string

solves the issue. This is due to the fact mentioned here Difference between / and /* in servlet mapping url pattern regarding the / pattern in web servlets.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文