5.request对象详解

可以通过request对象获取表单提交的值,get或者post方式都是可以得
例子:login.jsp表单
<%@ page language="java" import="java.util.*"
contentType="text/html; charset=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 'login.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>用户注册</h1>
<hr>
<form action="request.jsp" name="regForm" method="post">
<table>
<tr>
<td>用户名:</td>
<td><input type="text" name="username" />
</td>
</tr>
<tr>
<td>爱好:</td>
<td>
<input type="checkbox" name="favorite" value="read" />读书
<input type="checkbox" name="favorite" value="music" />音乐
<input type="checkbox" name="favorite" value="movie" />电影
<input type="checkbox" name="favorite" value="internet" />上网
</td>
</tr>
<tr>
<td colspan="2"><input type="submit" value="注册"></td>
</tr>
</table>
</form>
</body>
</html>
2.request.jsp接收表单的内容并且打印出来
<%@ page language="java" import="java.util.*" contentType="text/html; charset=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 'login.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>request内置对象</h1>
<%
request.setCharacterEncoding("utf-8");
%>
用户名<%=request.getParameter("username") %><br>
爱好<%
String[] favorites = request.getParameterValues("favorite");
for(int i = 0;i<favorites.length;i++){
out.println(favorites[i]+"  ");
}
%>
<hr>
</body>
</html>
3.效果如下:


注意编码的问题:在request接收的时候需要设置编码,否则中文会乱码
request.setCharacterEncoding("utf-8");
4.也可以通过简单的url传递参数而不通过提交表单的方式
<a href = "request.jsp?username=cai" >URL传参</a>
获取的时候一样通过request的方法
<%=request.getParameter("username") %>
但是url传递参数会出现中文乱码的问题,要解决需要打开tomcat目录下的conf的server.xml
在这里添加一句,改成这样子
<Connector port="8080" protocol="HTTP/1.1"
connectionTimeout="20000"
redirectPort="8443" URIEncoding="utf-8"/>
重启tomcat服务器即可
5.可以在request对象中保存一些属性,以键值对的形式存在
设置password的值是123456
<% request.setAttribute("password","123465");%>
获取password的值
<%=request.getAttribute("password")%>
6.其他的函数
获取请求体的MIME类型:<%=request.getContentType()%><br>
返回请求用的协议类型以及版本号: <%=request.getProtocol()%><br>
返回接受请求的服务器主机名: <%=request.getServerName()%><br>
服务器端口号 :<%=request.getServerPort()%><br>
返回的编码格式 :<%=request.getCharacterEncoding()%><br>
请求文件的长度 :<%=request.getContentLength()%><br>
请求客户端的IP地址 <%=request.getRemoteAddr()%><br>
请求的真实路径: <%=request.getRealPath("request.jsp")%><br>
请求的上下文路径: <%=request.getContextPath()%><br>
结果:

5.request对象详解的更多相关文章
- django中request对象详解(转载)
django中的request对象详解 Request 我们知道当URLconf文件匹配到用户输入的路径后,会调用对应的view函数,并将 HttpRequest对象 作为第一个参数传入该函数. ...
- request对象详解
先来了解一下Request的主要方法: setAttribute(String name,Object):设置名字为name的request的参数值getAttribute(String name): ...
- jsp request 对象详解
转自:http://www.cnblogs.com/qqnnhhbb/archive/2007/10/16/926234.html 1.request对象 客户端的请求信息被封装在request对象中 ...
- JSP中Out和Request对象详解
内置表示不需要new便可直接使用. 一.基础知识 1.缓冲区:IO最原始是一个一个字节的读取,这就像吃米饭的时候一粒一粒的吃,很没有效率,这时候就有了碗,一碗一碗的吃,岂不痛快. 2.Get提交不能超 ...
- django的views里面的request对象详解大全
简介 HTTP 应用的信息是通过 请求报文 和 响应报文 传递的,关于更多的相关知识,可以阅读<HTTP权威指南>获得. 其中 请求报文 由客户端发送,其中包含和许多的信息,而 djang ...
- django中的request对象详解
Request 我们知道当URLconf文件匹配到用户输入的路径后,会调用对应的view函数,并将 HttpRequest对象 作为第一个参数传入该函数. 我们来看一看这个HttpRequest对 ...
- Django_视图中的request对象详解(八)
本文参考:http://www.cnblogs.com/MnCu8261/p/5871085.html Request 我们知道当URLconf文件匹配到用户输入的路径后,会调用对应的view函数,并 ...
- response对象和request对象详解
request方法列举:request.getAuthType() // 获取保护servlet的认证方案名(BASIC或SSL),未受保护的servlet返回的就是nullrequest.getCh ...
- mvc-servlet---ServletConfig与ServletContext对象详解(转载)
ServletConfig与ServletContext对象详解 一.ServletConfig对象 在Servlet的配置文件中,可以使用一个或多个<init-param>标签为s ...
随机推荐
- [Angularjs]$http.post与$.post
摘要 在angularjs发送post请求的时候,确实很困惑,在传递json数据的时候,总会遇到在服务端无法接受到参数的情况,这里有必要与$.post进行比较学习一下. 一个例子 这里模拟登录的一个场 ...
- [Open Source] RabbitMQ 高可用集群方案
简介 RabbitMQ是用erlang开发的,集群非常方便,因为erlang天生就是一门分布式语言,但其本身并不支持负载均衡. Rabbit模式大概分为以下三种:单一模式.普通模式.镜像模式 Rabb ...
- iOS最新企业证书的生成
PS:不知道什么原因 图片显示不出来 请看我简书里面的文章 http://www.jianshu.com/p/baab03ac43e9 1.生成CSR文件 SpotLight搜索>钥匙串访问 ...
- 开涛spring3(3.1) - DI的配置使用
3.1.1 依赖和依赖注入 传统应用程序设计中所说的依赖一般指“类之间的关系”,那先让我们复习一下类之间的关系: 泛化:表示类与类之间的继承关系.接口与接口之间的继承关系: 实现:表示类对接口的实现 ...
- Jedis-returnResource使用注意事项
遇到过这样一个严重问题: 发布的项目不知从什么时候开始,每月会出现一两次串号问题.串号现象指的是,用户用账号A登录系统,然后某个时间,登录账号自动变成了B. 串号出现的时间不定,测试平台难以重现,且后 ...
- How to use data analysis for machine learning (example, part 1)
In my last article, I stated that for practitioners (as opposed to theorists), the real prerequisite ...
- Comparing the contribution of NBA draft picks(转)
When it comes to the NBA draft, experts tend to argue about a number of things: at which position wi ...
- SpringMVC——数据校验
数据校验在web应用里是非常重要的功能,尤其是在表单输入中.在这里采用Hibernate-Validator进行校验,该方法实现了JSR-303验证框架支持注解风格的验证. 一.导入jar包 若要实现 ...
- Javascript事件模型(二):Javascript事件的父元素和子元素
DOM事件标准定义了两种事件流,分别是捕获和冒泡.默认情况下,事件使用冒泡事件流,不使用捕获事件流.你可以指定使用捕获事件流,方法是在注册事件时传入useCapture参数,将这个参数设为true. ...
- JS闭包,以及适用场景
闭包的定义 不用解释了,网上到处都是.简单的说:一个定义在函数内部的函数与包含它的外部函数构成了闭包,内部函数可以访问外部函数的变量,这些变量将一直保存在内存中,直到无法再引用这个内部函数 举个例子: ...