HttpServletRequest 获取请求中的相关参数详解

版权声明:本文为博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/lovewebeye/article/details/88686503

//协议名称: HTTP/1.1

System.out.println("rotocol: " + request.getProtocol()); 

//http 或https
System.out.println("Scheme: " + request.getScheme()); 

// 返回服务器地址名称:如果是本地返回localhost或者127.0.0.1 

System.out.println("Server Name: " + request.getServerName() );

//返回服务器端口:如8080
System.out.println("Server Port: " + request.getServerPort()); 
 // 获得客户端的ip地址
System.out.println("Remote Addr: " + request.getRemoteAddr()); 

// 获得客户端的主机名。
System.out.println("Remote Host: " + request.getRemoteHost()); 

// 返回字符编码 Character Encoding: utf8 Content Length: -1
System.out.println("Character Encoding: " + request.getCharacterEncoding()); 
System.out.println("Content Length: " + request.getContentLength()); 

//定义网络文件的类型和网页的编码,决定浏览器将以什么形式、什么编码读取这个文件,
System.out.println("Content Type: "+ request.getContentType()); 

// 如果servlet由一个鉴定方案所保护,如HTTP基本鉴定,则返回方案名称
System.out.println("Auth Type: " + request.getAuthType()); 

// 返回HTTP请求方法(例如GET、POST等等)
System.out.println("HTTP Method: " + request.getMethod()); 

// 返回在URL中指定的任意附加路径信息。
System.out.println("path Info: " + request.getPathInfo()); 

 //返回在URL中指定的任意附加路径信息,被子转换成一个实际路径
System.out.println("path Trans: " + request.getPathTranslated()); 

// 返回查询字符串,即URL中?后面的部份。
System.out.println("Query String: " + request.getQueryString()); 

// 如果用户通过鉴定,返回远程用户名,否则为null。
System.out.println("Remote User: " + request.getRemoteUser()); 

// 返回客户端的会话ID 
System.out.println("Session Id: " + request.getRequestedSessionId()); 

// 返回URL中一部分,从“/”开始,包括上下文,但不包括任意查询字符串。 
System.out.println("Request URI: " + request.getRequestURI()); 

 // 返回请求URI上下文后的子串  
System.out.println("Servlet Path: " + request.getServletPath()); 

// request.getHeader()  获取HTTP头标指:如果其由请求给出,则名字应为大小写不敏感。   

// Accept:text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
System.out.println("Accept: " + request.getHeader("Accept")); 

// localhost:8080
System.out.println("Host: " + request.getHeader("Host")); 

// 点击超链接的页面url地址,如果地址栏直接输入则为null
System.out.println("Referer : " + request.getHeader("Referer")); 

// Accept-Language : zh-CN,zh;q=0.8
System.out.println("Accept-Language : " + request.getHeader("Accept-Language")); 

// Accept-Encoding : gzip,deflate,sdch
System.out.println("Accept-Encoding : " + request.getHeader("Accept-Encoding")); 

// User-Agent : Mozilla/5.0 (Windows NT 5.1) AppleWebKit/537.17 (KHTML, like Gecko) Chrome...
System.out.println("User-Agent : " + request.getHeader("User-Agent")); 

// Connection: keep-alive
System.out.println("Connection : " + request.getHeader("Connection")); 

// Cookie : JSESSIONID=A2B9CD01990BDC1353A135FFCCE688921
System.out.println("Cookie : " + request.getHeader("Cookie"));

//  CreateTime: 145886788998
System.out.println("CreateTime : " + session.getCreationTime()); 

//  LastAccessed: 145886798654
System.out.println("LastAccessed : " + session.getLastAccessedTime());

展开阅读全文

HttpServletRequest如何获取客户端请求的参数

11-02

服务端部署在Tomcat上,由Servlet处理,客户端使用socket进行通信(不是浏览器),工程名称TestProjectrnServlet端代码rn[code=Java]rnpublic class ProcessServlet extends HttpServlet rnrn private static final long serialVersionUID = 1L;rn rn public ProcessServlet() rn super();rnrn rnrn rn protected void doGet(HttpServletRequest request, HttpServletResponse response)rn throws ServletException, IOException rn System.out.println("receive Request message*******************:");rn request.setCharacterEncoding("UTF-8");rn BufferedReader reader = new BufferedReader(new InputStreamReader(request.getInputStream()));rn PrintWriter out = response.getWriter(); rn rn String msg = null;rn while ((msg = reader.readLine()) != null) //无法读取到rn System.out.println("receive Request message:" + msg);rn rn rn rn response.setContentType("text/html;charset=utf-8");rn out.println("");rn out.println(" First servlet");rn out.println("");rn out.println("welcome use selevet");rn out.println("");rn out.println(""); rn rn out.flush();rn out.close();rnrn rnrn protected void doPost(HttpServletRequest request, HttpServletResponse response)rn throws ServletException, IOException rn doGet(request, response);rn rnrnrn[/code]rn客户端代码rn[code=Java]rnpublic class Client rnrn private static int port = 8080;rnrn public static void main(String[] arg) rn Socket socket;rn try rnrn String para = "password=1232131&safeflg=0&username=abc";rn socket = new Socket("localhost", port);rn BufferedWriter wr = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream(),rn "UTF8"));rn InputStream ins = socket.getInputStream();rn StringBuffer sb = new StringBuffer();rn sb.append("POST /TestProject/ProcessServlet HTTP/1.1\r\n");// 注意\r\n为回车换行rn sb.append("Accept-Language: zh-cn\r\n");rn sb.append("Connection: Keep-Alive\r\n");rn sb.append("Host:localhost\r\n");rn sb.append("Content-Length:43\r\n");rn sb.append("Content-Type: application/x-www-form-urlencoded\r\n");rn sb.append("\r\n");rn sb.append(para);rn // 接收Web服务器返回HTTP响应包rn wr.write(sb.toString());rn wr.flush();rn rn BufferedReader rd = new BufferedReader(new InputStreamReader(ins));rn String line;rn while ((line = rd.readLine()) != null) rn System.out.println(line);rn rn catch (UnknownHostException e) rn e.printStackTrace();rn catch (IOException e) rn e.printStackTrace();rn rnrn rnrnrn[/code]rn在servlet端如何获得客户端发送的username,password等参数的值? 论坛

没有更多推荐了,返回首页