JSP学习入门3

130 阅读2分钟

开启掘金成长之旅!这是我参与「掘金日新计划 · 12 月更文挑战」的第3天,点击查看活动详情

session共享问题

session就是一个全局变量,注销一处,处处失效

示例:

login.jsp内容

 <%@ page language="java" contentType="text/html; charset=UTF-8"
     pageEncoding="UTF-8"%>
 <!DOCTYPE html>
 <html>
 <head>
 <meta charset="UTF-8">
 <title>Insert title here</title>
 </head>
 <body>
     <%!
         String myName;
     %>
     <%
     
         Cookie[] cookies=request.getCookies();
         for(Cookie cookie:cookies){
             if(cookie.getName().equals("uname")){
                 myName=cookie.getValue();
             }
         }
         
     %>
 ​
     <form action="check.jsp" method="post">
         用户名:<input type="text" name="uname" value="<%= (myName==null?"":myName)%>"/><br /> 
         密码:<input type="password" name="upwd" /><br /> 
         <input type="submit" value="提交" />
 ​
     </form>
 ​
 </body>
 </html>

check.jsp内容

 <%@ page language="java" contentType="text/html; charset=UTF-8"
     pageEncoding="UTF-8"%>
 <!DOCTYPE html>
 <html>
 <head>
 <meta charset="UTF-8">
 <title>Insert title here</title>
 </head>
 <body>
     <%
         request.setCharacterEncoding("utf-8");
     String name = request.getParameter("uname");
     String pwd = request.getParameter("upwd");
     if (name.equals("zs") && pwd.equals("abc")) {
         session.setAttribute("myname", name);
         session.setAttribute("mypwd", pwd);
         
         request.getRequestDispatcher("welcom.jsp").forward(request, response);
     } else {
         out.print("用户名或密码有误");
     }
     %>
 ​
 ​
 ​
 </body>
 </html>

welcom.jsp内容

 <%@ page language="java" contentType="text/html; charset=UTF-8"
     pageEncoding="UTF-8"%>
 <!DOCTYPE html>
 <html>
 <head>
 <meta charset="UTF-8">
 <title>Insert title here</title>
 </head>
 <body>
     <h1>登录成功!欢迎你😊</h1>
     <%
         String name = (String) session.getAttribute("myname");
     if (name != null) {
         out.print(name + "欢迎你");
     %>
     
     <a href="invali.jsp">注销</a>
     
     <%
         } else {
         response.sendRedirect("login.jsp");
     }
     %>
 ​
 </body>
 </html>

A.jsp内容

 <%@ page language="java" contentType="text/html; charset=UTF-8"
     pageEncoding="UTF-8"%>
 <!DOCTYPE html>
 <html>
 <head>
 <meta charset="UTF-8">
 <title>Insert title here</title>
 </head>
 <body>
     <%
         String name=(String)session.getAttribute("myname");
         out.print(name);
     %>
     
 </body>
 </html>

invali.jsp内容

 <%@ page language="java" contentType="text/html; charset=UTF-8"
     pageEncoding="UTF-8"%>
 <!DOCTYPE html>
 <html>
 <head>
 <meta charset="UTF-8">
 <title>Insert title here</title>
 </head>
 <body>
     <%
         session.invalidate();   //使当前session失效
         response.sendRedirect("login.jsp");
 //      session.removeAttribute("myname"); 只删除某一个属性
     %>
 </body>
 </html>

session与Cookie问题及application

示例(证明sessionId和jsession是同一个id):

login.jsp内容

 <body>
     <form action="check.jsp" method="post">
         用户名:<input type="text" name="uname"/><br /> 
         密码:<input type="password" name="upwd" /><br /> 
         <input type="submit" value="提交" />
     </form>
 </body>

check.jsp内容

 <body>
     <%
         request.setCharacterEncoding("utf-8");
         String name = request.getParameter("uname");
         String pwd = request.getParameter("upwd");
         if (name.equals("zs") && pwd.equals("abc")) {
             session.setAttribute("myname", name);
             session.setAttribute("mypwd", pwd);
             System.out.println("sessionId:"+session.getId());//在控制台输出sessionId的值
             Cookie cookie=new Cookie("myname",name);
             response.addCookie(cookie);
     
             request.getRequestDispatcher("welcom.jsp").forward(request, response);
         } else {
             out.print("用户名或密码有误");
         }
     %>
 </body>

welcom.jsp内容

 <body>
     <h1>登录成功!欢迎你😊</h1>
     <%
         String name = (String) session.getAttribute("myname");
     if (name != null) {
         out.print(name + "欢迎你");
     %>
     
     <a href="invali.jsp">注销</a>
     
     <%
         } else {
         response.sendRedirect("login.jsp");
     }
     %>
 </body>

invali.jsp内容

 <body>
     <%
         session.invalidate();   //使当前session失效
         response.sendRedirect("login.jsp");
 //      session.removeAttribute("myname"); 只删除某一个属性
     %>
 </body>

A.jsp内容

 <body>
     <%
         out.print(session.getAttribute("myname"));
         Cookie[] cookies=request.getCookies();
         for(Cookie cookie:cookies){//在控制台输出JSESSIONID的值
             if(cookie.getName().equals("JSESSIONID")){
                 System.out.println("JSESSIONID:"+cookie.getValue());
             }
         }
     %>
     
 </body>

cookie和session的区别

sessioncookie
保存的位置服务端客户端
安全性较为安全较不安全
保存的内容ObjectString

application全局对象

String getContextPath() 虚拟路径

String getRealPath(String name) 绝对路径(虚拟路径相对的绝对的路径)

 <body>
     <%="当前项目的虚拟路径:"+application.getContextPath() %><br/>
     <%="虚拟路径的绝对路径:"+application.getRealPath("/MyJspProject") %>
 </body>

cookie补充说明及四种范围对象作用域

cookie补充:

服务端在第一次响应客户端时,会发送一个JSESSIONID的cookie

客户端在第一次请求服务端时,如果服务端发现此请求没有JSESSIONID,则会创建一个name=JSESSIONID的cookie,并返回给客户端

Cookie:

a.不是内置对象,要使用必须new

b.但是,服务端会自动生成一个(服务端自动new一个cookie)name=JSESSIONID的cookie并返回给客户端

四种范围对象(小->大)

pageContext JSP页面容器(page对象) 当前页面有效

request 请求对象 同一次请求有效

session 会话对象 同一次会话有效

application 全局对象 全局有效(整个面目有效)

以上4个对象共有的方法:

Object getAttribute(String name):根据属性名,获得属性值

void setAttribute(String name,Object obj):设置属性值(新增,修改)

setAttribute("a","b");(如果a对象之前不存在,则新建一个a对象;如果a之前已经存在,则将a的值改为b)

void removeAttribute(String name):根据属性名,删除对象

a

pageContext 当前页面有效(页面跳转后无效)

b.

request 同一次请求有效;其他请求无效(请求转发后有效;重定向后无效)

c.

session 同一次会话有效(无论怎么跳转,都有效;关闭/切换浏览器后无效;从 登陆->退出 之间全部有效)

d.

application 全局变量;整个项目运行期间都有效(切换浏览器仍然有效);关闭服务、其他项目无效

->多个项目共享、重启后仍然有效:JNDI

1.以上4个范围对象,通过setAttribute()赋值,通过getAttribute()取值;

2.以上范围对象,尽量使用最小的范围。因为对象的范围越大,造成的性能损耗越大。