JSP的内置对象

  在JSP页面中不需要获取创建,可以直接使用的对象

  jsp一共有9个内置对象

  Request

  Response

    out:字节输出流对象,可以将数据输出到页面上,和Response.getWriter()类似

    Response.getWriter()和out.writer()的区别

    在tomcat服务器真正给客户端做出响应之前,会先找到Response缓存数据,在找out缓存区数据

    Response.getWriter()数据输出到永远在out.write()之前

 

 

 

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
  <head>
    <title>$Title$</title>
  </head>
  <body>
    <%
        System.out.println("hello jsp");
        int i =5;
        String contextPath = request.getContextPath();
        out.print(contextPath);
    %>
    <%!
        int i = 3;
    %>

    <%="hello"   %>

  <%
      response.getWriter().write("response");
  %>

  </body>
</html>

 

 

 

 

 

 

 

 

 

 

jsp案例_改造cookie案例

<%@ page import="java.net.URLDecoder" %>
<%@ page import="java.util.Date" %>
<%@ page import="java.text.SimpleDateFormat" %>
<%@ page import="java.net.URLEncoder" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
    <%
        Cookie[] cookies = request.getCookies();
        boolean flag = false;
        if (cookies !=null && cookies.length>0){
            for (Cookie cookie : cookies) {
                String name = cookie.getName();
                if ("lastTime".equals(name)){
                    flag = true;

                    String value = cookie.getValue();
                    System.out.println("解码前:"+value);
                    value = URLDecoder.decode(value, "utf-8");
                    System.out.println("解码后:"+value);

                    Date date = new Date();
                    SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");
                    String format = sdf.format(date);

                    System.out.println("编码前:"+format);
                    format = URLEncoder.encode(format, "utf-8");
                    System.out.println("编码后:"+format);

                    cookie.setValue(format);

                    cookie.setMaxAge(60*60*24*30);
                    response.addCookie(cookie);

                    %>
                       <h1>欢迎回来,您上次访问时间为<%=value%></h1>
                   <%


                    break;
                }
            }
        }
        if (cookies == null || cookies.length == 0 || flag == false){
            Date date = new Date();
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");
            String format = simpleDateFormat.format(date);

            System.out.println("编码前"+format);
            format = URLEncoder.encode(format,"utf-8");
            System.out.println("编码后"+format);

            Cookie cookie = new Cookie("lastTime",format);
            cookie.setValue(format);
            cookie.setMaxAge(60*60*24*30);

            response.addCookie(cookie);
    %>
               <h1>您好,欢迎您首页访问成功</h1>
        <span></span>
    <%
        }
    %>
</body>
</html>

 

 

 

posted on 2022-08-15 09:14  淤泥不染  阅读(18)  评论(0编辑  收藏  举报