在名称为 Hello 的 WEB 工程下有两个页面:html 页面和jsp 页面,其中都存在这样的表单<form action="servletOne" method="post">
<input type="text" name="username" value="HAVE">
<input type="submit" value="GO">
</form>这两个页面都在 context 下的 test 文件夹下。当在这两个页面中提交这相同的表单时出现问题:
html 页面中的访问 URL 被编译为:http://localhost:8080/Hello/test/servletOne
jsp 页面中的访问 URL 被编译为:http://localhost:8080/Hello/servletOne
哪为朋友能告之原因,先谢了!
<input type="text" name="username" value="HAVE">
<input type="submit" value="GO">
</form>这两个页面都在 context 下的 test 文件夹下。当在这两个页面中提交这相同的表单时出现问题:
html 页面中的访问 URL 被编译为:http://localhost:8080/Hello/test/servletOne
jsp 页面中的访问 URL 被编译为:http://localhost:8080/Hello/servletOne
哪为朋友能告之原因,先谢了!
<servlet-name>servletOne</servlet-name>
<servlet-class>test.ServletOne</servlet-class>
</servlet> <servlet-mapping>
<servlet-name>servletOne</servlet-name>
<url-pattern>/servletOne</url-pattern>
</servlet-mapping>
如果使用 struts
同样这个 jsp 页面其中的表单如下<form action="login.do" method="post">
<input type="text" name="username" value="HAVE">
<input type="submit" value="GO">
</form>此时的访问 URL 则又被编译成 http://localhost:8080/Hello/test/login.do
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
form里这样写:
<form action="<%!=basePath/servletName%>" >
这样jsp编译后就是完整路径,使你的代码更灵活 <servlet>
<servlet-name>servletOne</servlet-name>
<servlet-class>test.ServletOne</servlet-class>
</servlet> <servlet-mapping>
<servlet-name>servletOne</servlet-name>
<!-- 映射路径是basePath/servletOne ,是按这个路径访问servlet 的-->
<url-pattern>/servletOne</url-pattern>
</servlet-mapping>
帮看下代码吧
test 文件夹含有表单的 JSP 页面:<form action="login.do" method="post">
<input type="text" name="username" value="HAVE">
<input type="submit" value="GO">
</form>
struts-config.xml 中的配置<action path="/login" type="may.struts.controlLayer.actions.LoginAction"></action>web.xml 中关于 Struts 的 ActionServlet 的声明与映射 <servlet>
<servlet-name>action</servlet-name>
<servlet-class>org.apache.struts.action.ActionServlet</servlet-class>
<init-param>
<param-name>config</param-name>
<param-value>/WEB-INF/struts-config.xml</param-value>
</init-param>
<init-param>
<param-name>debug</param-name>
<param-value>3</param-value>
</init-param>
<init-param>
<param-name>detail</param-name>
<param-value>3</param-value>
</init-param>
<load-on-startup>0</load-on-startup>
</servlet> <servlet-mapping>
<servlet-name>action</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
这样的条件下,我在 这个 JSP 页面中提交表单则请求 URL 为:http://localhost:8080/Hello/test/login.do
所以表单无法正常提交到 LoginAction 。只有将表单的 action 的值改为:"../login.do" 或者将 struts 配置文件中的 <action> 标签的 path 属性的值该为 "/test/login" 才可以正常到达 LoginAction.
可是按照先前的结论:这个 test 文件夹下的 Jsp 页面所提交的表单的 URL 应该是http://localhost:8080/Hello/login.do 才对啊!
这里疑惑啊!
就是写成 login.do 产生了如下疑问!
______________________________________________________________________________________
这个 JSP 页面中提交表单则请求 URL 为:http://localhost:8080/Hello/test/login.do
所以表单无法正常提交到 LoginAction 。只有将表单的 action 的值改为:"../login.do" 或者将 struts 配置文件中的 <action> 标签的 path 属性的值该为 "/test/login" 才可以正常到达 LoginAction.
______________________________________________________________________________________
有时间给我你 Email 过去。今天有点紧了!因为那个工程挺大的,我改天写个小的,让这个问题暴露其中,然后给你发过去!
好了,谢了朋友!
这贴就这样了,结了!
不管服务器端是怎么样,是JSP也好
HTML也,用了struts也好,甚至是
.net也好,FORM的submit的路径是和服务
器端无关的,它只和当前URL里的地址有关,
和本身在工程的位置也是无关的.我想LZ大概是用了struts转到那个JSP页面
的,而且是forward的,所以实际上地址栏里
应该是xxx.do,而不是/test/xxx.do