servlet代码
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 DemoServlet extends HttpServlet { /** * @see javax.servlet.http.HttpServlet#doGet(javax.servlet.http.HttpServletRequest, * javax.servlet.http.HttpServletResponse) */ public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html"); response.setCharacterEncoding("UTF-8"); PrintWriter out = response.getWriter(); out.println("------服务器信息------" + "<br>"); out.println("获取请求的URL中指向请求上下文的那部分字符串【request.getContextPath()】:" + request.getContextPath() + "<br>"); out.println("获取请求使用的协议名【request.getScheme()】:" + request.getScheme() + "<br>"); out.println("获取请求使用的具体协议版本【request.getProtocol()】:" + request.getProtocol() + "<br>"); out.println("获取请求送往的服务器主机名【request.getServerName()】:" + request.getServerName() + "<br>"); out.println("获取请求送往的服务器端口号【request.getServerPort()】:" + request.getServerPort() + "<br>"); out.println("获取请求接收的主机地址【request.getLocalAddr()】:" + request.getLocalAddr() + "<br>"); out.println("获取请求接收的本地主机名【request.getLocalName()】:" + request.getLocalName() + "<br>"); out.println("获取请求接收的本地端口【request.getLocalPort():】" + request.getLocalPort() + "<br>"); out.println("获取请求的方法【request.getMethod():】" + request.getMethod() + "<br>"); out.println("获取请求URL从端口到请求参数中间的部分【request.getRequestURI():】" + request.getRequestURI() + "<br>"); out.println("获取请求URL【request.getRequestURL():】" + request.getRequestURL() + "<br>"); out.println("获取请求URL中访问servlet的那部分路径【request.getServletPath():】" + request.getServletPath() + "<br>"); out.println("获取给定虚拟路径在服务端的真实路径,从servlet3.0开始【request.getServletContext().getRealPath(‘/‘):】" + request.getServletContext().getRealPath("/") + "<br>"); out.println("获取给定虚拟路径在服务端的真实路径,从servlet2.3开始【request.getSession().getServletContext().getRealPath(‘/‘):】" + request.getSession().getServletContext().getRealPath("/") + "<br>"); out.println("<br>"); out.println("------客户端信息------" + "<br>"); out.println("获取发送请求的客户端地址【request.getRemoteAddr():】" + request.getRemoteAddr() + "<br>"); out.println("获取发送请求的客户端主机名【request.getRemoteHost():】" + request.getRemoteHost() + "<br>"); out.println("获取发送请求的客户端端口【request.getRemotePort():】" + request.getRemotePort() + "<br>"); out.println("<br>"); out.flush(); out.close(); } }
web.xml
<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd" > <web-app> <display-name>Archetype Created Web Application</display-name> <servlet> <servlet-name>DemoServlet</servlet-name> <display-name>This is the display name of my J2EE component</display-name> <description>This is the description of my J2EE component</description> <servlet-class>DemoServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>DemoServlet</servlet-name> <url-pattern>/servlet/DemoServlet</url-pattern> </servlet-mapping> </web-app>
pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>Demo.Web</groupId> <artifactId>Web-Snapshot</artifactId> <packaging>war</packaging> <version>1.0</version> <name>Web-Snapshot Maven Webapp</name> <url>http://maven.apache.org</url> <build> <finalName>Web-Snapshot</finalName> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-surefire-plugin</artifactId> <configuration> <includes> <include>**/*Test.class</include> </includes> </configuration> </plugin> <!-- compile plugins --> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>2.3.1</version> <configuration> <source>1.6</source> <target>1.6</target> <encoding>UTF-8</encoding> </configuration> <dependencies> <dependency> <groupId>org.codehaus.plexus</groupId> <artifactId>plexus-compiler-javac</artifactId> <version>1.8.1</version> </dependency> </dependencies> </plugin> </plugins> </build> <dependencies> <!-- 单元测试 --> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> <scope>test</scope> </dependency> <!-- servlet --> <dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> <version>3.0.1</version> <scope>provided</scope> </dependency> <!-- jsp --> <dependency> <groupId>javax.servlet.jsp</groupId> <artifactId>jsp-api</artifactId> <version>2.2</version> <scope>provided</scope> </dependency> <!-- jstl --> <dependency> <groupId>taglibs</groupId> <artifactId>standard</artifactId> <version>1.1.2</version> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>jstl</artifactId> <version>1.1.2</version> </dependency> </dependencies> </project>
其他运行环境信息
tomcat7
访问url:http://192.168.26.31/Web-Snapshot/servlet/DemoServlet
服务端ip:192.168.26.31
客户端ip:192.168.26.32
结果
------服务器信息------ 获取请求的URL中指向请求上下文的那部分字符串【request.getContextPath()】:/Web-Snapshot 获取请求使用的协议名【request.getScheme()】:http 获取请求使用的具体协议版本【request.getProtocol()】:HTTP/1.1 获取请求送往的服务器主机名【request.getServerName()】:192.168.26.31 获取请求送往的服务器端口号【request.getServerPort()】:80 获取请求接收的主机地址【request.getLocalAddr()】:192.168.26.31 获取请求接收的本地主机名【request.getLocalName()】:GJB-PC 获取请求接收的本地端口【request.getLocalPort():】80 获取请求的方法【request.getMethod():】GET 获取请求URL从端口到请求参数中间的部分【request.getRequestURI():】/Web-Snapshot/servlet/DemoServlet 获取请求URL【request.getRequestURL():】http://192.168.26.31/Web-Snapshot/servlet/DemoServlet 获取请求URL中访问servlet的那部分路径【request.getServletPath():】/servlet/DemoServlet 获取给定虚拟路径在服务端的真实路径,从servlet3.0开始【request.getServletContext().getRealPath(‘/‘):】E:\java\apache-tomcat-7.0.55\webapps\Web-Snapshot获取给定虚拟路径在服务端的真实路径,从servlet2.3开始【request.getSession().getServletContext().getRealPath(‘/‘):】E:\java\apache-tomcat-7.0.55\webapps\Web-Snapshot ------客户端信息------ 获取发送请求的客户端地址【request.getRemoteAddr():】192.168.26.32 获取发送请求的客户端主机名【request.getRemoteHost():】192.168.26.32 获取发送请求的客户端端口【request.getRemotePort():】52859
时间: 2024-10-08 16:44:04