Javaweb request对象 , 以及一些操作

来源:互联网 发布:126邮箱smtp端口号 编辑:程序博客网 时间:2024/05/30 12:03
JSP request 对象 :
      request中包含了HTTP 请求中的所有的细节 
      包含 : HTTP 请求头信息
             系统信息
             请求方式
             请求参数(GET 和 POST方式的参数都是一样的)  
             请求转发

             等





# GET 和 POST 数据的的获取
#请求转发时的属性的获取

<%@ page language="java" pageEncoding="UTF-8" ContentType="text/html;charset=utf-8" %><%-- 1. 获取请求参数 --%><%   //获取表单的信息   String username = "";   String password = "";   if( null == request.getParameter("hidden") ){       username = request.getParameter("username");       password = request.getParameter("password");%><html>   <head>     <title></title>   </head>   <body>     <fieldset>       <legend>用户登录</legend>     </fieldset>    <form name="form1" action="" ,method="POST" >    用户名 : <input type="text" name="username"/>    密码: <input type="password" name="password"/>    <input type="hidden" name="hidden" value="login"/>    <input type="submit" value="登录"/>    </form>   </body><%  }else{%><%=username%> 你好 ,登录成功;</html



#请求转发时的属性的获取   # 1.send_attribute操作<%@ page language="java" pageEncoding="UTF-8" ContentType="text/html;charset=utf-8" %><html>   <head>     <title></title>   </head>   <body><%   request.setAttribute("attr1","这是属性1");%> <jsp:forward page="get_attribute.jsp">      </body></html>   # 2 .get_attribute.jsp<%@ page language="java" pageEncoding="UTF-8" ContentType="text/html;charset=utf-8" %><html>   <head>     <title></title>   </head>   <body><%  String attrValue =  request.getAttribute("attr1","这是属性1");%> <%="attr1 : "+attrVAlue %>   </body></html>



#获取Cookie里面数据 : cookie.getCookies() 返回是一个Cookie 对象的集合<%@ page language="java" pageEncoding="UTF-8" ContentType="text/html;charset=utf-8" %><html>   <head>     <title></title>   </head>   <body><%  Cookies cookies[] = request.getCookies();  if(cookie !=null){     for(int i=0;i<cookies.length;++i){            String cookieName = cookies[i].getName();            String cookieValue = URLDecoder.decode(cookies[i].getValue);%><!--输出信息--><%=cookieName+"   :   "+cookieValue%><%          }    }%>   </body></html>




#获取客户端的信息<%@ page language="java" pageEncoding="UTF-8" ContentType="text/html;charset=utf-8" %><html>   <head>     <title></title>   </head>   <body><%  //获取请求的方式  String method = request.getMethod();    //获取协议信息  String protocol = request.getProtocol();  //获取请求字符串的地址  String URL = request.getRequestURL();  //获取请求字符串的地址  String URI = request.getRequestURI();  //获取当前文件的绝对路径  String realPath = request.getRealPath("index.jsp");    //获取客户端IP  String ip = request.getRemoteAddr();  //获取主机名称  String host = request.getRemoteHost();  //获取服务器的名字  String serverName = request.getServerName();  //获取客户端请求的脚本的路径  String clientPath = request.getServerPath();    //获取服务器端口号  String port = request.getServerPort();%> <jsp:forward page="get_attribute.jsp">      </body></html>




# 根据 request.getLocale 获取的locale对象来 判断<%@ page language="java" pageEncoding="UTF-8" ContentType="text/html;charset=utf-8" %><html>   <head>     <title></title>   </head>   <body><%  java.util.Locale locale = request.getLocale();  //显示中文  String str = "";  if(locale.equals(java.util.Local.CHINA)){      str = "这是我的web程序";  }//end if  //显示英文  if(locale.equals(java.util.Local.US)){      str = "this is my web application";  }//end if%> <%=str%><jsp:forward page="get_attribute.jsp">      </body></html>

原创粉丝点击