关于HttpServletRequest类中的方法getPathInfo()获得额外路径的问题
//代码如下:
package test;
import
java.io.IOException;
import java.io.PrintWriter;
import
javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class Hello extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws
ServletException,
IOException {
response.setContentType( "text/html ");
PrintWriter out = response.getWriter();
out.println( " <html> ");
out.println( " <head> <body> <center> <h2> <pre> ");
out.println( "the servlet pathTranslated is: "+request.getPathTranslated());
out.println( "the servlet pathInfo is: "+request.getPathInfo());
out.println( " </pre> </h2> </center> </body> </html> ");
out.flush();
out.close();
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request,response);
}
}
/*我用MyEclipse在Tomcat上部署之后,在IE的地址栏里输入:
http://127.0.0.1:8080/Test/servlet/Hello/index.shtml
回车后显示如下结果:
The requested resource (/Test011/servlet/Hello/index.shtml) is not available.
按照书上说getPathInfo()能返回附加在用户端送来URL的额外路径名称,本例的结果不应该是 "/index.shtml "吗?为什么不是呢?
我建立的是WEB Project,其中: "Test "是Project Name*/
------解决方案--------------------你写的只是一个Servlet,里面只不过是把里面request.getPathInfo()显示出来,没有什么其他的作用,你的index.html肯定是工程下面的另外的一个html文件,其实你这个Servlet只要配制好了web.xml就可以直接访问了,
------解决方案--------------------你用 http://127.0.0.1:8080/Test/servlet/Hello/index.shtml 想要访问什么?
如果要直接访问servlet,就直接在web.xml中找 <servlet-mapping> 对应的映射地址就可以了
: http://127.0.0.1:8080/Test/ <servlet-mapping> 映射地址;
若要访问WebRoot下的页面,http://127.0.0.1:8080/Test/<页面rui>,
不知你弄明白了没,
------解决方案--------------------应该是你的web.xml的问题,没有把请求路径映射到servlet里面去
下面这样才对。
<servlet-mapping>
<servlet-name> hello </servlet-name>
<url-pattern> /servlet/hello/* </url-pattern>
</servlet-mapping>