客户端向浏览器发出的请求被封装成为一个HttpServletRequest对象,里面包含了本次请求的所有信息,如地址,参数,提交的数据等等
request已经封装了许多方法来获取这些数据,下面是一个获取数据的例子
package qdm.test; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; import java.io.PrintWriter; import java.security.Principal; import java.util.Locale; /** * Created by Seffrui_M on 2015/12/16. */ public class FirstServlet extends HttpServlet { //返回语言信息 private String getLocal(Locale local) { if(Locale.SIMPLIFIED_CHINESE.equals(local))return "简体中文"; else if(Locale.TRADITIONAL_CHINESE.equals(local))return "繁体中文"; else if(Locale.ENGLISH.equals(local))return "英文"; return "其他语言"; } private String getExplore(String agent) { if(agent.indexOf("TencentTraveler")>0)return "腾讯浏览器"; else if(agent.indexOf("Firefox")>0)return "火狐浏览器"; return "其他浏览器"; } @Override protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { request.setCharacterEncoding("UTF-8"); response.setContentType("text/html;charset=UTF-8"); response.setCharacterEncoding("UTF-8"); String autoType=request.getAuthType(); String localAddr=request.getLocalAddr();//获取服务器ip String localName=request.getLocalName();//获取服务器名 int localport=request.getLocalPort();//获取服务器端口号 Locale local=request.getLocale();//获取语言环境 String contextPath=request.getContextPath();//获取context的路径 String method=request.getMethod();//获取get还是post方法 String getPathInfo=request.getPathInfo(); String pathTranslated=request.getPathTranslated(); String protocol=request.getProtocol();//获取协议信息 String ip=request.getRemoteAddr();//获取客户端ip String remoteUser=request.getRemoteUser();//客户端用户 int port=request.getRemotePort();//客户端端口; String requestURI=request.getRequestURI();//获取URI StringBuffer requestURL=request.getRequestURL();//获取URL String scheme=request.getScheme();//获取协议头部 String serverName=request.getServerName(); int serverPort=request.getServerPort(); String servlet=request.getServletPath(); Principal userPrincipal=request.getUserPrincipal(); String accept=request.getHeader("accept"); String referer=request.getHeader("referer"); String userAgent=request.getHeader("user-agent"); String serverInfo=this.getServletContext().getServerInfo(); PrintWriter out=response.getWriter(); out.println("<html lang=\"zh-cn\">"); out.println("<body>"); out.println("<h1>本次访问的信息为</h1><br>"); out.println("<p>"); out.println("服务器为:"+localName+"   "); out.println("服务器ip地址为:"+localAddr+"   "); out.println("服务器ip端口号为:"+localport+"<br>"); out.println("所使用的语言为:"+getLocal(local)+"   "); out.println("context路径:"+contextPath+"   "); out.println("所使用的方法时:"+method+"<br>"); out.println("路径为:"+getPathInfo+"   "); out.println("路径转移为:"+pathTranslated+"   "); out.println("协议为:"+protocol+"<br>"); out.println("客户端ip:"+ip+"   "); out.println("客户端用户为:"+remoteUser+"   "); out.println("客户端端口为:"+port+"<br>"); out.println("URI为:"+requestURI+"   "); out.println("URL为:"+requestURL+"   ");
/*
.
.
.
.
*/
out.println("</p>"); out.println("</body>"); out.println("</html>"); } }