日期:2014-05-18  浏览次数:20812 次

eclipse中第二次提交自动加入/servlet

错误是: HTTP Status 404 - /PetHospital/servlet/servlet/AddOwnerInfoServlet

我用eclipse中进行编译的,由第一次JSP页面传参到servelt,servlet是接收值后把值插入数据库, 可以成功插入数据库插入数据库然后用request.getRequestDispatcher("/addOwner.jsp").forward(request, response);再跳回JSP进行再次添加,但这次添加没有成功,我在控制台看了一下第一次提交的路径是 /servlet/AddOwnerInfoServlet,而第二次提交的路径竟然自己又加了多加了个servlet这样 去除上下文路径, /servlet/servlet/AddOwnerInfoServlet

jsp中的action
<form name="form1" method="post" action="servlet/AddOwnerInfoServlet"> 


web.xml
  <servlet>
  <description>This is the description of my J2EE component</description>
  <display-name>This is the display name of my J2EE component</display-name>
  <servlet-name>AddOwnerInfoServlet</servlet-name>
  <servlet-class>com.mfl.Servlet.AddOwnerInfoServlet</servlet-class>
  </servlet>


  <servlet-mapping>
  <servlet-name>AddOwnerInfoServlet</servlet-name>
  <url-pattern>/servlet/AddOwnerInfoServlet</url-pattern>
  </servlet-mapping>

------解决方案--------------------
<form name="form1" method="post" action="/servlet/AddOwnerInfoServlet" >


web.xml 
<servlet > 
<description >This is the description of my J2EE component </description > 
<display-name >This is the display name of my J2EE component </display-name > 
<servlet-name >AddOwnerInfoServlet </servlet-name > 
<servlet-class >com.mfl.Servlet.AddOwnerInfoServlet </servlet-class > 
</servlet > 


<servlet-mapping > 
<servlet-name >AddOwnerInfoServlet </servlet-name > 
<url-pattern >/servlet/AddOwnerInfoServlet </url-pattern > 
</servlet-mapping >
加一斜杠就没 问题了
------解决方案--------------------
request.getRequestDispatcher("/addOwner.jsp").forward(request, response);
改为
response.sendRedirect("/addOwner.jsp");
试一下