request内置对象介绍和使用

request对象

     主要用于接收客户端发送而来的请求信息,客户端的请求信息被封装在request对象中,通过它才能了解到客户的需求,然后做出响应。它是HttpServletRequest类的实例。

 

序号 方 法 说 明 
1   object getAttribute(String name) 返回指定属性的属性值 
2   Enumeration getAttributeNames() 返回所有可用属性名的枚举 
3   String getCharacterEncoding() 返回字符编码方式 
4   int getContentLength() 返回请求体的长度(以字节数) 
5   String getContentType() 得到请求体的MIME类型 
6   ServletInputStream getInputStream() 得到请求体中一行的二进制流 
7   String getParameter(String name) 返回name指定参数的参数值 
8   Enumeration getParameterNames() 返回可用参数名的枚举 
9   String[] getParameterValues(String name) 返回包含参数name的所有值的数组 如checkbox这种多选框可用此方法
10   String getProtocol() 返回请求用的协议类型及版本号 
11   String getScheme() 返回请求用的计划名,如:http.https及ftp等 
12   String getServerName() 返回接受请求的服务器主机名 
13   int getServerPort() 返回服务器接受此请求所用的端口号 
14   BufferedReader getReader() 返回解码过了的请求体 
15   String getRemoteAddr() 返回发送此请求的客户端IP地址 
16   String getRemoteHost() 返回发送此请求的客户端主机名 
17   void setAttribute(String key,Object obj) 设置属性的属性值 
18   String getRealPath(String path) 返回一虚拟路径的真实路径 
19   void setCharacterEncoding(String code)用于设置传递的字符编码,可用于解决乱码问题
20     
      

 

 

 

 

 

 

 

<%@ page contentType="text/html;charset=gb2312"%>
<%request.setCharacterEncoding("gb2312");%>
<html>
<head>
<title>request对象_例1</title>
</head>
<body bgcolor="#FFFFF0">
<form action="" method="post">
   <input type="text" name="qwe">
   <input type="submit" value="提交">
</form>
请求方式:<%=request.getMethod()%><br>
请求的资源:<%=request.getRequestURI()%><br>
请求用的协议:<%=request.getProtocol()%><br>
请求的文件名:<%=request.getServletPath()%><br>
请求的服务器的IP:<%=request.getServerName()%><br>
请求服务器的端口:<%=request.getServerPort()%><br>
客户端IP地址:<%=request.getRemoteAddr()%><br>
客户端主机名:<%=request.getRemoteHost()%><br>
表单提交来的值:<%=request.getParameter("qwe")%><br>
</body>
</html> 
<%@ page contentType="text/html;charset=gb2312"%>
<%request.setCharacterEncoding("gb2312");%>
<%@ page import="java.util.Enumeration"%>
<html>
<head>
   <title>request对象_例2</title>
</head>
<body bgcolor="#FFFFF0">
<form action="" method="post">
    用户名:<input type="text" name="username">&nbsp;&nbsp;
    密 码:<input type="text" name="userpass">&nbsp;&nbsp;
    <input type="submit" value="进入" >
</form>
<%
String str="";
if(request.getParameter("username")!=null && request.getParameter("userpass")!=null){
    Enumeration enumt = request.getParameterNames();
    while(enumt.hasMoreElements()){
       str=enumt.nextElement().toString();
       out.println(str+":"+request.getParameter(str)+"<br>");
    }
}
%>
</body>
</html> 
<%@ page contentType="text/html;charset=gb2312"%>
<%request.setCharacterEncoding("gb2312");%>
<html>
<head>
    <title>request对象_例3</title>
</head>
<body bgcolor="#FFFFF0">
<form action="" method="post">
   擅长:<input type="checkbox" name="cb" value="ON1">VC++&nbsp;
        <input type="checkbox" name="cb" value="ON2">JAVA&nbsp;
        <input type="checkbox" name="cb" value="ON3">DELPHI&nbsp;
        <input type="checkbox" name="cb" value="ON4">VB&nbsp;
        <br>
        <input type="submit" value="进入" name="qwe">
</form>
<%
if(request.getParameter("qwe")!=null ){
    for(int i=0;i<request.getParameterValues("cb").length;i++){
       out.println("cb"+i+":"+request.getParameterValues("cb")[i]+"<br>");
    }
    out.println(request.getParameter("qwe"));
}
%>
</body>
</html> 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值