可以通过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. Java7中的ForkJoin并发框架初探(下)—— ForkJoin的应用

    前两篇文章已经对Fork Join的设计和JDK中源码的简要分析.这篇文章,我们来简单地看看我们在开发中怎么对JDK提供的工具类进行应用,以提高我们的需求处理效率. Fork Join这东西确实用好了 ...

  2. Erlang使用ProtoBuffer

    最近有工作需要打算为项目服务器做一个机器人,测试测试压力,根据自己的经验,使用Erlang来做是最合适不过的了,但是服务器使用的C++语言,使用了Google的ProtoBuffer作为协议进行数据交 ...

  3. Linux学习第三步(Centos7安装mysql5.7数据库)

    版本:mysql-5.7.16-1.el7.x86_64.rpm-bundle.tar 前言:在linux下安装mysql不如windows下面那么简单,但是也不是很难.本文向大家讲解了如何在Cent ...

  4. swift学习 - tableView自适应高度1(xib autoLayout)

    tableView自适应高度 效果图: 源码: class ViewController: UIViewController,UITableViewDelegate,UITableViewDataSo ...

  5. 使用Git上传项目代码到github

    github是一个基于Git的代码托管平台,付费用户可以建私人仓库,我们一般的免费用户只能使用公共仓库,也就是代码要公开.这对于一般人来说公共仓库就已经足够了.   注册账户以及创建仓库 要想使用gi ...

  6. 第三章(附)mysql表类型MyISAM和InnoDB区别(决定了是否支持事务)

    mysql表类型MyISAM和InnoDB区别 MyISAM:这个是默认类型,它是基于传统的ISAM类型,ISAM是Indexed Sequential Access Method (有索引的顺序访问 ...

  7. Creating beautiful charts in chinese with ggplot2

    Before we start My chinese skills are poor and biased. I did learn during my internship and I contin ...

  8. Handling Class Imbalance with R and Caret - An Introduction

    When faced with classification tasks in the real world, it can be challenging to deal with an outcom ...

  9. java 与操作系统同步问题(三)————父亲儿子女儿水果问题

    问题描述:父亲每次都会放一个水果在桌子上,女儿喜欢吃香蕉(只吃香蕉), 儿子喜欢吃苹果(只吃苹果).父亲每次只会随机往桌子上放一个水果(苹果或香蕉),儿子,女儿会来取.使用p.v操作来完成父亲.儿子. ...

  10. 基于cookie使用过滤器实现客户每次访问自登陆一次

    原创声明:本文为本人原创作品,绝非他处摘取,转载请联系博主 相信大家在各大网站都会遇到,登录时,在登录框出现下次免登陆/一个月免登陆的类似选项,本次博文就是讲解如何实现,在这记录一下,也算是做个备忘录 ...