Java后端——JSP技术

今天的课讲的是jsp的东西,在此做个记录。

几个重要的jsp语法:

1.<% %> java代码
2.<%! %> java声明,声明函数,声明变量
3.<%= %> java输出 特别重要
4.<%@ %> page指令
5.<%@ include file = …> include静态包含
6. (…)

示范案例:

写一个demo.jsp

<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Insert title here</title>
</head>
<body>
<%
   pageContext.setAttribute("name", "john"); //john为object对象
   request.setAttribute("name", "kate");
   session.setAttribute("name", "marry"); 
   application.setAttribute("name", "jack");
   String name = (String)pageContext.getAttribute("name");
   String name2 = (String)request.getAttribute("name");
   String name3 = (String)session.getAttribute("name");
   String name4 = (String)application.getAttribute("name");
   // out内置对象输出
   out.print(name+"<br/>");
   out.print(name2+"<br/>");
   out.print(name3+"<br/>");
   out.print(name4+"<br/>");
%>
</body>
</html>

输出结果:
john
kate
marry
jack
上述使用的是out内置对象输出。

内置对象:

9个基本的隐式对象(内置):
out,request(sendRedirect重定向到其他页面会null),response,session(客户端浏览器关一下,再打开就会null),config(服务器配置),application,page,pageContext(只作用在当前页面,getRequestDispatcher到其他页面会null),exception

也可以用jsp的输出方式,在上面的代码下方添加如下代码

<!-- jsp输出 -->
<%=name %><br/>
<%=name2 %><br/>
<%=name3 %><br/>
<%=name4 %><br/>

现在我们测试一下内置对象的局限性

创建另一个demo2.jsp

把demo1.jsp的取得资源的部分和输出部分注释掉,换成在demo2.jsp中输出

demo1.jsp

<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Insert title here</title>
</head>
<body>
<%
   pageContext.setAttribute("name", "john"); //john为object对象
   request.setAttribute("name", "kate");
   session.setAttribute("name", "marry"); 
   application.setAttribute("name", "jack");
   /* String name = (String)pageContext.getAttribute("name");
   String name2 = (String)request.getAttribute("name");
   String name3 = (String)session.getAttribute("name");
   String name4 = (String)application.getAttribute("name"); */
   // 请求转发
   request.getRequestDispatcher("demo2.jsp").forward(request,response); 
   // out内置对象输出
   /* out.print(name+"<br/>");
   out.print(name2+"<br/>");
   out.print(name3+"<br/>");
   out.print(name4+"<br/>");*/
%>
<%-- <!-- jsp输出 -->
<%=name %><br/>
<%=name2 %><br/>
<%=name3 %><br/>
<%=name4 %><br/> --%>
</body>
</html>

demo2.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<%
String name = (String)pageContext.getAttribute("name");
String name2 = (String)request.getAttribute("name");
String name3 = (String)session.getAttribute("name");
String name4 = (String)application.getAttribute("name");
out.print(name+"<br/>");
out.print(name2+"<br/>");
out.print(name3+"<br/>");
out.print(name4+"<br/>");
%>
</body>
</html>

输出结果:
null
kate
marry
jack

得知,pageContext(只作用在当前页面,getRequestDispatcher到其他页面会null)

随后把getRequestDispatcher请求转发改成response.sendRedirect重定向之后,再测试一下结果:
null
null
marry
jack

得知,request(sendRedirect重定向到其他页面会null)

我们再把页面的链接复制之后,客户端浏览器关掉再打开复制的链接,得到结果:
null
null
null
jack

得知,session(客户端浏览器关一下,再打开就会null),即session只要不关闭客户端都可以用


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

BeJav

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值