Search code examples
javaservletsfilterrequesturl-pattern

The server encountered an unexpected condition that prevented it from fulfilling the request. HTTP 500


I want to use one servlet for many pages. when i try to run the project, an error occurs because the request is full. have tried many options for URL patterns, but nothing comes out

right now, i see only one way. to use only post methods and have empty url-pattern but i'm not sure that it is right choice :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.StackOverflowError
        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)

How to configure web.xml to map a servlet to more than one url pattern in Java and where is the problem?


Solution

  • you can put as many mappings as you want for servlet, but you put them as separate serrvlet-mapping elements.

     <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>
    

    About the second problem, you get indefinite cycle, because JEE filters are also get triggered in internal dispatching through RequestDispatcher to your JSP page. So when you try to dispatch to your JSP, you again hit the first filter in the chain of filters, and here you get indefinite cycle. So you can specify that your filter is only for the external requests.

    <filter-mapping>
      <filter-name>RequestLoggingFilter</filter-name> <!-- mandatory -->
      <url-pattern>/*</url-pattern>
      <servlet-name>XXXXXXXXXXXXXXXXX</servlet-name>
      <dispatcher>REQUEST</dispatcher>
    </filter-mapping>
    

    Check your WsFilter, it is causing the problem , because it appears all filters are triggered on /*. If you publish full web.xml so I know the order of appearance I can help you more.

    You go through Oracle tutorial for filtering requests, it is the best one to go through for those things.

    https://docs.oracle.com/javaee/5/tutorial/doc/bnagb.html

    I think it is not a good idea to put logic in your filters. Good design is to put logic into servlets.