request对象的方法及其参数的传递

 先设计一个简单的登录界面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

页面效果如图所示:

request对象的方法及其参数的传递_第1张图片

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

request对象的方法及其参数的传递_第2张图片

其中显示了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

你可能感兴趣的:(html,String,table,null,input,border)