自己怎么做电影网站吗,明星网站设计,上海企业登记一网通办,网站信息安全保障制度建设情况1 JSP注释
1.1 显示注释
显示注释会出现在生成的HTML文档中#xff0c;对用户可见。
!-- 这是一个HTML显示注释 --
1.2 隐式注释
隐式注释不会出现在生成的HTML文档中#xff0c;对用户不可见。
%-- 这是一个JSP隐式注释 --%
2 JSP脚本元素
2.1 局部…1 JSP注释
1.1 显示注释
显示注释会出现在生成的HTML文档中对用户可见。
!-- 这是一个HTML显示注释 --
1.2 隐式注释
隐式注释不会出现在生成的HTML文档中对用户不可见。
%-- 这是一个JSP隐式注释 --%
2 JSP脚本元素
2.1 局部变量和语句
使用% %编写局部变量和语句。
% page contentTypetext/html;charsetUTF-8 languagejava %
!DOCTYPE html
html
headtitleJSP脚本元素/title
/head
body%String message 欢迎来到JSP世界;%h1% message %/h1
/body
/html
2.2 全局变量、方法和类
使用%! %声明全局变量、方法和类。
% page contentTypetext/html;charsetUTF-8 languagejava %
!DOCTYPE html
html
headtitleJSP脚本元素/title
/head
body%!public String getMessage() {return 欢迎来到JSP世界;}%h1% getMessage() %/h1
/body
/html
2.3 表达式
使用% %输出表达式的值。
% page contentTypetext/html;charsetUTF-8 languagejava %
!DOCTYPE html
html
headtitleJSP脚本元素/title
/head
bodyh1当前时间% new java.util.Date() %/h1
/body
/html
2.4 JSP Include
jsp:include动作用于动态包含另一个JSP页面的内容。这对于避免重复代码非常有用例如在多个页面中包含相同的头部和尾部内容。
% page contentTypetext/html;charsetUTF-8 languagejava %
!DOCTYPE html
html
headtitleJSP Include/title
/head
bodyjsp:include pageheader.jsp /p这是主页面的内容。/pjsp:include pagefooter.jsp /
/body
/html
3 动态包含
动态包含是指在一个JSP页面中动态地包含另一个JSP页面的内容。这种包含方式是动态的因为被包含的页面只有在请求到来时才会被加载。
3.1 特点 包含与被包含的页面是独立的它们各自有自己的生命周期和作用域。 可以出现同名变量。由于每个页面都有自己的作用域因此即使变量名相同也不会发生冲突。 动态包含可以传递参数这使得被包含的页面可以根据传递的参数动态地生成内容。
3.2 示例
假设我们有两个JSP页面main.jsp 和 included.jsp。
main.jsp
% page languagejava contentTypetext/html; charsetUTF-8 pageEncodingUTF-8%
!DOCTYPE html
html
head
meta charsetUTF-8
titleDynamic Include Example/title
/head
bodyh1Main Page/h1jsp:include pageincluded.jsp /
/body
/html
included.jsp
% page languagejava contentTypetext/html; charsetUTF-8 pageEncodingUTF-8%
h2Included Page/h2
在这个例子中当用户访问 main.jsp 时服务器会动态地包含 included.jsp 的内容。
4 四大作用域
在JSP中有四种不同类型的作用域它们决定了变量的生命周期和可见性。
4.1 Page作用域 生命周期仅限于当前页面。 可见性只在当前页面内可见。 当页面跳转时Page作用域内的变量将失效。
4.2 Request作用域 生命周期一次HTTP请求。 可见性在整个请求处理过程中可见包括转发和包含操作。 超链接跳转后Request作用域内的变量仍然有效。
4.3 Session作用域 生命周期一次用户会话。 可见性在整个用户会话期间可见。 当用户关闭浏览器或会话超时后Session作用域内的变量将失效。
4.4 Application作用域 生命周期整个Web应用程序的生命周期。 可见性在整个Web应用程序中可见。 只有在应用程序重启后Application作用域内的变量才会失效。
4.5 示例代码
使用不同作用域的变量
% page languagejava contentTypetext/html; charsetUTF-8 pageEncodingUTF-8%
!DOCTYPE html
html
head
meta charsetUTF-8
titleScope Example/title
/head
body% // Page作用域的变量String pageVar This is a page scope variable.;// Request作用域的变量request.setAttribute(requestVar, This is a request scope variable.);// Session作用域的变量session.setAttribute(sessionVar, This is a session scope variable.);// Application作用域的变量application.setAttribute(applicationVar, This is an application scope variable.);%h1Page Scope Variable:/h1% pageVar %h1Request Scope Variable:/h1% request.getAttribute(requestVar) %h1Session Scope Variable:/h1% session.getAttribute(sessionVar) %h1Application Scope Variable:/h1% application.getAttribute(applicationVar) %
/body
/html
5 案例代码
5.1 编写代码
5.1.1 后端验证
创建一个简单的用户类User.java
public class User {private String username;private String password;
public User(String username, String password) {this.username username;this.password password;}
public String getUsername() {return username;}
public String getPassword() {return password;}
}
创建一个ServletLoginServlet.java来处理登录请求
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.IOException;
public class LoginServlet extends HttpServlet {private User user new User(admin, password); // 模拟用户数据
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {String username request.getParameter(username);String password request.getParameter(password);
if (user.getUsername().equals(username) user.getPassword().equals(password)) {HttpSession session request.getSession();session.setAttribute(user, username);response.sendRedirect(index.jsp);} else {response.sendRedirect(login.jsp?errortrue);}}
}
5.1.2 前端登录页面
创建一个HTML文件login.html作为登录页面
!DOCTYPE html
html langen
headmeta charsetUTF-8meta nameviewport contentwidthdevice-width, initial-scale1.0titleLogin/title
/head
bodyh1Login/h1form actionLoginServlet methodpostlabel forusernameUsername:/labelinput typetext idusername nameusername requiredbrlabel forpasswordPassword:/labelinput typepassword idpassword namepassword requiredbrinput typesubmit valueLogin/form% if (request.getParameter(error) ! null) { %p stylecolor: red;Invalid username or password./p% } %
/body
/html
5.1.3 首页显示用户名
创建一个JSP文件index.jsp作为首页
% page contentTypetext/html;charsetUTF-8 languagejava %
!DOCTYPE html
html
headmeta charsetUTF-8titleIndex/title
/head
bodyh1Welcome to the Index Page/h1% if (session.getAttribute(user) ! null) { %pHello, % session.getAttribute(user) %!/p% } else { %pPlease login first./p% } %a hreflogin.htmlLogin/a
/body
/html
5.1.4 配置Web应用程序 在web.xml文件中配置LoginServlet
web-appservletservlet-nameLoginServlet/servlet-nameservlet-classLoginServlet/servlet-class/servletservlet-mappingservlet-nameLoginServlet/servlet-nameurl-pattern/LoginServlet/url-pattern/servlet-mapping
/web-app 将login.html、index.jsp、User.java和LoginServlet.java文件放置在正确的目录下。
6 EL表达式Expression Language
EL表达式是JSP的一部分用于简化数据访问和操作。它允许开发者在JSP页面中直接访问JavaBean属性、集合元素以及请求作用域属性。
6.1 EL表达式的基本用法 使用${}来包裹要访问的变量或属性。 EL表达式默认从pageScope开始查找属性如果没有找到会继续查找requestScope、sessionScope和applicationScope。 如果找不到对应的属性EL表达式会返回空字符串而不是抛出异常。
6.2 EL表达式获取值 List通过索引访问列表元素如${list[0]}。也可以使用${list.size()}来获取列表的大小但注意这不是EL标准用法应使用JSTL的c:forEach配合varStatus属性。 Map通过键访问映射元素如${map.key}。 JavaBean通过属性名访问JavaBean属性如${bean.propertyName}。
7 JSTLJavaServer Pages Standard Tag Library
JSTL是一组标准的JSP标签库用于简化JSP页面的开发。
7.1 核心标签库 条件判断c:if标签用于条件判断但没有else部分。 循环c:forEach标签用于遍历集合。
7.2 格式化标签库 格式化日期fmt:formatDate标签用于格式化日期。
7.3 使用taglib引入标签库 使用% taglib %指令引入标签库并通过prefix属性指定前缀。
7.4 案例代码
7.4.1 EL表达式示例
% page contentTypetext/html;charsetUTF-8 languagejava %
!DOCTYPE html
html
headtitleEL Expression Example/title
/head
body%// 设置作用域属性pageContext.setAttribute(name, Alice);request.setAttribute(city, New York);session.setAttribute(country, USA);application.setAttribute(hobby, Reading);%pName: ${name}/ppCity: ${city}/ppCountry: ${country}/ppHobby: ${hobby}/p
/body
/html
7.4.2 JSTL核心标签库示例
% page contentTypetext/html;charsetUTF-8 languagejava %
% taglib prefixc urihttp://java.sun.com/jsp/jstl/core %
!DOCTYPE html
html
headtitleJSTL Core Tags Example/title
/head
body%// 设置作用域属性request.setAttribute(numbers, Arrays.asList(1, 2, 3, 4, 5));%c:if test${not empty numbers}ulc:forEach items${numbers} varnumli${num}/li/c:forEach/ul/c:if
/body
/html
7.4.3 JSTL格式化标签库示例
% page contentTypetext/html;charsetUTF-8 languagejava %
% taglib prefixfmt urihttp://java.sun.com/jsp/jstl/fmt %
!DOCTYPE html
html
headtitleJSTL Format Tags Example/title
/head
body%Date now new Date();pageContext.setAttribute(now, now);%fmt:formatDate value${now} patternyyyy-MM-dd HH:mm:ss /
/body
/html