可以通过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]+"&nbsp;&nbsp");
}
%>
<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对象详解的更多相关文章

  1. django中request对象详解(转载)

    django中的request对象详解 Request 我们知道当URLconf文件匹配到用户输入的路径后,会调用对应的view函数,并将  HttpRequest对象  作为第一个参数传入该函数. ...

  2. request对象详解

    先来了解一下Request的主要方法: setAttribute(String name,Object):设置名字为name的request的参数值getAttribute(String name): ...

  3. jsp request 对象详解

    转自:http://www.cnblogs.com/qqnnhhbb/archive/2007/10/16/926234.html 1.request对象 客户端的请求信息被封装在request对象中 ...

  4. JSP中Out和Request对象详解

    内置表示不需要new便可直接使用. 一.基础知识 1.缓冲区:IO最原始是一个一个字节的读取,这就像吃米饭的时候一粒一粒的吃,很没有效率,这时候就有了碗,一碗一碗的吃,岂不痛快. 2.Get提交不能超 ...

  5. django的views里面的request对象详解大全

    简介 HTTP 应用的信息是通过 请求报文 和 响应报文 传递的,关于更多的相关知识,可以阅读<HTTP权威指南>获得. 其中 请求报文 由客户端发送,其中包含和许多的信息,而 djang ...

  6. django中的request对象详解

    Request 我们知道当URLconf文件匹配到用户输入的路径后,会调用对应的view函数,并将  HttpRequest对象  作为第一个参数传入该函数. 我们来看一看这个HttpRequest对 ...

  7. Django_视图中的request对象详解(八)

    本文参考:http://www.cnblogs.com/MnCu8261/p/5871085.html Request 我们知道当URLconf文件匹配到用户输入的路径后,会调用对应的view函数,并 ...

  8. response对象和request对象详解

    request方法列举:request.getAuthType() // 获取保护servlet的认证方案名(BASIC或SSL),未受保护的servlet返回的就是nullrequest.getCh ...

  9. mvc-servlet---ServletConfig与ServletContext对象详解(转载)

    ServletConfig与ServletContext对象详解 一.ServletConfig对象    在Servlet的配置文件中,可以使用一个或多个<init-param>标签为s ...

随机推荐

  1. memcache 启动 failed to start

    以为是 端口冲突,到注册表中直接改了memcache的注册表,还是启动不了.memcache运行不了,还能咋办,看防火墙有没有阻止程序运行呗 勾上,我的windows 上的memcache 就可以运行 ...

  2. Android 真机无线调试

    有很多人在学Android的时候最开始接触的都是模拟机的测试,如果像好的模拟机比如genimotion,次一点的蓝手指,测试都还比较可以.有的也不缺乏是用真机测试.本人开始用华为真机测试,也是一直连线 ...

  3. 深度解析MySQL启动时报“The server quit without updating PID file”错误的原因

    很多童鞋在启动mysql的时候,碰到过这个错误, 首先,澄清一点,出现这个错误的前提是:通过服务脚本来启动mysql.通过mysqld_safe或mysqld启动mysql实例并不会报这个错误. 那么 ...

  4. Openstack & Ansible

    Opennstack Open source software for creating private and public clouds Manages the servers at these ...

  5. Json及Json字符串

    JSON(JavaScript Object Notation)是一种独立于开发语言的用于存储和交换文本数据的格式,JSON 语法是JavaScript 语法的子集. Json 可以保存数组格式和对象 ...

  6. 开涛spring3(12.1) - 零配置 之 12.1 概述

    12.1  概述 12.1.1  什么是零配置 在SSH集成一章中大家注意到项目结构和包结构是不是很有规律,类库放到WEB-INF/lib文件夹下,jsp文件放到WEB-INF/jsp文件夹下,web ...

  7. nginx源码分析——线程池

    源码: nginx 1.13.0-release   一.前言      nginx是采用多进程模型,master和worker之间主要通过pipe管道的方式进行通信,多进程的优势就在于各个进程互不影 ...

  8. Easy machine learning pipelines with pipelearner: intro and call for contributors

    @drsimonj here to introduce pipelearner – a package I'm developing to make it easy to create machine ...

  9. [oracle]Oracle数据库安全管理

    目录 +  1.数据库安全控制策略概述 +  2.用户管理 +  3.资源限制与口令管理 +  4.权限管理 +  5.角色管理 +  6.审计 1.数据库安全控制策略概述 安全性是评估一个数据库的重 ...

  10. PHP科普

    1.PHP是什么意思? 超文本预处理器(Hypertext Preprocessor) 2.PHP是干什么用的? PHP是一种通用开源脚本语言.语法吸收了C语言.Java和Perl(实际抽取与汇报语言 ...