在WebRoot目录下直接创建jsp文件时,无需修改web.xml中的配置信息,而当创建的jsp文件在子文件夹中时,就需要修改web.xml的配置信息了。下面用一个实例加以说明:
在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>LoginServlet</servlet-name>
<servlet-class>LoginServlet</servlet-class> <!-- servlet文件在src目录中的位置 -->
</servlet>
<servlet-mapping>
<servlet-name>LoginServlet</servlet-name>
<url-pattern>/chap01/login</url-pattern> <!-- URL是相对于basepath的路径,即:http://yilong-pc:8080/servletBasis/chap01/login -->
</servlet-mapping>
service project的名称为servletBasis,
该服务端basepath为http://yilong-pc:8080/servletBasis/
JSP页面的路径为\servletBasis\WebRoot\chap01\index.jsp
其中需要注意的是:
在JSP页面中 form表单的<form action="login" method="post"> action链接并不是实际转向的servlet文件链接,实际处理响应的servlet链接=该JSP页面路径+action链接=http://yilong-pc:8080/servletBasis/chap01/login,与web.xml 的<url-pattern>参数一致,即配置正确,servlet可以成功处理。
总结:
当jsp文件为子文件中,web.xml的<url-pattern>参数为 action值前面加上子文件名,即:/子文件/action参数。
1559

被折叠的 条评论
为什么被折叠?



