开发者

Java servlet not dispatching to another servlet

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"

 <form action="Search" method="post" name="searchForm">

So the first thing the SearchController does is:

 @Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    if (!validator.isValidAlphaOnly(request.getParameter("breed"))) {
        request.setAttribute("error", "Breed search term invalid. Enter only letters");
        Reque开发者_JAVA百科stDispatcher requestVar =  request.getRequestDispatcher("/Home");
        requestVar.forward(request, response);
    } else {
        processRequest(request, response);
    }
}

Except is doesn't go to /Home even though I have mappings as follows:

  <servlet>
    <servlet-name>HomeController</servlet-name>
    <servlet-class>Controllers.HomeController</servlet-class>
</servlet>
<servlet>
    <servlet-name>SearchController</servlet-name>
    <servlet-class>Controllers.SearchController</servlet-class>
</servlet>
<servlet>
    <servlet-name>DogController</servlet-name>
    <servlet-class>Controllers.DogController</servlet-class>
</servlet>
<servlet>
    <servlet-name>LogoutController</servlet-name>
    <servlet-class>Controllers.LogoutController</servlet-class>
</servlet>
<servlet>
    <servlet-name>UpdateController</servlet-name>
    <servlet-class>Controllers.UpdateController</servlet-class>
</servlet>
<servlet>
    <servlet-name>AddController</servlet-name>
    <servlet-class>Controllers.AddController</servlet-class>
</servlet>
<servlet-mapping>
    <servlet-name>SearchController</servlet-name>
    <url-pattern>/Search</url-pattern>
</servlet-mapping>
<servlet-mapping>
    <servlet-name>LogoutController</servlet-name>
    <url-pattern>/Logout</url-pattern>
</servlet-mapping>
<servlet-mapping>
    <servlet-name>UpdateController</servlet-name>
    <url-pattern>/Update</url-pattern>
</servlet-mapping>
<servlet-mapping>
    <servlet-name>AddController</servlet-name>
    <url-pattern>/Add</url-pattern>
</servlet-mapping>
<servlet-mapping>
    <servlet-name>DogController</servlet-name>
    <url-pattern>/Dog/View/*</url-pattern>
    <url-pattern>/Dog/Edit/*</url-pattern>
    <url-pattern>/Dog/Add</url-pattern>
</servlet-mapping>
<servlet-mapping>
    <servlet-name>HomeController</servlet-name>
    <url-pattern>/Home</url-pattern>
    <url-pattern>/</url-pattern>
</servlet-mapping>


You can use getNamedDispatcher:

ServletContext context = getServletContext();
RequestDispatcher requestVar = context.getNamedDispatcher("HomeController");


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.


  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.


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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜