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 ...
随机推荐
- 使用PHP函数输出前一天的时间和后一天的时间
1.明确date()函数和time()函数的功能,其中time()函数是获取时间戳函数 2.输出前一天的当前时间: echo '一天之前的时间为:'.date('Y-m-d H:i:s',time() ...
- nginx访问css js 图片等静态资源,报404或无法定向访问到
配置完nginx,把php的项目放上去后,发现css,js和图片全部访问不到,一直重定向到根目录执行index.php,郁闷的在网上查了半天,原来不同后缀名的文件访问时都要在nginx.conf中声明 ...
- 复制对象(一)copy和mutableCopy方法
本文转载至 http://www.tuicool.com/articles/Fn6rMn CSDN博客原文 http://blog.csdn.net/u010962810/article/detai ...
- NPOI操作Excel常用函数
最近因项目接触了NPOI,感觉还是蛮不错的,网络上的教程普遍版本较老,本篇记录所常用操作,采用NPOI 2.0版本. 推荐: NPOI官方网站 NPOI 1.2.4/1.2.5 官方教程 新建Exce ...
- ubuntu14.0 hadoop2.4.0 64位基于jdk1.7搭建
注意:hadoop有两种运行模式,安全模式和非安全模式.安装模式是以指定在健壮的,基于身份验证上运行的,本文无需运行在非安全模式下,可以直接使用root用户. 本文用户是基于root用户来运行的 一. ...
- shadow批量破解
john有个参数可以设置破解时间,比如破解5秒则设置:--max-run-time=5,可以利用这个参数批量破解 for i in *;do (echo $i>>out;john --ma ...
- adobe flash player升级coredump分析
flash player版本号:14.0.0.125 产品名称:Adobe® Flash® Player Installer/Uninstaller 系统:windows xp sp3 调试器:win ...
- 运行 Tomcat, 在 Intellij IDEA 控制台输出中文乱码的解决方法
打开 Run/Debug Configurations → Tomcat Server → 要运行的 Tomcat → Server 页签,在 VM options 中输入: -Dfile.encod ...
- app开发流程有哪些
app开发流程是需求方和供求方相互协调的过程,一般分为需求分析.功能设计.功能实现.项目测试.上线等几个步骤,下面我们就来一起看看ytkah团队进行app开发各个流程主要做哪些事情,让您对app开发设 ...
- 003-centos搭建idea开发java
一.jdk安装 卸载openjdk 安装jdk 配置环境变量 二.下载idea 安装:http://www.cnblogs.com/bjlhx/p/6667291.html 三.配置git http: ...