<% // 设置page属性 pageContext.setAttribute("name", "ming"); pageContext.setAttribute("birtday", new Date()); %><% // 重page中取出属性 String username = (String)pageContext.getAttribute("name"); Date userbirthday = (Date)pageContext.getAttribute("birtday"); %> 姓名<%=username%> 生日<%=userbirthday%>
服务器跳转后,属性会被继续保存
浏览器的URL地址会发生改变
<%@ page import="java.util.Date" %><%-- Created by IntelliJ IDEA. User: ming Date: 19-3-10 Time: 下午1:02 To change this template use File | Settings | File Templates. --%> <%@ page contentType="text/html;charset=UTF-8" language="java" %>
<% request.setAttribute("name", "ming"); request.setAttribute("birthday", new Date()); %>
<%@ page import="java.util.*" %><%-- Created by IntelliJ IDEA. User: ming Date: 19-3-10 Time: 下午1:29 To change this template use File | Settings | File Templates. --%> <%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%=(String)request.getAttribute("name")%><%=(Date)request.getAttribute("birthday")%>
当一个属性设置以后,任何一个与设置页面相关的页面都可以取得
即,session,session属于服务器端保存.
cokice 属于客户端保存
<%-- Created by IntelliJ IDEA. User: ming Date: 19-3-10 Time: 下午3:07 To change this template use File | Settings | File Templates. --%> <%@ page contentType="text/html;charset=UTF-8" language="java" %> <%@ page import="java.util.*" %>
<% // 设置session属性范围 session.setAttribute("name", "ming"); session.setAttribute("birthday", new Date()); %> 超级链接跳转
<%-- Created by IntelliJ IDEA. User: ming Date: 19-3-10 Time: 下午3:07 To change this template use File | Settings | File Templates. --%> <%@ page contentType="text/html;charset=UTF-8" language="java" %> <%@ page import="java.util.*" %>
<% // 取出session属性 String username = (String)session.getAttribute("name"); Date userbirthday = (Date)session.getAttribute("birthday"); %><%=username%><%=userbirthday%>
此为公共参数
此属性保存在服务器上
<%@ page import="java.util.Date" %><%-- Created by IntelliJ IDEA. User: ming Date: 19-3-10 Time: 下午10:17 To change this template use File | Settings | File Templates. --%> <%@ page contentType="text/html;charset=UTF-8" language="java" %>
<% application.setAttribute("name", "ming"); application.setAttribute("birthday", new Date()); %> 超级链接获得属性
<%@ page import="java.util.Date" %><%-- Created by IntelliJ IDEA. User: ming Date: 19-3-10 Time: 下午10:20 To change this template use File | Settings | File Templates. --%> <%@ page contentType="text/html;charset=UTF-8" language="java" %>
<% // application中获得属性 String username = (String)application.getAttribute("name"); Date userbirthday = (Date)application.getAttribute("birthday"); %><%=username%><%=userbirthday%>
接收客户端发送的请求,请求的参数,头部信息.
<!DOCTYPE html>
<%-- Created by IntelliJ IDEA. User: ming Date: 19-3-10 Time: 下午11:54 To change this template use File | Settings | File Templates. --%> <%@ page contentType="text/html;charset=UTF-8" language="java" %>
<% // 接收参数 String content = request.getParameter("info"); %><%=content%>
用getParameterNames
使用getHeaderNames()
额…依旧没啥东东
学过
定时跳转属于客户端跳转
额..依旧没啥
在response中调用addCookie
需要注意的是会返回一个jsessionid
当服务器端使用session的时候,可以保存在redis中
会有一个不重复的编号,即session id
cookie中保存的jsessionid为同样道理
实现思路,设置session范围的属性,需要验证的页面进行判断session
即,保存用户的信息,使用session进行保存
<%-- Created by IntelliJ IDEA. User: ming Date: 19-3-11 Time: 下午9:17 To change this template use File | Settings | File Templates. --%> <%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%-- Created by IntelliJ IDEA. User: ming Date: 19-3-11 Time: 下午10:06 To change this template use File | Settings | File Templates. --%> <%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%-- Created by IntelliJ IDEA. User: ming Date: 19-3-11 Time: 下午9:59 To change this template use File | Settings | File Templates. --%> <%@ page contentType="text/html;charset=UTF-8" language="java" %>
使用isnew的方式,
原理,在第一次访问的时候,给客户端设置cokkie,然后再次访问的时候,会带上cokkie中的jsessionid,用来判断是否为新用户
使用getCreationTime获取第一个session创建的session时间,和最后一次操作的时间,用来判断秒数
用来获取serlet对象上下文 ServletContext表示整个容器的操作
使用表单输入要保存的文件名称和内容,直接在web项目的根目录的note文件夹中保存文件
<!DOCTYPE html>
<%@ page import="java.io.File" %> <%@ page import="java.io.PrintStream" %> <%@ page import="java.io.FileOutputStream" %> <%@ page import="java.util.Scanner" %> <%@ page import="java.io.FileInputStream" %><%-- Created by IntelliJ IDEA. User: ming Date: 19-3-11 Time: 下午10:46 To change this template use File | Settings | File Templates. --%> <%@ page contentType="text/html;charset=UTF-8" language="java" %>
<% // 接收保存的文件名称 String name = request.getParameter("filename"); // 接收文件内容 String content = request.getParameter("filecontent"); // 获得文件名称 String fileName = this.getServletContext().getRealPath("/") + "note" + File.separator + name; // 获得文件对象 File file = new File(fileName); // 用于判断父文件夹是否存在 if(!file.getParentFile().exists()){ // 创建文件夹 file.getParentFile().mkdir(); } // 定义打印流对象 PrintStream printStream = null; // 创建一个到文件的输入流 printStream = new PrintStream(new FileOutputStream(file)); // 往流中输入内容 printStream.println(content); // 关闭流 printStream.close(); %><% // 通过Scanner获取流的输入 Scanner scanner = new Scanner(new FileInputStream(file)); // 设置读取分隔符 scanner.useDelimiter("n"); // 新建缓冲区 StringBuffer stringBuffer = new StringBuffer(); // 读取内容,保存进入缓冲区 while(scanner.hasNext()){ stringBuffer.append(scanner.next()).append("
未分类
上一篇文章
下一篇文章
© 2020mySoul
主题设计师: Anders Norén