关闭

Servlet Request对象 获取服务器的相关信息

290人阅读 评论(0) 收藏 举报

文章摘自http://zhyx5017192.blog.163.com/blog/static/30925128201241922947126/

 

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 GetServerInfoServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		doPost(request, response);
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		response.setContentType("text/html");
		response.setCharacterEncoding("UTF-8"); // 这句一定要加上,否则在浏览器中输出中文乱码
		PrintWriter out = response.getWriter();
		out
				.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");
		out.println("<HTML>");
		out.println("  <HEAD><TITLE>通过Servlet获取服务器的运行参数</TITLE></HEAD>");
		out.println("  <BODY>");
		out.println("请求的URL为:" + request.getRequestURL() + "</br>");
		out.println("请求的URI为:" + request.getRequestURI() + "</br>");
		out.print("服务器Ip为:" + request.getRemoteAddr() + "</br>");
		out.println("服务器名为:" + request.getServerName() + "</br>");
		out.println("服务器端口号为:" + request.getServerPort() + "</br>");
		out.println("协议名为:" + request.getScheme() + "</br>");
		out.println("协议版本为:" + request.getProtocol() + "</br>");
		out.println("请求方法为:" + request.getMethod() + "</br>");
		out.println("上下文路径为:" + request.getContextPath() + "</br>");
		out.println("Servlet路径为:" + request.getServletPath() + "</br>");
		out.println("  </BODY>");
		out.println("</HTML>");
		out.flush();
		out.close();
	}
}

 

 

部署项目,启动tomcat服务器,在浏览器中输入
http://localhost:8080/TestJSP/servlet/GetServerInfoServlet

请求的URL为:http://localhost:8080/TestJSP/servlet/GetServerInfoServlet
请求的URI为:/TestJSP/servlet/GetServerInfoServlet
服务器Ip为:0:0:0:0:0:0:0:1
服务器名为:localhost
服务器端口号为:8080
协议名为:http
协议版本为:HTTP/1.1
请求方法为:GET
上下文路径为:/TestJSPServlet
路径为:/servlet/GetServerInfoServlet

 

<servlet>    
	<servlet-name>GetServerInfoServlet</servlet-name>  
	<servlet-class>com.xx.servlet.GetServerInfoServlet</servlet-class> 
</servlet>  
<servlet-mapping>  
	<servlet-name>GetServerInfoServlet</servlet-name>  
	<url-pattern>/servlet/GetServerInfoServlet</url-pattern>
</servlet-mapping>

 

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:326493次
    • 积分:8918
    • 等级:
    • 排名:第2254名
    • 原创:1096篇
    • 转载:14篇
    • 译文:0篇
    • 评论:5条
    最新评论