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>
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 分享4款.NET开源、免费、实用的商城系统
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
· 上周热点回顾(2.24-3.2)