先设计一个简单的登录界面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. (办公)面试java设计模式

      1.单例模式: 程序开发的时候,有些对象只能有一个.有实例,且只有一个,比如工具类. 修改构造方法为私有的. 饿汉模式: 线程安全 创建一个实例 Private Static 实例; 提供一个静态 ...

  2. 06-Nodejs介绍

    06-Nodejs介绍 打开Nodejs英文网:https://nodejs.org/en/ 中文网:http://nodejs.cn/ 我们会发现这样一句话: 翻译成中文如下: Node.js 是一 ...

  3. Python第五天 文件访问 for循环访问文件 while循环访问文件 字符串的startswith函数和split函数 linecache模块

    Python第五天   文件访问    for循环访问文件    while循环访问文件   字符串的startswith函数和split函数  linecache模块 目录 Pycharm使用技巧( ...

  4. centos6.5上进行crontab操作

    1.service crond start 2. vi  /home/cron.ini */ * * * * /home/monitor.sh 3.crontab  /home/cron.ini OK

  5. Apollo的Oracle适配改动

    这几天工作需要使用Apollo配置中心.Apollo唯一的依赖是MySQL数据库,然而公司只有Oracle数据库资源.这里有一个Oracle适配改动的分支,但是它是基于0.8.0版本的Apollo.看 ...

  6. Docker 教程(一)

    Docker 使用客户端-服务器 (C/S) 架构模式,使用远程API来管理和创建Docker容器. Docker 容器通过 Docker 镜像来创建. 容器与镜像的关系类似于面向对象编程中的对象与类 ...

  7. jdbc链接数据库

    JDBC简介 JDBC全称为:Java Data Base Connectivity (java数据库连接),可以为多种数据库提供填统一的访问.JDBC是sun开发的一套数据库访问编程接口,是一种SQ ...

  8. How Cigna Tuned Its Spark Streaming App for Real-time Processing with Apache Kafka

    Explore the configuration changes that Cigna’s Big Data Analytics team has made to optimize the perf ...

  9. 高速排序,归并排序,堆排序python实现

    高速排序的时间复杂度最好情况下为O(n*logn),最坏情况下为O(n^2),平均情况下为O(n*logn),是不稳定的排序 归并排序的时间复杂度最好情况下为O(n*logn),最坏情况下为O(n*l ...

  10. Java 通过地址获取经纬度 - 高德地图

    一.添加依赖 <dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-v ...