先设计一个简单的登录界面index.htm:

<html>
<head><title>request的使用</title></head>
<body bgcolor="#FFFFCC">
<center>
<table border="1">

<h1 align="center">登陆验证</h1>
<hr>
<form action="requestform.jsp">
<tr>
<td>用户名:</td>
<td><input type="text" name="username" size="20"></td>
</tr>
<tr>
<td>密&nbsp;码:</td>
<td><input type="password" name="password" size="20"></td>
</tr>
<tr>
<td align="center"><input type="submit" name="Submit" value="提交"></td>
<td align="center"><input type="reset" name="Submit2" value="重填"></td>
</form>
</table>
<hr>
<h3>演示request对象的方法及其参数的传递</h3>
</center>
</body></html>

输入信息后,提交到requestform.jsp页面,该页面用来显示一些参数:

<%@ page contentType="text/html; charset=gb2312" language="java" import="java.util.*" %>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<%
    request.setCharacterEncoding("GBK");
%>
<html>
<head><title>request对象的使用</title></head>
<body bgcolor="#FFFFCC">
<h3 align="center">request对象的使用</h3>
<center>
<table border="1" width="800">
<tr>
<td>HttpUtil.getRequestURL(request)</td>
<td><%=HttpUtils.getRequestURL(request)%></td>
</tr>
<tr>
<!--返回发送信息的方式-->
<td>request.getMethod()</td>
<td><%=request.getMethod()%></td>
</tr>
<tr>
<!--返回请求的URI字符串-->
<td>request.getRequestURL(request)</td>  
<td><%=request.getRequestURI()%></td>
</tr>
<tr>
<!--返回通信协议的方式-->
<td>request.getProtocol()</td>
<td><%=request.getProtocol()%></td>
</tr>
<tr>
<!--返回程序的相对路径和文件名称-->
<td>request.getServletPath()</td>
<td><%=request.getServletPath()%></td>
</tr>
<tr>
<!--返回程序的相对路径和文件名称-->
<td>request.getPathInfo()</td>
<td><%=request.getPathInfo()%></td>
</tr>
<tr>
<!--返回发送信息的方式-->
<td>request.getPathTranslated()</td>
<td><%=request.getPathTranslated()%></td>
</tr>
<tr>
<!--返回地址栏中后面的字符串-->
<td>request.getQueryString()</td>
<td><%=request.getQueryString()%></td>
</tr>
<tr>
<!--返回发送信息的方式-->
<td>request.getContentType()</td>
<td><%=request.getContentType()%></td>
</tr>
<tr>
<!--返回发送信息的方式-->
<td>request.getContentLength()</td>
<td><%=request.getContentLength()%></td>
</tr>
<tr>
<!--返回服务器主机名称-->
<td>request.getServerName()</td>
<td><%=request.getServerName()%></td>
</tr>
<tr>
<!--返回服务器主机连接的端口号-->
<td>request.getServerPort()</td>
<td><%=request.getServerPort()%></td>
</tr>
<tr>
<!--返回客户端用户的IP地址-->
<td>request.getRemoteAddr()</td>
<td><%=request.getRemoteAddr()%></td>
</tr>
<tr>
<!--返回返回客户端用户的主机名称-->
<td>request.getRemoteHost()</td>
<td><%=request.getRemoteHost()%></td>
</tr>
<tr>
<!--返回发送信息的方式-->
<td>request.getAuthType()</td>
<td><%=request.getAuthType()%></td>
</tr>
</table>
<h3 align="center">request.getHeaderNames()</h3>
<table border="1" width="800">
<%
    Enumeration enu1=request.getHeaderNames();
    while(enu1.hasMoreElements()){
        String names=(String)enu1.nextElement();
%>
<tr>
<!--返回发送信息的方式-->
<td><%=names%></td>
<td><%=request.getHeader(names)%></td>
</tr>
<%
    }
%>
</table>
<h3 align="center">getParameterNames()</h3>
<table border="1" width="800">
<%
    Enumeration enu2=request.getParameterNames();
    while(enu2.hasMoreElements()){
        String names=(String)enu2.nextElement();
%>
<tr>
<!--返回发送信息的方式-->
<td><%=names%></td>
<td><%=request.getParameter(names)%></td>
</tr>
<%
    }
%>
</table>
</table>
</center>
</body></html>

测试request对象的方法,以及传的参数的功能:

启动Tomcat服务器,在IE地址栏中键入URL为:

http://localhost:8080/sky2098/request/index.htm

页面效果如图所示:

我们可以随意输入参数,也可以是空值,则提交后页面为(我输入的用户名为sky2098,密码88888888):

其中显示了request对象的一些方法能够实现的功能,我们可以看到各个方法的实现以及参数的传递:

request对象的使用

HttpUtil.getRequestURL(request) http://localhost:8080/sky2098/request/requestform.jsp
request.getMethod() GET
request.getRequestURL(request) /sky2098/request/requestform.jsp
request.getProtocol() HTTP/1.1
request.getServletPath() /request/requestform.jsp
request.getPathInfo() null
request.getPathTranslated() null
request.getQueryString() username=sky2098&password=88888888&Submit=%CC%E1%BD%BB
request.getContentType() null
request.getContentLength() -1
request.getServerName() localhost
request.getServerPort() 8080
request.getRemoteAddr() 127.0.0.1
request.getRemoteHost() 127.0.0.1
request.getAuthType() null

request.getHeaderNames()

accept */*
referer http://localhost:8080/sky2098/request/index.htm
accept-language zh-cn
ua-cpu x86
accept-encoding gzip, deflate
user-agent Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; SV1; .NET CLR 1.1.4322)
host localhost:8080
connection Keep-Alive
cookie JSESSIONID=81EBF4B4973D85FBCC5C0EE2774D5413

getParameterNames()

password 88888888
Submit ?á??
username sky2098

request对象的方法及其参数的传递的更多相关文章

  1. 【java】值传递和引用传递---对象作为方法的参数传入属于哪种传递

    首先 这篇作为一个永久性的问题,欢迎大家讨论 其次,个人结论如下几条: ①Java有且只有一种传递,即 值传递 ②作为方法的参数传入,都是对原本的实参进行了copy ③只不过[实参]若是[基本数据类型 ...

  2. request对象多种方法封装表单数据

    public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, ...

  3. day65 request对象,以及方法,response对象,render,redirect

    这里的都是我们会频繁使用到的,用得多了自然就会了,我们写项目都是少不了这些用法的,所以这就把老师的博客粘过来就好了, Request对象 官方文档 属性 所有的属性应该被认为是只读的,除非另有说明. ...

  4. request对象的方法

    request对象封装的是请求的数据,由服务器创建,作为实参传递给Servlet的方法,一个请求对应一个request对象,request对象可以获得请求数据. 1.获取请求行信息 (1)get提交 ...

  5. 匿名对象作为方法的参数和返回值与Random概念和基本使用

    应用场景 1. 创建匿名对象直接调用方法,没有变量名. new Scanner(System.in).nextInt(); 2. 一旦调用两次方法,就是创建了两个对象,造成浪费,请看如下代码. new ...

  6. django 修改 request 对象中的请求参数, 并重新赋值给 request 对象

    直接上代码, 实现流程看代码及注释 def your_view(self, request): method = request.method if method == "GET" ...

  7. 小程序wx.request的POST方法的参数传输服务器接收不到

    这是API里面的例子: 而实际这样,服务端拿到的是空值. 将header更改一下,application/x-www-form-urlencoded,则可以让服务器收到数据

  8. JAVA-JSP内置对象之request对象参数

    相关资料:<21天学通Java Web开发> request对象1.request对象不但可以用来设置和取得requset范围变量,还可以用来获得客户端请求参数请求的来源.表头.cooki ...

  9. scrapy之Request对象

    我们在使用scrapy框架的时候,会经常疑惑,数据流是怎么样在各个组件中间传递的.最近经常用scrapy+selenium爬取淘宝,又因为今天周五心情好,本宝宝决定梳理一下这方面知识. scrapy中 ...

随机推荐

  1. Linux操作系统--定时任务

    最近在学习Linux操作系统.学到了关于定时任务的章节,作为一个总结写下这篇文章.在Linux中,我们可以将耗时大的任务如复制大文件,压缩.解压缩大文件等放进定时任务中(深夜执行,因为工作时间访问量大 ...

  2. window64位电脑如何通过VMware Workstation12.5.6安装苹果操作系统 macOS High Sierra 10.13

    1.下载 VMware-workstation-full-12.5.6.exe,macOS High Sierra 10.13.iso 2.安装 VMware-workstation时不要选择C盘,因 ...

  3. Server/Service/Engine/Host/Context} Setting property 'source' to 'org.eclipse.jst.jee

    双击 勾上蓝色保存

  4. Java - String 的字面量、常量池、构造函数和intern()函数

    一.内存中的 String 对象 Java 的堆和栈 对于基本数据类型变量和对象的引用,也就是局部变量表属于栈内存: 而通过 new 关键字和 constructor 创建的对象存放在堆内存: 直接的 ...

  5. Linux Collection:用户管理

    adduser 添加(新建)用户账户 $ sudo adduser username groups 添加组 $ groups username # 查看用户已有的组 $ groups username ...

  6. Django 【认证系统】auth

    本篇内容 介绍Django框架提供的auth 认证系统 方法: 方法名 备注 create_user 创建用户 authenticate 登录验证 login 记录登录状态 logout 退出用户登录 ...

  7. HBase 数据模型

    在HBase中,数据是存储在有行有列的表格中.这是与关系型数据库重复的术语,并不是有用的类比.相反,HBase可以被认为是一个多维度的映射. HBase数据模型术语 Table(表格) 一个HBase ...

  8. bat——批量删除文件文件夹

    bat批处理,在工作中会带来很多便利. 例如:想删除多个文件夹内的文件夹“Quality”及其子文件 同时删除所有Cyc*文件夹内的所有R00*.tif文件 则可如下操作 先建立父bat文件run_d ...

  9. 第1章 初始Docker容器

    1.1 什么是Docker slogan:Build Ship Run Any App Anywher.关键在于Ship,通过把程序和程序运行所需要的环境一起交付. Linux容器技术: Docker ...

  10. ORACLE 常见等待事件

    一. 等待事件的相关知识 1.1 等待事件主要可以分为两类,即空闲(IDLE)等待事件和非空闲(NON-IDLE)等待事件.1). 空闲等待事件指ORACLE正等待某种工作,在诊断和优化数据库的时候, ...