🔥码云GVP开源项目 12k star Uniapp+ElementUI 功能强大 支持多语言、二开方便! 广告
~~~ package com.servlet; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; import java.io.PrintWriter; public class ServletDemo extends HttpServlet { //属性赋值,在单例上会有线程安全问题 private Integer id=0; protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { System.out.println("haha"); } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { id +=1; Integer num = 0; num+=1; System.out.println(id); System.out.println(num); /** * 1.获得客户机信息 */ String requestUrl = request.getRequestURL().toString();//得到请求的URL地址 String requestUri = request.getRequestURI();//得到请求的资源 String queryString = request.getQueryString();//得到请求的URL地址中附带的参数 String remoteAddr = request.getRemoteAddr();//得到来访者的IP地址 String remoteHost = request.getRemoteHost(); int remotePort = request.getRemotePort(); String remoteUser = request.getRemoteUser(); String method = request.getMethod();//得到请求URL地址时使用的方法 String pathInfo = request.getPathInfo(); String localAddr = request.getLocalAddr();//获取WEB服务器的IP地址 String localName = request.getLocalName();//获取WEB服务器的主机名 response.setCharacterEncoding("UTF-8");//设置将字符以"UTF-8"编码输出到客户端浏览器 //通过设置响应头控制浏览器以UTF-8的编码显示数据,如果不加这句话,那么浏览器显示的将是乱码 response.setHeader("content-type", "text/html;charset=UTF-8"); PrintWriter out = response.getWriter(); out.write("获取到的客户机信息如下:"); out.write("<hr/>"); out.write("请求的URL地址:"+requestUrl); out.write("<br/>"); out.write("请求的资源:"+requestUri); out.write("<br/>"); out.write("请求的URL地址中附带的参数:"+queryString); out.write("<br/>"); out.write("来访者的IP地址:"+remoteAddr); out.write("<br/>"); out.write("来访者的主机名:"+remoteHost); out.write("<br/>"); out.write("使用的端口号:"+remotePort); out.write("<br/>"); out.write("remoteUser:"+remoteUser); out.write("<br/>"); out.write("请求使用的方法:"+method); out.write("<br/>"); out.write("pathInfo:"+pathInfo); out.write("<br/>"); out.write("localAddr:"+localAddr); out.write("<br/>"); out.write("localName:"+localName); } } ~~~ 注:在单例的类上,不用属性作用域,转用局部方法作用域