一、JSP概述
- JSP(JavaServer Pages)是一种基于Java的服务器端技术,用于创建动态生成的Web页面
- 允许开发人员将Java代码嵌入到HTML页面中,从而能够动态地生成内容
二、JSP执行流程
- 客户端请求JSP页面
- Web容器将JSP翻译成Servlet源代码
- 编译Servlet源代码生成.class文件
- 加载并执行Servlet
- 将结果返回给客户端
JSP最终会被编译成Servlet类,JSP可以轻松实现在html中加入Java代码
三、JSP脚本语法
脚本元素

JSP指令

JSP动作

三、JSP九大隐含对象

request请求对象
java
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>JSP隐含对象-request请求对象</title>
</head>
<body>
<h1>JSP隐含对象-request请求对象</h1>
<p>当前web应用的上下文路径为:<%=request.getContextPath()%></p>
<hr>
<h2>request对象调用getRequestURI()方法获取当前请求的资源路径</h2>
<p>当前请求的资源路径为:<%=request.getRequestURI()%></p>
<hr>
<h2>request对象调用getRequestURI()方法获取完整的URL</h2>
<p>当前请求的完整URL为:<%=request.getRequestURL()%></p>
<hr>
<h2>request对象调用getParameter()方法获取参数的值</h2>
<p>当前请求参数name的值为:<%=request.getParameter("name")%></p>
</body>
</html>
response响应对象
java
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>JSP隐含对象-response响应对象</title>
</head>
<body>
<h1>response响应对象</h1>
<h3>response对象可以调用getWriter/getOutputStream方法获取字符流/字节流,用于向客户端输出数据。</h3>
<%
//字节流只输出文本字符串
//response.getWriter().write("Hello World!");
//字节流可以输出任意字符数据,例如图片、视频、音频等
response.getOutputStream().write("abc".getBytes());
%>
<hr>
<h3>response对象addCookie方法可以向客户端添加Cookie。</h3>
<%
Cookie cookie = new Cookie("msg","success");
response.addCookie(cookie);
%>
<hr>
<h3>response对象sendRedirect方法重定向到其他页面。</h3>
<%
//资源跳转-请求重定向(浏览器的地址url会发生变化)
response.sendRedirect("demo1.jsp");
%>
</body>
</html>
JSP四大作用域对象

四、ShowUsersServlet优化
将所有用户的集合users存入到request作用域,转发到showUsers.jsp页面

