我遇到了一个奇怪的问题,我的servlet doGet方法会运行三次。
我在Chrome和Firefox中对此进行了测试,结果相同。我还在另一台计算机上部署了该项目,并使用apache服务器查看该网站是否存在但是没有更改。
我在doGet中使用了一个开关,并在getParameter请求中找到了要运行的正确方法。我已将开关缩短为两种情况。但每次行动都是"创造"它将运行该方法三次。
我知道这是非常狭隘的,我希望我能够提供更多信息,但我不知道在哪里挖掘。
编辑:问题已经找到。 request.getRequestDispatcher正在导致它。非常感谢任何有关此问题的帮助。protected void doGet(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
try {
String action = request.getParameter("action");
// To see that it runs 3 times.
System.out.println(action);
switch (action) {
case "create":
this.create(request, response);
break;
default:
this.greetGuest(request, response);
break;
}
}
protected void createEvent(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
request.setAttribute("AbstractUser", SessionUser);
// Here's the problem. This part will cause the doGet to be called multiply times.
request.getRequestDispatcher("/WEB-INF/jsp/view/Organisation/create.jsp").forward(
request, response);
}
protected void doPost(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
this.doGet(request, response);
}
//html
<a href="eventHandler?action=create">Create</a>
//Configuration
@WebServlet(name = "eventHandler", urlPatterns = { "/eventHandler" }, loadOnStartup = 1)
//web.xml
<jsp-config>
<jsp-property-group>
<url-pattern>*.jsp</url-pattern>
<url-pattern>*.jspf</url-pattern>
<page-encoding>UTF-8</page-encoding>
<scripting-invalid>false</scripting-invalid>
<include-prelude>/WEB-INF/jsp/base.jspf</include-prelude>
<trim-directive-whitespaces>true</trim-directive-whitespaces>
<default-content-type>text/html</default-content-type>
</jsp-property-group>
</jsp-config>