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 ...
随机推荐
- JavaScript开发中几个常用知识点总结
最近在做项目的时候自己写了一些JavaScipt代码,于是自己又进行简单的查阅资料整理了一下,发现了如下几个比较有用的知识点: 1.三种声明函数的方式 2.jQuery $(document).rea ...
- nodejs6下使用koa2
koa2里面使用ES7的语法,如async.await所以需要运行在node7.6之后:但在node7.6之前也可以利用babel是的koa2可以运行. 首先项目中安装babel,和babel的几个模 ...
- React-Router 4 的新玩意儿
上一个项目用的还是 2.6.1,转眼的功夫 4.0 都发布了,API 变化实在有点大,2.X那套东西不顶用了,老老实实重新看一遍文档,其中有几点需要注意的,拿出来说一说. 本文只讨论针对浏览器的应用, ...
- 【JAVAWEB学习笔记】17_jsp
动态页面技术(JSP/EL/JSTL) 学习目标 案例:完成商品的列表的展示 一.JSP技术 1.jsp脚本和注释 jsp脚本: 1)<%java代码%> ----- 内部的java代码翻 ...
- MIME协议在邮件中的应用详解
1.定义 全称是多用途互联网邮件扩展(MIME,Multipurpose Internet Mail Extensions),在MIME出台之前,使用RFC 822只能发送基本的ASCII码文本信息, ...
- 掌握Docker命令
1.管理镜像命令 获取镜像 docker push ubuntu:14:04 查看镜像列表 docker images 重命名image docker tag IMAGE-NAME NEW-IMAGE ...
- HDU1829(种类并查集)
ps:本来是想找个二分图判断的题来写,结果百度到这鬼题 Problem Description Background Professor Hopper is researching the sexua ...
- 如何使用华为软件开发云快速部署PHP网站
华为软件开发云这个工具,从去年推出我就一直在关注,毕竟是华为最新的一款软件开发工具,最近我一直在使用华为软件开发云进行开发项目管理,它有在线编译和构建.云端在线代码检查等功能,编译省去了很多物理机器的 ...
- 关于JAVA自带MD5的方法
有空再详细解释 import java.security.MessageDigest; public class MD5 { public final static String MD51(Strin ...
- Angular Route导航
我们用Angular cli创建带有路由的新项目 ng new router --routing Angular Routes API文档 Angular的文档中有详细的解释: 1)https://a ...