day117为项目名
1.在servlet中访问
(一) web.xml
<servlet>
<servlet-name>Aservlet</servlet-name>
<servlet-class>day117.Aservlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Aservlet</servlet-name>
<url-pattern>/test/*</url-pattern>
</servlet-mapping>
(二) servlet
package day117;
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 Aservlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
String pathInfo = request.getPathInfo();
System.out.println("pathInfo:"+pathInfo);
String servletPath = request.getServletPath();
System.out.println("servletPath:"+servletPath);
String contextPath = request.getContextPath();
System.out.println("contextPath:"+contextPath);
StringBuffer requestURL = request.getRequestURL();
System.out.println("requestURL:"+requestURL);
String requestURI = request.getRequestURI();
System.out.println("requestURi:"+requestURI);
}
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
}
(三) 运行结果
访问 http://localhost/day117/test/hello/world?name=jack
pathInfo:/hello/world
servletPath:/test
contextPath:/day117
requestURL:http://localhost/day117/test/hello/world
requestURi:/day117/test/hello/world
servletPath:/test
contextPath:/day117
requestURL:http://localhost/day117/test/hello/world
requestURi:/day117/test/hello/world
2.在jsp中访问
path.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'index.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
<%
String pathInfo = request.getPathInfo();
System.out.println("pathInfo:"+pathInfo);
String servletPath = request.getServletPath();
System.out.println("servletPath:"+servletPath);
String contextPath = request.getContextPath();
System.out.println("contextPath:"+contextPath);
StringBuffer requestURL = request.getRequestURL();
System.out.println("requestURL:"+requestURL);
String requestURI = request.getRequestURI();
System.out.println("requestURi:"+requestURI);
%>
</body>
</html>
(三) 运行结果
访问 http://localhost/day117/path.jsp?name=jack
pathInfo:null
servletPath:/path.jsp
contextPath:/day117
requestURL:http://localhost/day117/path.jsp
requestURi:/day117/path.jsp
servletPath:/path.jsp
contextPath:/day117
requestURL:http://localhost/day117/path.jsp
requestURi:/day117/path.jsp