zoukankan      html  css  js  c++  java
  • jsp九大内置对象

    一、JSP内置对象:JSP内置对象是 Web 容器创建的一组对象,不用通过手动new就可以使用

    二、九大内置对象:

      1.out   

    <%
        int[] value = { 60, 70, 80 };
        for (int i : value) {
             //将输出信息输出到控制台
            System.out.println("控制台:"+i);
             //out作为JSP最简单的内置对象,主要用于将信息输出到页面上
            out.println(i);        
        }
    %>

       2.request:客户端向服务器端发送请求数据,我们通过request对象接收数据

    <%
                //获取数据之前解决乱码   解决表单POST提交方式的乱码
                request.setCharacterEncoding("UTF-8");
    
                //request用于获取客户端向服务器提交的数据
                String username=request.getParameter("username");
                String password=request.getParameter("password");
                
                //获取表单组件对应多个值时的请求数据
                String [] hobbys=request.getParameterValues("hobby");
                for(int i=0;i<hobbys.length;i++){
                    //获取数据之前解决乱码   解决表单Get提交方式的乱码
                    /* hobbys[i]=new String(hobbys[i].getBytes("ISO-8859-1"),"UTF-8"); */
                    out.print(hobbys[i]);
                }
                out.println();
                out.println(username+"	"+password);
                
                //性别
                String sex=request.getParameter("sex");
                out.println(sex);
                
                //下拉框地址
                String address=request.getParameter("address");
                out.println(address);
            %>

      3.response:响应
        转发(是在服务器内部进行,无法访问到除内部以外的资源):request.getRequestDispatcher("/response/welcome.jsp").forward(request, response);
        重定向(全路径):response.sendRedirect("/Chap02/response/Login.jsp");
        区别:1.转发是在服务器内部进行,重定向是客户端完成的,需要写入全路径,地址xxxxxxx
           2.转发请求1次,其余的操作都是在服务器内部进行的;重定向请求至少2次,其余的请求命令客户端再次请求一个URL
           3.转发可以携带这次请求的数据,重定向不带数据

            深入理解:https://blog.csdn.net/lijun102542/article/details/78518733

      4.session:会话;在一段时间内,客户端和服务器建立连接的过程,只要会话时间不过期,只要会话不关闭,那么会话就一直存在,那么会话中保存的数据就一直存在

      不同浏览器的会话是不相等的
        可以设置Session的会话时长:
        1.session.setMaxInactiveInterval(秒数)
        2.在项目web.xml当中去设置Session的实效时长:
          <session-config>
          <session-timeout>1</session-timeout>
          </session-config>
        手动设置Session实效:
          session.invalidate();

      会话保存数据:
        
    session.setAttribute(String name, Object value);
        Object value=session.getAttribute(String name);
      
    删除会话当中保存的数据:
        
    session.removeAttribute(String name);

      5.application   

        application实现用户之间的数据共享
        void setAttribute(String key,Object value)  以key/value的形式保存对象值
        Object getAttribute(String key)  通过key获取对象值
        String getRealPath(String path)  返回相对路径的真实路径

    //统计网站访问次数:
                <%
                    //获取当前网站的访问次数
                    Integer count=(Integer)application.getAttribute("count");
                    if(count!=null){
                        count++;
                    }else{
                        count=1;
                    }
                    application.setAttribute("count", count);
                %>
                <%
                    out.print("当前网站访问次数:"+application.getAttribute("count"));
                
                %>

      6.page:页面对象

      7.pageContext:上下文对象

      8.config:配置对象

      9.exception:异常

    三、四大作用域:

       从小到大:page(pageContext)--->request---->session---->application
        page对象作用域只在当前页面有效
        request作用域是在当前请求内有效
        session作用域是在当前会话内有效
        application对象的作用域是在应用上下文

    四、cookie

      由服务器端生成并发送给客户端浏览器,作用在客户端,默认会存放一个session的id值

      在jsp中使用cookie:

        1.创建cookie对象:

          Cookie ck=new Cookie(key,value);

        2.将cookie对象加入cookie中

          response.addCookie(ck)

        3.读取cookie

          Cookie [] cookie=request.getCookies();

          for(int i=0; i<cookie.lenght,i++){

            System.out.println(cookie[i].getName());

            //System.out.println(cookie[i].getValue());

          }

    五、session 和 cookie区别

        http://www.voidcn.com/article/p-bxdwejmm-or.html

    九大内置对象总结图:

  • 相关阅读:
    POJ 1401 Factorial
    POJ 2407 Relatives(欧拉函数)
    POJ 1730 Perfect Pth Powers(唯一分解定理)
    POJ 2262 Goldbach's Conjecture(Eratosthenes筛法)
    POJ 2551 Ones
    POJ 1163 The Triangle
    POJ 3356 AGTC
    POJ 2192 Zipper
    POJ 1080 Human Gene Functions
    POJ 1159 Palindrome(最长公共子序列)
  • 原文地址:https://www.cnblogs.com/xiao-ran/p/11255420.html
Copyright © 2011-2022 走看看