JSP内置9大对象学习总结(一)

JSP中有9个内置对象,分别是:request、response、session、application、out、pageContext、config、page、exception。这些对象是由系统容器实现和管理的,在JSP页面中不需要定义即可直接使用。

一:request对象
request对象是javax.servlet.http.HttpServletRequest类型的对象,该对象代表了客户端的请求信息,主要用于接收通过HTTP协议传送到服务器端的数据(包括头信息、系统信息、请求方式以及请求参数等)。request对象的作用域为一次请求。
1. 获取请求参数值
<body>
        <a href=”text.jsp?id=007&name=tt”>获取请求参数的值</a>
</body>
可以通过下面方法获取参数
String id = request.getParameter(“id”);
String name = request.getParameter(“name”);
2. 解决中文乱码
如果上面传入的参数中含有中文,例如
<body>
        <a href=”text.jsp?id=007&name=涛涛”>获取请求参数的值</a>
</body>
我们获取的将是乱码,所有的request请求都是iso-8859-1编码,我们可以使用GBK,GB2312,UTF-8等编码。
解决方法为
String id = new String(request.getParameter(“id”).getBytes(“iso-8859-1”), ”GBK”);
String name = request.getParameter(“name”);
3. 获取表单提交数据
获取表单提交数据和获取请求参数方法基本是一样的,只不过我们是通过名字获取对象的值,在HTML页面当中,一组数据是可以同名的,也就是说同一个名字可以对应多个数据的,因此我们需要用
request.getParameter(name);
request.getParameterValues(name);
两种方法来获取数据。
4. 获取请求客户端信息
<html>
        <head>
               <meta http-equiv="Content-Type" content="x-download; charset=gbk" />
               <title>获取请求客户端信息</title>
        </head>
        <body>
               <ul style="line-height:200%">
                      <li> 客户使用的协议:<%=request.getProtocol() %> </li>
                      <li> 客户端发送请求的方式:<%=request.getMethod() %> </li>
                      <li> 客户端请求路径:<%=request.getContextPath() %> </li>
                      <li> 客户机IP地址:<%=request.getRemoteAddr() %> </li>
                      <li> 客户机名称:<%=request.getRemoteHost() %> </li>
                      <li> 客户机请求端口号:<%=request.getRemotePort() %> </li>
                      <li> 接收客户信息的页面:<%=request.getServletPath() %> </li>
                      <li> 获取URL:<%=request.getRequestURL() %> </li>
                      <li> 更多信息参考Javadoc </li>
               </ul>
        </body>
</html>
参考文档:
javax.servlet Interface ServletRequest
All Known Subinterfaces: HttpServletRequest
All Known Implementing Classes: HttpServletRequestWrapper, ServletRequestWrapper
5. 在作用域中管理属性

通过使用void setAttribute(String name, Object o)方法可以在request对象的属性列里面添加一个属性,通过Object getAttribute(String name)方法获取对应属性的值,可以通过void removeAttribute(String name)方法将该属性移除掉。(暂时不知道有什么用处?)

6. Cookie管理

 
Creates a cookie, a small amount of information sent by a servlet to a Web browser, saved by the browser, and later sent back to the server. A cookie's value can uniquely identify a client, so cookies are commonly used for session management.
Constructor SummaryCookie(String name, String value)
通过cookie的getCookies()方法可以获取到所有cookie对象集合,然后通过cookie对象的getName()方法获取指定名称的cookie,再通过getValue()方法可以获取cookie对象的值,将一个cookie对象发送到客户端使用了response对象的addCookie()方法。
比如一个简单的用户登录页面login.jsp提交给show.jsp页面,我们可以使用cookie保存用户的信息。
login.jsp
<html>
           <head>
                    <meta http-equiv="Content-Type" content="x-download; charset=gbk" />
                    <title>用户登录</title>
           </head>
           <body>
                    <%
                             String[] userInfo={"",""};
                             Cookie[] cookie=request.getCookies();
                             if(cookie!=null) {
                                       for(int i=0; i<cookie.length; i++) {
                                                if(cookie[i].getName().equals("cookieInfo")) {
                                                         userInfo = cookie[i].getValue().split("#");
                                                }
                                       }
                             }
                    %>
                    <form action="show.jsp" method="post">
                             姓名:<input name="name" type="text" value="<%=userInfo[0] %>"/>
                             密码:<input name="pwd" type="password" value="<%=userInfo[1] %>"/>
                             <input type="submit" value="登录"/>
                             <input type="reset" value="取消"/>
                    </form>
           </body>
</html>
 
show.jsp
<html>
           <head>
                    <meta http-equiv="Content-Type" content="x-download; charset=gbk" />
                    <title>登录验证</title>
           </head>
           <body>
                    <%
                             String name=request.getParameter("name");
                             String pwd=request.getParameter("pwd");
                             Cookie myCookie = new Cookie("cookieInfo", name+"#"+pwd);
                             myCookie.setMaxAge(60*60*24*7);
                             response.addCookie(myCookie);
                             response.flushBuffer();
                    %>
                    表单提交成功
                    您的名字是:<%=name %>。
           </body>
</html>
以上代码在保存英文字符的cookie时是没有问题的,当包含中文字符时会出现错误。
7. 获取浏览器使用的语言使用getLocal()方法。
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值