先设计一个简单的登录界面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. mssql 存储过程调用另一个存储过程中的结果的方法分享

    转自:http://www.maomao365.com/?p=6801 摘要: 下文将分享"一个存储过程"中如何调用"另一个存储过程的返回结果",并应用到自身的 ...

  2. c/c++ 重载运算符 ==和!=的重载

    重载运算符 ==和!=的重载 问题:假如有一个类似于vector的类,这个类只能存放string,当有2个这个类的对象时,如何比较这2个对象. 自己重载==和!= 代码(重载==,!=) #inclu ...

  3. js 学习之路6: if...else...条件语句的使用

    1.1 if (...) { ... } else { ... } <!DOCTYPE html> <html> <meta http-equiv="Conte ...

  4. MPLAB X IDE调试仿真功能简单入门

    仿真分为硬件仿真和软件仿真,这里的硬件仿真和软件仿真的区别,就不多说了,相信大家都听说过这两个概念. 我这里想给大家介绍的是“Set PC at Cursor”--“设置PC到光标处”这个功能,这个功 ...

  5. addq

    <template> <el-row id="AddRoom"> <el-col :xs="0" :sm="2" ...

  6. Linux systemtap定位系统IO资源使用情况(ok)

    一.systemtap介绍 SystemTap是一个强大的调试工具,是监控和跟踪运行中的Linux 内核的操作的动态方法,确切的说应该是一门调试语言,因为它有自己的语法,也有解析.编译.运行等过程(准 ...

  7. jquery中数组对象下面的属性名名是动态的如何获取

    <script> let normalListData = []; function temp() { for (var i = 0; i < 10; i++) { let rowC ...

  8. 更多的贴片SOT-23三极管,请点击以下表格购买。

    更多的贴片SOT-23三极管,请点击以下表格购买. 型号 标识 电流 V数 极性 封装 购买链接 S9012 2T1 0.3A 20V PNP SOT-23 点击购买 S9013 J3 0.3A 25 ...

  9. Linux基础(一)

    01-服务器 1.1 服务器型号 1.2 电源---双电源 1.3 CPU---计算,(2个CPU=2路) 1.4 内存 面试题:bufffer和cache的区别? buffer:写入数据到内存里,这 ...

  10. 关于gitee代码上传下载

    1.在gitee上面创建新分支: 2.复制本地ssh秘钥(C:\Users\Administrator\.ssh) 添加到 gitee设置页面的ssh:(如果之前没有秘钥,就执行ssh-keygen ...