先设计一个简单的登录界面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. SQLServer之创建AFETER DELETE触发器

    DML AFTER DELETE触发器创建原理 触发器触发时,系统自动在内存中创建deleted表或inserted表,inserted表临时保存了插入或更新后的记录行,deleted表临时保存了删除 ...

  2. 阿里云上的Centos 7.6的一次Nginx+Mysql+PHP7.3 部署

    阿里云申请了一台服务器 Centos 7.6,每次安装都要上网找一大堆教程,因为不熟悉,因为总是忘记. 所以,有时间的时候,还是记录下自己的学习过程,有助于下次的问题解决. 我先总结下: 1)安装VS ...

  3. Hadoop从入门到精通系列之--0.Hadoop生态体系

    https://blog.csdn.net/Haidaiya/article/details/84568588#%E4%B8%80%20%E5%A4%A7%E6%95%B0%E6%8D%AE%E7%9 ...

  4. 多节点,多线程下发订单,使用zookeeper分布式锁机制保证订单正确接入oms系统

    假设订单下发, 采用单机每分钟从订单OrderEntry接口表中抓100单, 接入订单oms系统中. 由于双十一期间, 订单量激增, 导致订单单机每分钟100单造成, 订单积压. 所以采用多节点多线程 ...

  5. bzoj3122 [SDOI2013]随机数生成器

    bzoj3122 [SDOI2013]随机数生成器 给定一个递推式, \(X_i=(aX_{i-1}+b)\mod P\) 求满足 \(X_k=t\) 的最小整数解,无解输出 \(-1\) \(0\l ...

  6. Android测试(四):Instrumented 单元测试

    原文:https://developer.android.com/training/testing/unit-testing/instrumented-unit-tests.html Instrume ...

  7. 可视化工具Grafana:简介及安装

    随着业务的越发复杂,对软件系统的要求越来越高,这意味着我们需要随时掌控系统的运行情况.因此,对系统的实时监控以及可视化展示,就成了基础架构的必须能力. 这篇博客,介绍下开源的可视化套件grafana的 ...

  8. Java 200+ 面试题补充② Netty 模块

    让我们每天都能看到自己的进步.老王带你打造最全的 Java 面试清单,认真把一件事做到最好. 本文是前文<Java 最常见的 200+ 面试题>的第二个补充模块,第一模块为:<Jav ...

  9. 迄今为止 .Net 平台功能最强大,性能最佳的 JSON 序列化和反序列化库。

    Swifter.Json 这是迄今为止 .Net 平台功能最强大,性能最佳的 JSON 序列化和反序列化库. Github : https://github.com/Dogwei/Swifter.Js ...

  10. Django 之 admin管理工具

    -------------------------------------------------------------------------妄尝恶果,苦果自来. admin组件使用 Django ...