Java servlet not dispatching to another servlet

前端 未结 4 1226
无人共我
无人共我 2021-01-24 08:58

I have 2 servlets, \"HomeController\" and \"SearchController\". On the home.jsp I have a form that has a search box and when submitted actions to \"Search\"

 <         


        
相关标签:
4条回答
  • 2021-01-24 09:12

    You can use getNamedDispatcher:

    ServletContext context = getServletContext();
    RequestDispatcher requestVar = context.getNamedDispatcher("HomeController");
    
    0 讨论(0)
  • 2021-01-24 09:12

    Well, for one your servlet mapping config doesn't quite look right: you have this:

    <servlet-mapping>
        <servlet-name>SearchController</servlet-name>
        <url-pattern>/Search</url-pattern>
    </servlet-mapping>
    <servlet-mapping>
    <servlet-mapping>
        <servlet-name>HomeController</servlet-name>
    

    Notice the 2 servlet-mapping lines?

    I expect you just have an error in your web.xml file: your code looks fine.

    0 讨论(0)
  • 2021-01-24 09:19

    I think I found your problem, your code is actually ok, the web.xml is the one that has the problem:

    <servlet-mapping>
        <servlet-name>HomeController</servlet-name>
        <url-pattern>/Home</url-pattern>
        <url-pattern>/</url-pattern>
    </servlet-mapping>
    

    You have there multiple url-patterns in a servlet mapping. Try this instead

    <servlet-mapping>
        <servlet-name>HomeController</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>
    <servlet-mapping>
        <servlet-name>HomeController</servlet-name>
        <url-pattern>/Home</url-pattern>
    </servlet-mapping>
    

    It may resolve your issue, web containers are sometimes very picky with these details.

    Source: http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd

    The xsd specifies for the servlet-mapping type:

    <xsd:sequence>
      <xsd:element name="servlet-name"
               type="j2ee:servlet-nameType"/>
      <xsd:element name="url-pattern"
               type="j2ee:url-patternType"/>
    </xsd:sequence>
    

    There is no multiple url-patterns there.

    0 讨论(0)
  • 2021-01-24 09:29
    1. Check if it is going into the condition.
    2. Check if the response is getting committed before forward. It yes, then the forward will fail.
    3. Worst case, it could be an issue with your app server.
    4. Try using ServletContext.getNamedDispatcher("HomeController"); as an alternative.
    0 讨论(0)
提交回复
热议问题