iii.代码脚本
代码脚本的格式是: <% 代码脚本 %>
代码脚本可以写以方法中可以写的任何代码。
代码脚本翻译之后都在_jspService方法中
1.代码脚本----if 语句
2.代码脚本----for 循环语句
3.翻译后java文件中_jspService方法内的代码都可以写
c)jsp中的三种注释
i.html注释
<!-- html注释 -->
翻译之后是out.write输出到客户端。
ii.java注释
// 单行注释
/* 多行注释 */
Java 注释 翻译之后会原封不动翻译到源代码中
iii.jsp注释
<%-- 这是jsp注释 --%>
jsp注释 在翻译的时候,会被忽略掉
7.jsp九大内置对象
8.jsp四大域对象
pageContext ====>>>> request ====>>>>> session =====>>>>> application
域对象 数据操作范围
pageContext 当前jsp页面
request 同一次请求
session 同一次会话(打开浏览器,访问服务器之后会话就打开了,只要浏览器不关。会话都在)
application 一个web工程
四个域的数据操作范围从小到大分别是:pageContext====>>>request====>>>session=====>>>>application
四个域都可以用来保存数据,如何挑选使用?
四个域从小到大进行优先选择。
Context1.jsp页面
<body> <h1>Context1.jsp页面</h1> <% pageContext.setAttribute("key", "pageContextData"); request.setAttribute("key", "requestData"); session.setAttribute("key", "sessionData"); application.setAttribute("key", "applicaionData"); %> pageContext域:<%=pageContext.getAttribute("key") %><br/> request域:<%=request.getAttribute("key") %><br/> session域:<%=session.getAttribute("key") %><br/> application域:<%=application.getAttribute("key") %><br/> <% // request.getRequestDispatcher("/Context2.jsp").forward(request, response); %> </body>
Context2.jsp页面
<body> <h1>Context2.jsp页面</h1> pageContext域:<%=pageContext.getAttribute("key") %><br/> request域:<%=request.getAttribute("key") %><br/> session域:<%=session.getAttribute("key") %><br/> application域:<%=application.getAttribute("key") %><br/> </body>
9.jsp中的out输出和response.getWriter输出的区别
现在我们都知道out和response都可以往页面上输出数据。
通过观察jsp翻译之后的源码我们发现,都是使用out进行输出。所以我们以后也是统一使用out进行输出操作。
out.write 可以输出字符串
out.print 可以输出任意数据
深入浅出:统一使用out.print进行输出
10.jsp的常用标签
a)jsp静态包含
<%-- <%@ include file="" %> 是静态包含。 静态包含地址中打头的斜杠, 表示http://ip:port/工程名/ 映射到代码的WebContent目录 静态包含,其实本质上,只是把被包含的jsp页面的内容。原封不动的拷贝到被包含的位置执行。 静态包含,不会翻译被包含的jsp文件。 --%> <%@ include file="/include/footer.jsp" %>
b)jsp标签-动态包含
<%-- <jsp:include page=""></jsp:include> 是动态包含 动态包含地址中打头的斜杠, 表示http://ip:port/工程名/ 映射到代码的WebContent目录 动态包含,会把被包含的jsp页面也翻译成为servlet程序。 动态包含,会翻译成为如下语句: JspRuntimeLibrary.include(request, response, "/include/footer.jsp", out, false); 等价于把request,response,out对象传递给footer.jsp所翻译出来的Servlet去执行使用。 动态包含,还可以传递参数 --%> <jsp:include page="/include/footer.jsp"> <jsp:param value="wzg168" name="username"/> <jsp:param value="123456" name="password"/> </jsp:include>
动态包含底层原理:
c)jsp标签-转发
<jsp:forward page="/Context2.jsp"></jsp:forward>
请求转发功能,
跟request.getRequestDispatcher("/Context2.jsp").forward(request, response);代码功能完全一样
11.静态包含和动态包含的区别
随着整个javaEE技术的不断升级,那么jsp这种技术,在整个javaEE体系中的定位慢慢发生变化。
jsp的定位慢慢就变成了,只是用来输出html页面数据而已。所以一般情况下。都使用静态包含。
jsp的练习题
练习一:在jsp中输出10*10的表格
<%@ 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> <style type="text/css"> table{ width: 500px; border: 1px solid red; border-collapse: collapse; } th , td{ border: 1px solid red; } </style> </head> <body> <table> <% for (int i = 1; i <= 10; i++) { %> <tr> <% for (int j = 1; j <= 10; j++) { %> <td><%=i + "," + j %></td> <% } %> </tr> <% } %> </table> </body> </html>
练习二:在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> <center> <h1>九九乘法口诀表</h1> <table width="600"> <% for(int i = 1; i < 10; i++){ %> <tr> <% for(int j = 1; j <= i ; j++){ %> <td><%=j + "x" + i + "=" + (j*i) %></td> <% } %> </tr> <% } %> </table> </center> </body> </html>
练习三:jsp输出一个表格,里面有20个学生信息。
请求转发的流程示例:
Student类
public class Student { private Integer id; private String name; private String phone; private String email; public Student(Integer id, String name, String phone, String email) { super(); this.id = id; this.name = name; this.phone = phone; this.email = email; } public Student() { super(); }
SearchStduent程序
public class SearchStudent extends HttpServlet { private static final long serialVersionUID = 1L; protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // 获取请求参数 // 查询学生信息 // 得到多个学生信息 List<Student> stus = new ArrayList<Student>(); for (int i = 1; i < 21; i++) { stus.add(new Student(i,"name"+i, "phone"+i,"email"+i)); } // 把学生信息保存到request域中 request.setAttribute("stus", stus); // 请求转发到jsp页面中输出数据 request.getRequestDispatcher("/test/showStudent.jsp").forward(request, response); } }
showStudent.jsp页面
<%@page import="java.util.ArrayList"%> <%@page import="com.atguigu.pojo.Student"%> <%@page import="java.util.List"%> <%@ 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> <style type="text/css"> table{ width: 500px; border: 1px solid red; border-collapse: collapse; } th , td{ border: 1px solid red; } </style> </head> <body> <% List<Student> stus = (List<Student>) request.getAttribute("stus"); %> <table> <tr> <th>编号</th> <th>姓名</th> <th>电话</th> <th>邮箱</th> <th>操作</th> </tr> <% for (int i = 0; i < stus.size(); i++) { %> <% Student stu = stus.get(i); %> <tr> <td><%=stu.getId() %></td> <td><%=stu.getName() %></td> <td><%=stu.getPhone() %></td> <td><%=stu.getEmail() %></td> <td>修改、删除</td> </tr> <% } %> </table> </body> </html>
12、什么是Listener监听器?
Listener监听器是JavaWeb的三大组件之一,
三大组件分别是:Servlet程序、Filter过滤器、Listener监听器
Listener监听器,顾名思义,它监听某个事物状态变化,然后反馈给用户信息。
1、监听事物状态变化
2、反馈用户结果
12.1、ServletContextListener监听器
ServletContextListener监听器,监听ServletContext对象的创建和销毁。
ServletContext是在web工程启动的时候创建,在web工程停止的时候销毁
如何使用ServletContextListener监听器,步骤如下:
1、编写一个类去实现ServletContextListener监听器接口
2、到web.xml中去配置监听器
ServletContextListenerImpl 代码:
public class ServletContextListenerImpl implements ServletContextListener { /** * ServletContext对象创建并初始化就马上调用此contextInitialized方法 */ @Override public void contextInitialized(ServletContextEvent sce) { System.out.println("ServletContext对象被创建啦啦啦…………"); } /** * 当ServletContext对象被销毁之后,就会马上调用contextDestroyed方法 */ @Override public void contextDestroyed(ServletContextEvent sce) { System.out.println("ServletContext对象被销毁啦啦啦…………"); } }
web.xml中的配置:
<!-- listener标签配置监听器 --> <listener> <!-- listener-class配置监听器的全类名 --> <listener-class>com.atguigu.listener.ServletContextListenerImpl</listener-class> </listener>