zoukankan      html  css  js  c++  java
  • JSP内置对象

    JSP提供了request对象,response对象,session对象,application对象,out对象,page对象,config对象,exception对象和pageContent对象等9个内置对象。

    其中session对象有效范围是当前整个会话。application对象的有效范围是当前应用。

    request对象:处理客户端提交的信息

    response对象:响应客户端请求信息

    session对象:在同一个应用中,在每个客户端的各个页面中共享数据

    application对象:在同一个应用程序中,各个用户共享数据。通常用在计数器或者是聊天室中。

    out对象:适用于向客户端输出各种类型的数据。

    page对象:适用于操作JSP页面自身。该对象在进行Web应用开发时,很少应用。

    config对象:适用于读取服务器的配置信息。

    exception对象:适用于操作JSP文件执行时发生的异常信息。

    一、request对象:封装了由客户端生成的HTTP请求的所有细节,包括主要的HTTP头信息、系统信息、请求方式和请求参数等。

    解决乱码问题:

    String username = new String(request.getParameter("id").getBytes("iso-8859-1"), "utf-8");

     通过cookie实现用户登陆:

    <%@page import="java.net.URLDecoder"%>
    <%@ page language="java" contentType="text/html; charset=UTF-8"
        pageEncoding="UTF-8"%>
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>Insert title here</title>
    </head>
    <body>
    <%
    Cookie[] cookies=request.getCookies();
    String user="";
    String date="";
    if(cookies!=null)
    {
        for(int i=0;i<cookies.length;i++)
        {
            if(cookies[i].getName().equals("mrCookie1"))
            {
                user=URLDecoder.decode(cookies[i].getValue().split("#")[0]);
                date=cookies[i].getValue().split("#")[1];
            }
        }
    }
            if("".equals(user)&&"".equals(date))
            {
                %>
                游客您好,欢迎光临!
                <form action="first.jsp" method="post">
                请输入姓名:<input name="user" type="text" value="">
                <input type="submit" value="确认">
                </form>
                <%
            }
            else
            {%>
                欢迎【<b><%=user %></b>】再次光临
                您注册的时间是【<b><%=date %></b>】
                <% }%>
    </body>
    </html>
    <%@page import="java.util.Date"%>
    <%@page import="java.net.URLEncoder"%>
    <%@ page language="java" contentType="text/html; charset=UTF-8"
        pageEncoding="UTF-8"%>
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>Insert title here</title>
    </head>
    <body>
    <%
    request.setCharacterEncoding("GB18030");
    String user=URLEncoder.encode(request.getParameter("user"), "GB18030");
    Cookie cookie=new Cookie("mrCookie1",user+"#"+new java.util.Date().toLocaleString());
    cookie.setMaxAge(60*60*24*1);
    response.addCookie(cookie);
    %>
    <script type="text/javascript">window.location.href="number_index.jsp"</script>
    </body>
    </html>

    二、response响应对象

    1、实现重定向:

    response.sendRedirect("index.jsp");
    

    2、通过response设置HTTP响应报文,其中,最常见的是设置响应的内容类型、禁用缓存、设置页面自动刷新、定时跳转;

    自动刷新:response.setHeader("refresh","10");

    定是跳转:response.setHeader("refresh","5;URL=login.jsp");

    3、设置缓冲区

    三、out输出对象:

    通过out对象,可以向客户端浏览器输出信息,并且管理应用服务器上的输出缓冲区。

  • 相关阅读:
    web 单例 多例
    python socket客户端
    foy: 轻量级的基于 nodejs 的通用 build 工具
    Hydux: 一个 Elm-like 的 全功能的 Redux 替代品
    AlarmManager使用注意事项
    【转】android ListView 几个重要属性
    自己写的小工具软件集合
    win8.1 cygwin编译java轻量虚拟机avian
    android 图片缩放抗锯齿
    windows phone和android,ios的touch事件兼容
  • 原文地址:https://www.cnblogs.com/huiqin126/p/6440458.html
Copyright © 2011-2022 走看看