我有2个servlet,“HomeController”和“SearchController”。在home.jsp上,我有一个表格,其中包含一个搜索框,当提交的操作为“搜索”时
<form action="Search" method="post" name="searchForm">
所以SearchController的第一件事就是:
@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");
RequestDispatcher requestVar = request.getRequestDispatcher("/Home");
requestVar.forward(request, response);
} else {
processRequest(request, response);
}
}
除非我有以下映射,否则不会进入/ Home:
<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>
答案 0 :(得分:2)
您可以使用getNamedDispatcher:
ServletContext context = getServletContext();
RequestDispatcher requestVar = context.getNamedDispatcher("HomeController");
答案 1 :(得分:0)
好吧,对于一个你的servlet映射配置看起来不太正确:你有这个:
<servlet-mapping>
<servlet-name>SearchController</servlet-name>
<url-pattern>/Search</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-mapping>
<servlet-name>HomeController</servlet-name>
注意2 servlet-mapping
行?
我希望你的web.xml文件中有一个错误:你的代码看起来很好。
答案 2 :(得分:0)
答案 3 :(得分:0)
我认为我发现了你的问题,你的代码确实没问题,web.xml是有问题的那个:
<servlet-mapping>
<servlet-name>HomeController</servlet-name>
<url-pattern>/Home</url-pattern>
<url-pattern>/</url-pattern>
</servlet-mapping>
servlet映射中有多个url-patterns。 试试这个
<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>
它可能会解决您的问题,Web容器有时会非常挑剔这些细节。
来源:http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd
xsd指定servlet映射类型:
<xsd:sequence>
<xsd:element name="servlet-name"
type="j2ee:servlet-nameType"/>
<xsd:element name="url-pattern"
type="j2ee:url-patternType"/>
</xsd:sequence>
那里没有多个网址模式。