服务器遇到意外情况,无法完成请求。 HTTP 500
The server encountered an unexpected condition that prevented it from fulfilling the request. HTTP 500
我想为多个页面使用一个 servlet。当我尝试 运行 项目时,由于请求已满而发生错误。已经为 URL 模式尝试了很多选项,但没有结果
现在,我只看到一种方法。仅使用 post 方法并使用空的 url-pattern 但我不确定这是正确的选择 :D
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
version="4.0">
<display-name>Archetype Created Web Application</display-name>
<session-config>
<session-timeout>10</session-timeout>
</session-config>
<filter>
<filter-name>InitialFilter</filter-name>
<filter-class>eshop.filter.InitialFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
</filter>
<filter>
<filter-name>SessionFilter</filter-name>
<filter-class>eshop.filter.SessionFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>InitialFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<filter-mapping>
<filter-name>SessionFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<servlet>
<servlet-name>mainServlet</servlet-name>
<servlet-class>eshop.controller.MainServlet</servlet-class>
<load-on-startup>0</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>mainServlet</servlet-name>
<url-pattern>*.jsp</url-pattern>
</servlet-mapping>
<error-page>
<location>/WEB-INF/views/error.jsp</location>
</error-page>
</web-app>
public class IndexCommand implements Command {
@Override
public void execute(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException, MessagingException, ServiceException, SQLException {
if (request.getSession().getAttribute("role")=="admin")
{
Command command=new AccountCommand();
command.execute(request,response);
}
System.out.println(request.getRequestURI());
request.getRequestDispatcher("/WEB-INF/views/startPage.jsp").forward(request,response);
}
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
Command command = CommandFactory.createCommand(request);
try {
command.execute(request, response);
} catch (MessagingException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
} catch (ServiceException e) {
e.printStackTrace();
}
}
Type Exception Report
Message Servlet execution threw an exception
Description The server encountered an unexpected condition that prevented it from fulfilling the request.
Exception
javax.servlet.ServletException: Servlet execution threw an exception
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
eshop.command.IndexCommand.execute(IndexCommand.java:34)
eshop.controller.MainServlet.doGet(MainServlet.java:62)
javax.servlet.http.HttpServlet.service(HttpServlet.java:635)
javax.servlet.http.HttpServlet.service(HttpServlet.java:742)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
eshop.command.IndexCommand.execute(IndexCommand.java:34)
eshop.controller.MainServlet.doGet(MainServlet.java:62)
javax.servlet.http.HttpServlet.service(HttpServlet.java:635)
javax.servlet.http.HttpServlet.service(HttpServlet.java:742)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
Root Cause
java.lang.WhosebugError
eshop.command.IndexCommand.execute(IndexCommand.java:34)
eshop.controller.MainServlet.doGet(MainServlet.java:62)
javax.servlet.http.HttpServlet.service(HttpServlet.java:635)
javax.servlet.http.HttpServlet.service(HttpServlet.java:742)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
如何配置 web.xml 将 servlet 映射到 Java 中的多个 url 模式,问题出在哪里?
您可以为 servlet 放置任意数量的映射,但您将它们作为单独的 serrvlet-mapping 元素放置。
<servlet-mapping>
<servlet-name>mainServlet</servlet-name>
<url-pattern>*.jsp</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>mainServlet</servlet-name>
<url-pattern>*.jsp</url-pattern>
</servlet-mapping>
关于第二个问题,你会得到无限循环,因为 JEE 过滤器也会在通过 RequestDispatcher 到你的 JSP 页面的内部调度中被触发。因此,当您尝试向您的 JSP 发送消息时,您会再次点击过滤器链中的第一个过滤器,并且会出现无限循环。所以你可以指定你的过滤器只针对外部请求。
<filter-mapping>
<filter-name>RequestLoggingFilter</filter-name> <!-- mandatory -->
<url-pattern>/*</url-pattern>
<servlet-name>XXXXXXXXXXXXXXXXX</servlet-name>
<dispatcher>REQUEST</dispatcher>
</filter-mapping>
检查您的 WsFilter,问题是它引起的,因为似乎所有过滤器都在 /* 上触发。如果你发布完整的 web.xml 这样我就知道出现的顺序我可以帮助你更多。
您通过 Oracle 教程来过滤请求,这是完成这些事情的最佳方法。
我认为将逻辑放入过滤器中并不是一个好主意。好的设计就是把逻辑放到servlet中。
我想为多个页面使用一个 servlet。当我尝试 运行 项目时,由于请求已满而发生错误。已经为 URL 模式尝试了很多选项,但没有结果
现在,我只看到一种方法。仅使用 post 方法并使用空的 url-pattern 但我不确定这是正确的选择 :D
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
version="4.0">
<display-name>Archetype Created Web Application</display-name>
<session-config>
<session-timeout>10</session-timeout>
</session-config>
<filter>
<filter-name>InitialFilter</filter-name>
<filter-class>eshop.filter.InitialFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
</filter>
<filter>
<filter-name>SessionFilter</filter-name>
<filter-class>eshop.filter.SessionFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>InitialFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<filter-mapping>
<filter-name>SessionFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<servlet>
<servlet-name>mainServlet</servlet-name>
<servlet-class>eshop.controller.MainServlet</servlet-class>
<load-on-startup>0</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>mainServlet</servlet-name>
<url-pattern>*.jsp</url-pattern>
</servlet-mapping>
<error-page>
<location>/WEB-INF/views/error.jsp</location>
</error-page>
</web-app>
public class IndexCommand implements Command {
@Override
public void execute(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException, MessagingException, ServiceException, SQLException {
if (request.getSession().getAttribute("role")=="admin")
{
Command command=new AccountCommand();
command.execute(request,response);
}
System.out.println(request.getRequestURI());
request.getRequestDispatcher("/WEB-INF/views/startPage.jsp").forward(request,response);
}
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
Command command = CommandFactory.createCommand(request);
try {
command.execute(request, response);
} catch (MessagingException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
} catch (ServiceException e) {
e.printStackTrace();
}
}
Type Exception Report
Message Servlet execution threw an exception
Description The server encountered an unexpected condition that prevented it from fulfilling the request.
Exception
javax.servlet.ServletException: Servlet execution threw an exception
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
eshop.command.IndexCommand.execute(IndexCommand.java:34)
eshop.controller.MainServlet.doGet(MainServlet.java:62)
javax.servlet.http.HttpServlet.service(HttpServlet.java:635)
javax.servlet.http.HttpServlet.service(HttpServlet.java:742)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
eshop.command.IndexCommand.execute(IndexCommand.java:34)
eshop.controller.MainServlet.doGet(MainServlet.java:62)
javax.servlet.http.HttpServlet.service(HttpServlet.java:635)
javax.servlet.http.HttpServlet.service(HttpServlet.java:742)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
Root Cause
java.lang.WhosebugError
eshop.command.IndexCommand.execute(IndexCommand.java:34)
eshop.controller.MainServlet.doGet(MainServlet.java:62)
javax.servlet.http.HttpServlet.service(HttpServlet.java:635)
javax.servlet.http.HttpServlet.service(HttpServlet.java:742)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
如何配置 web.xml 将 servlet 映射到 Java 中的多个 url 模式,问题出在哪里?
您可以为 servlet 放置任意数量的映射,但您将它们作为单独的 serrvlet-mapping 元素放置。
<servlet-mapping>
<servlet-name>mainServlet</servlet-name>
<url-pattern>*.jsp</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>mainServlet</servlet-name>
<url-pattern>*.jsp</url-pattern>
</servlet-mapping>
关于第二个问题,你会得到无限循环,因为 JEE 过滤器也会在通过 RequestDispatcher 到你的 JSP 页面的内部调度中被触发。因此,当您尝试向您的 JSP 发送消息时,您会再次点击过滤器链中的第一个过滤器,并且会出现无限循环。所以你可以指定你的过滤器只针对外部请求。
<filter-mapping>
<filter-name>RequestLoggingFilter</filter-name> <!-- mandatory -->
<url-pattern>/*</url-pattern>
<servlet-name>XXXXXXXXXXXXXXXXX</servlet-name>
<dispatcher>REQUEST</dispatcher>
</filter-mapping>
检查您的 WsFilter,问题是它引起的,因为似乎所有过滤器都在 /* 上触发。如果你发布完整的 web.xml 这样我就知道出现的顺序我可以帮助你更多。
您通过 Oracle 教程来过滤请求,这是完成这些事情的最佳方法。
我认为将逻辑放入过滤器中并不是一个好主意。好的设计就是把逻辑放到servlet中。