WebServlet 将多个路径映射到 /
我正在开发一个学校项目,其中使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
正如 @rickz 在评论中和 更改默认主页的建议在 servlet 的根路径中使用 doGet 将代码注释编辑为:
解决了问题。这是由于此处提到的事实 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:
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.