jsp提供了9个内置对象,该对象会自动进行实例化操作
page 只在一个保存属性,跳转无效
request 一次请求保存属性,跳转依旧有效
session 同一会话有效
application 整个服务器上保存,所有用户都可使用
一个属性设置在本页上,跳转后无法获得
<%@ 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" %> <html> <head> <title>Title</title> </head> <body> <% // 设置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%> </body> </html>
服务器跳转后,属性会被继续保存
浏览器的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" %> <html> <head> <title>Title</title> </head> <body> <% request.setAttribute("name", "ming"); request.setAttribute("birthday", new Date()); %> <jsp:forward page="request_scope_02.jsp"/> </body> </html>
<%@ 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" %> <html> <head> <title>Title</title> </head> <body> <%=(String)request.getAttribute("name")%> <%=(Date)request.getAttribute("birthday")%> </body> </html>
当一个属性设置以后,任何一个与设置页面相关的页面都可以取得
即,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.*" %> <html> <head> <title>Title</title> </head> <body> <% // 设置session属性范围 session.setAttribute("name", "ming"); session.setAttribute("birthday", new Date()); %> <!-- 超链接跳转 --> <a href="session_scope_02.jsp">超级链接跳转</a> </body> </html>
<%-- 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.*" %> <html> <head> <title>Title</title> </head> <body> <% // 取出session属性 String username = (String)session.getAttribute("name"); Date userbirthday = (Date)session.getAttribute("birthday"); %> <%=username%> <%=userbirthday%> </body> </html>
此为公共参数
此属性保存在服务器上
<%@ 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" %> <html> <head> <title>Title</title> </head> <body> <% application.setAttribute("name", "ming"); application.setAttribute("birthday", new Date()); %> <!-- 超级链接跳转 --> <a href="./application_scope_02.jsp">超级链接获得属性</a> </body> </html>
<%@ 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" %> <html> <head> <title>Title</title> </head> <body> <% // application中获得属性 String username = (String)application.getAttribute("name"); Date userbirthday = (Date)application.getAttribute("birthday"); %> <%=username%> <%=userbirthday%> </body> </html>
接收客户端发送的请求,请求的参数,头部信息.
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> <form action="request_demo01.jsp" method="post"> <input type="text" name="info"> <input type="submit" value="submit"> </form> </body> </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" %> <html> <head> <title>Title</title> </head> <body> <% // 接收参数 String content = request.getParameter("info"); %> <%=content%> </body> </html>
用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" %> <html> <head> <title>Title</title> </head> <body> <form action="login.jsp" method="post"> 用户名 <input type="text" name="uname"/> 密码 <input type="password" name="upass"/> <input type="submit" value="登录"> </form> <% // 用户名 密码 // 获得name String name = request.getParameter("uname"); // 获得password String password = request.getParameter("upass"); // 进行用户名密码比对 if(!(name==null||"".equals(name) || password == null || "".equals(password)) ){ if("admin".equals(name) && "admin".equals(password)){ // 跳转 response.setHeader("refresh", "2;URL=welcome.jsp"); // 设置session session.setAttribute("userid", name); %> <h3>用户登录成功,两秒后将会跳转到欢迎页!</h3> <h3>如果没有跳转,点击<a href="./welcome.jsp">这里</a></h3> <% }else{ %> <h3>用户名密码错误</h3> <% } } %> </body> </html>
<%-- 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" %> <html> <head> <title>Title</title> </head> <body> <% // 设置两秒跳转 response.setHeader("refresh", "2;URL=login.jsp"); // 清除session session.invalidate(); %> <h3>成功退出本系统,两秒跳转回首页</h3> <h3>如果没有跳转,访问<a href="login.jsp">点我</a> </h3> </body> </html>
<%-- 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" %> <html> <head> <title>Title</title> </head> <body> <% // 判断此用户的session是否设置过 if(session.getAttribute("userid")!=null){ %> <h3>欢迎<%=session.getAttribute("userid")%></h3> <h3>注销登录<a href="./logout.jsp">点我</a></h3> <% }else{ %> <h3>非法访问</h3> <% } %> </body> </html>
使用isnew的方式,
原理,在第一次访问的时候,给客户端设置cokkie,然后再次访问的时候,会带上cokkie中的jsessionid,用来判断是否为新用户
使用getCreationTime获取第一个session创建的session时间,和最后一次操作的时间,用来判断秒数
用来获取serlet对象上下文 ServletContext表示整个容器的操作
使用表单输入要保存的文件名称和内容,直接在web项目的根目录的note文件夹中保存文件
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> <form action="./input_content.jsp" method="post"> 输入文件名称 <input type="text" name="filename"/><br/> 输入文件内容 <textarea name="filecontent" cols="30" rows="3"> </textarea> <input type="submit" value="保存"/> <input type="reset" value="重置"/> </form> </body> </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" %> <html> <head> <title>Title</title> </head> <body> <% // 接收保存的文件名称 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("<br>"); } // 关闭 scanner.close(); %> <%=stringBuffer%> </body> </html>