Request对象介绍(客户端到服务器)
1.处理请求和响应的过程request,response,关于request可以从三个方面着手学习。1:如何获取请求头 行 体 2:请求中文处理 3:请求对象的其它常用方法
1.1:request常用方法:
package com.test; import java.io.IOException;
import java.io.PrintWriter;
import java.util.Enumeration; import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; public class UserServlet extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // request.getMethod()方法
System.out.println("方法:"+request.getMethod()); // 获取请求的路径URI(统一资源标识符)和URL(同一资源定位符)
System.out.println("URI:"+request.getRequestURI());
System.out.println("URL:"+request.getRequestURL()); // 获取请求的协议类型
System.out.println("协议:"+request.getProtocol()); // 获取IP地址
System.out.println("IP地址:"+request.getRemoteAddr()); // 获取端口号
System.out.println("服务器端口号:"+request.getLocalPort()); // 获取请求头 头信息都可以获取到
System.out.println("请求头名称:"+request.getHeader("Accept"));
System.out.println("请求头名称:"+request.getHeader("Accept-Language"));
System.out.println(); // 获取所有请求头信息
Enumeration<String> headerNames = request.getHeaderNames();
while(headerNames.hasMoreElements()){
String element = headerNames.nextElement();
System.out.println(element+" "+request.getHeader(element));
}
} public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response);
} }
结果:

1.2:request请求中文处理
GET乱码解决:
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>"> <title>My JSP 'index.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head> <body> <h1>GET方式</h1>
<form method="GET" action="servlet/UsersServlet">
帐号:<input type="text" name="username" /><br />
<input type="submit"value="提交" />
</form> </body>
</html>
Servlet:
package com.test; 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 UsersServlet extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // 解决GET乱码
String name = request.getParameter("username"); name = new String(name.getBytes("iso-8859-1"),"utf-8"); System.out.println(name); } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response);
} }
POST乱码解决:
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>"> <title>My JSP 'index.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head> <body>
<h1>POST方式</h1>
<form method="POST" action="servlet/UsersServlet">
帐号:<input type="text" name="username" /><br />
<input type="submit"value="提交" />
</form>
</body>
</html>
Servlet:
package com.test; 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 UsersServlet extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // 解决POST乱码
request.setCharacterEncoding("UTF-8");
String name = request.getParameter("username"); System.out.println(name); } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response);
} }
总结要记住得内容:
1 处理get请求乱码 String string = new String(parameter.getBytes("iso-8859-1"),"utf-8");
2 处理post请求乱码 request.setCharacterEncoding("utf-8");
Request容器(存取删)(域对象)和请求转发
总结:
当一个Web资源收到客户端的请求后,如果希望服务器通知另外一个资源处理.
可以通过 转发对象 RequestDispatcher 对象的forward(request,response)方法,将当前请求传递给其他的Web资源进行处理,这种方式称为请求转发。
注:在这些转发的过程中,所有的Servlet共享同一个请求对象。在转发中,客户端是感觉不到服务器内部在跳转。而且客户端的浏览器的地址栏中是不会发生任何变化的。
因为在多个Servlet中可以进行转发,导致多个Servlet之间共享同一个request对象,于是在发出转发的Servlet中,可以把request对象当做一个容器,然后给其中保存数据,在其他的Servlet中可以取出前面的Servlet给request对象中保存的数据。request对象如果当作容器的话,它只是在当前请求中有效。当请求响应结束了,这个容器就消失了。
转发得小结:
1 转发可以调用其他得servlet程序
2 转发可以获取保密路径(WEB-INF)下得资源
另:
1 使用request对象,可以获取请求行
2 使用request对象,可以获取请求头
3 使用request对象,可以处理中文乱码
4 使用request对象,调用下一个servlet(请求转发)
5 使用request对象,在一个请求转发过程中,让两个servlet共享数据,是一个容器。
Request对象介绍(客户端到服务器)的更多相关文章
- 通过request对象获取客户端的相关信息
通过request对象获取客户端的相关信息 制作人:全心全意 通过request对象可以获取客户端的相关信息.例如HTTP报头信息.客户信息提交方式.客户端主机IP地址.端口号等等. request获 ...
- .NET Request对象介绍
Request对象用于检索从浏览器向服务器所发送的请求信息.它提供对当前页请求的访问,包括标题,Cookie,客户端证书等等.它也与HTTP协议的请求消息对应 Request常用的属性 属性 具体内容 ...
- 通过HttpservletRequest对象获取客户端的真实IP地址
这篇文章主要介绍了Java中使用HttpRequest获取用户真实IP地址,使用本文方法可以避免Apache.Squid.nginx等反向代理软件导致的非真实IP地址,需要的朋友可以参考下 在JSP里 ...
- request对象学习
import java.io.IOException; import java.util.Enumeration; import javax.servlet.ServletException; imp ...
- SignalR一个集成的客户端与服务器库。内部的两个对象类:PersistentConnection和Hub
SignalR 将整个交换信息的行为封装得非常漂亮,客户端和服务器全部都使用 JSON 来沟通,在服务器端声明的所有 hub 的信息,都会一般生成 JavaScript 输出到客户端. 它是基于浏览器 ...
- Request对象主要用于获取来自客户端的数据,如用户填入表单的数据、保存在客户端的Cookie等。
1.主要属性 ApplicationPath 获取服务器上asp.net应用程序的虚拟应用程序根路径 Browser 获取有关正在请求的客户端的浏览器功能的信息,该属性值为:HttpBrows ...
- 序列化与反序列化、def的介绍与快速使用、cbv源码分析、APIView与request对象分析
今日内容概要 序列化与反序列化 def介绍和快速使用 cbv源码流程分析 drf之APIView和Request对象分析 内容详细 1.序列化和反序列化 # api接口开发 最核心最常见的一个过程就是 ...
- 介绍一款chrom浏览器插件 DHC是一款使用chrome模拟REST客户端向服务器发送测试数据的谷歌浏览器插件
先打个小广告哈 公司招java架构师,月薪25K以上,负责电商平台架构工作,工作地点在北京 1号线永安里站 附近,如有意向 请把简历发我邮箱jia6235@163.com 可以内部推荐. DHC是一款 ...
- JSP内置对象--request对象
本文主要介绍JSP中的request对象 request对象的主要方法: setAttribute(String name,Object):设置名字为name的request的参数值 getAttri ...
随机推荐
- python3----split and join
s = "I am fine" s = s.split(" ") print(s) print("%".join(s)) results: ...
- iOS --有行距的图文混排
UILabel *label = [[UILabel alloc]init]; label.numberOfLines = ; [self.view addSubview:label]; label. ...
- PDO drivers no value in Windows
学习php编程遇到 Uncaught exception 'PDOException' with message 'could not find driver' 或者 Undefined class ...
- Javadoc生成html帮助文档
注意事项: 右键项目 -> Export -> Java -> JavaDoc -> 选定Public表示录入所有的源文件,其他的可想而知 按步骤走下去最后Finish时 ...
- Scala之模式匹配(Patterns Matching)
前言 首先.我们要在一開始强调一件非常重要的事:Scala的模式匹配发生在但绝不仅限于发生在match case语句块中.这是Scala模式匹配之所以重要且实用的一个关键因素!我们会在文章的后半部分具 ...
- ios中的coredata
本文转载至 http://blog.csdn.net/chen505358119/article/details/9334831 分类: ios2013-07-15 18:12 12449人阅读 评论 ...
- Android UI开发第二十七篇——实现左右划出菜单
年前就想写左右滑动菜单,苦于没有时间,一直拖到现在,这篇代码实现参考了网上流行的SlidingMenu,使用的FrameLayout布局,不是扩展的HorizontalScrollView. 程序中自 ...
- 【BZOJ2044】三维导弹拦截 DP+(有上下界的)网络流
[BZOJ2044]三维导弹拦截 Description 一场战争正在A国与B国之间如火如荼的展开. B国凭借其强大的经济实力开发出了无数的远程攻击导弹,B国的领导人希望,通过这些导弹直接毁灭A国的指 ...
- 烂代码 git blame
关于烂代码的那些事(上) - Axb的自我修养 http://blog.2baxb.me/archives/1343 关于烂代码的那些事(上) 六月 21, 2015 57 条评论 目录 [显示] 1 ...
- Python3.6全栈开发实例[007]
7.此函数只接收一个参数且此参数必须是列表数据类型,此函数完成的功能是返回给调用者一个字典,此字典的键值对为此列表的索引及对应的元素.例如传入的列表为:[11,22,33] 返回的字典为 {0:11, ...