🥼JavaWeb~(肆)摸索JSP,JavaBean
笔记代码
JSP
定制错误页面
- 方法一: 直接在 jsp 页面配置 errorPage - errors/index.jsp- <%@ page contentType="text/html;charset=UTF-8" language="java" %> 
 <%--此处定制错误页面指向--%>
 <%@ page errorPage="errors/500.jsp" %>
 <html>
 <head>
 <title>错误页面</title>
 </head>
 <body>
 <%
 int a = 1 / 0;
 %>
 </body>
 </html>
 - errors/500.jsp- <%@ page contentType="text/html;charset=UTF-8" language="java" %> 
 <html>
 <head>
 <title>500错误</title>
 </head>
 <body>
 <h1>500错误页面.</h1>
 </body>
 </html>
 
- 方法二: 在 web.xml 中配置 - errors/404.jsp - <%@ page contentType="text/html;charset=UTF-8" language="java" %> 
 <html>
 <head>
 <title>404错误</title>
 </head>
 <body>
 <h1>404错误页面.</h1>
 </body>
 </html>
 - web.xml
 <web-app>
 <display-name>Archetype Created Web Application</display-name>
 <error-page>
 <error-code>404</error-code>
 <location>/errors/404.jsp</location>
 </error-page>
 <error-page>
 <error-code>500</error-code>
 <location>/errors/500.jsp</location>
 </error-page>
 </web-app>
 
合并页面
- common/header.jsp- <%@ page contentType="text/html;charset=UTF-8" language="java" %> 
 <h1>这里是header</h1>
- common/footer.jsp- <%@ page contentType="text/html;charset=UTF-8" language="java" %> 
 <h1>这里是footer</h1>
- common/index.jsp- <%@ page contentType="text/html;charset=UTF-8" language="java" %> 
 <html>
 <head>
 <title>测试common</title>
 </head>
 <body>
 <!--
 这种会把include的页面内容合并进本页面,最后是展示一个页面
 -->
 <%@ include file="header.jsp" %>
 <h1>网页主体</h1>
 <%@ include file="footer.jsp" %>
 <HR />
 <!--
 jsp:include标签,本质是导入其他文件内容,最后是多个文件合为一个页面展示
 (更灵活,建议用这个)
 上面方法的因为是合并,所以很有可能出现变量重名,干扰的问题
 这个方法不同文件之间互不干扰
 -->
 <jsp:include page="/common/header.jsp" />
 <h1>网页主体</h1>
 <jsp:include page="/common/footer.jsp" />
 </body>
 </html>
内置对象及作用域
- 内置对象 - PageContext 存东西 
 Request 存东西
 Response
 Session 存东西
 Application 【SerlvetContext】 存东西
 config 【SerlvetConfig】
 out
 page ,不用了解
 exception
- 操作对象 - demo/1.jsp- <!-- 
 * @?: *********************************************************************
 * : Weidows
 * : 2021-04-07 16:29:42
 * : Weidows
 * : 2021-04-07 17:29:11
 * : \Weidows\JavaWeb\JSP\src\main\webapp\1.jsp
 * :
 * @!: *********************************************************************
 * servlet 2.3默认不支持EL表达式,需要开启.
 -->
 <%@ page isELIgnored="false" %>
 <%@ page contentType="text/html;charset=UTF-8" language="java" %>
 <html>
 <head>
 <title>index4</title>
 </head>
 <body>
 <%--
 PageContext 存东西
 Request 存东西
 Response
 Session 存东西
 Application 【SerlvetContext】 存东西
 config 【SerlvetConfig】
 out
 page ,不用了解
 exception
 --%>
 <%
 pageContext.setAttribute("name1", "秦疆1号"); // 保存的数据只在一个页面中有效
 request.setAttribute("name2", "秦疆2号"); // 保存的数据只在一次请求中有效,请求转发会携带这个数据
 session.setAttribute("name3", "秦疆3号"); // 保存的数据只在一次会话中有效,从打开浏览器到关闭浏览器
 application.setAttribute("name4", "秦疆4号"); // 保存的数据只在服务器中有效,从打开服务器到关闭服务器
 // 作用域从小到大
 String name1 = (String) pageContext.findAttribute("name1");
 String name2 = (String) pageContext.findAttribute("name2");
 String name3 = (String) pageContext.findAttribute("name3");
 String name4 = (String) pageContext.findAttribute("name4");
 String name5 = (String) pageContext.findAttribute("name5"); // 这个不存在
 %>
 <%--
 EL表达式
 ${内容} == <%= 内容 %>
 用EL表达式的话,null值不会显示,而使用JSP表达式会显示null值
 --%>
 <h1>取出的内容:</h1>
 <h3>${name1}</h3>
 <h3>${name2}</h3>
 <h3>${name3}</h3>
 <h3>${name4}</h3>
 <HR>
 <h3>${name5}</h3>
 <h3><%=name5%></h3>
 </body>
 </html>
- 作用域 
JSP 标签
- 例子: 使用 JSP 标签转发页面,读取参数并显示出来. 
- src/main/webapp/demo/JSP-label/forward.jsp- <%@ page contentType="text/html;charset=UTF-8" language="java" %> 
 <html>
 <head>
 <title>JSP-label</title>
 </head>
 <body>
 <%-- 转发request携带的参数(注意JSP标签内不能有注释,会报错500) --%>
 <jsp:forward page="forward.jsp">
 <jsp:param name="name" value="kuangshen"/>
 <jsp:param name="age" value="12"/>
 </jsp:forward>
 </body>
 </html>
- src/main/webapp/demo/JSP-label/forward.jsp- <%@ page contentType="text/html;charset=UTF-8" language="java" %> 
 <%@ page isELIgnored="false" %>
 <html>
 <head>
 <title>转发页面</title>
 </head>
 <body>
 <%
 pageContext.setAttribute("name", request.getParameter("name"));
 pageContext.setAttribute("age", request.getParameter("age"));
 %>
 <h3>姓名: ${name}</h3>
 <h3>年龄: ${age}</h3>
 </body>
 </html>
JSTL 表达式
JSTL 标签库的使用就是为了弥补 HTML 标签的不足,功能上与 Java 相似.
- 分类 - 格式化标签 
- SQL 标签 
- XML 标签 
- JSTL 函数 
- 核心标签 (掌握部分) 
 
- 使用 - 在 pom.xml 中导入依赖 - <!-- JSTL表达式依赖 --> 
 <dependency>
 <groupId>javax.servlet</groupId>
 <artifactId>jstl</artifactId>
 <version>1.2</version>
 </dependency>
 <!-- Standard标签库 -->
 <dependency>
 <groupId>taglibs</groupId>
 <artifactId>standard</artifactId>
 <version>1.1.2</version>
 </dependency>
- 在 JSP 页面还需要引入对应要使用的 taglib,这里只用到 core: - <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> 
 
- if - <%@ page contentType="text/html;charset=UTF-8" language="java" %> <%@ taglib 
 prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> <%@ page
 isELIgnored="false" %>
 <html>
 <head>
 <title>if测试</title>
 </head>
 <body>
 <%--action是代码提交位置--%>
 <form action="if.jsp" method="post">
 <label>
 用户名:
 <input type="text" name="username" />
 </label>
 <input type="submit" value="登录" />
 </form>
 <hr />
 <%--
 EL表达式获取表单中的数据 ${param.参数名}
 判断如果提交的用户名是admin,则登录成功,并把isAdmin置为true
 --%>
 <c:if test="${param.username=='admin'}" var="isAdmin">
 <c:out value="管理员欢迎您!" />
 </c:if>
 </body>
 </html>
- choose-when 
 <%-- Created by IntelliJ IDEA. User: 29845 Date: 2021/4/8 Time: 20:38 To change
 this template use File | Settings | File Templates. --%> <%@ page
 contentType="text/html;charset=UTF-8" language="java" %> <%@ page
 isELIgnored="false" %>
 <html>
 <head>
 <title>choose-when测试</title>
 </head>
 <body>
 <form action="" method="post">
 <label>
 <%--定义一个变量score--%>
 <input type="text" name="score" />
 </label>
 <input type="submit" name="提交" />
 </form>
 <c:choose>
 <c:when test="${param.score>=90}"> 你的成绩为优秀 </c:when>
 <c:when test="${param.score>=80}"> 你的成绩为一般 </c:when>
 <c:when test="${param.score>=70}"> 你的成绩为良好 </c:when>
 <c:when test="${param.score<=60}"> 你的成绩为不及格 </c:when>
 </c:choose>
 </body>
 </html>
- forEach - <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> 
 <%@ page import="java.util.ArrayList" %><%--
 Created by IntelliJ IDEA.
 User: 29845
 Date: 2021/4/8
 Time: 20:59
 To change this template use File | Settings | File Templates.
 --%>
 <%@ page contentType="text/html;charset=UTF-8" language="java" %>
 <%@ page isELIgnored="false" %>
 <html>
 <head>
 <title>forEach</title>
 </head>
 <body>
 <%
 ArrayList<String> setPeople = new ArrayList<>();
 setPeople.add(0, "张三");
 setPeople.add(1, "李四");
 setPeople.add(2, "王五");
 setPeople.add(3, "赵六");
 setPeople.add(4, "田六");
 request.setAttribute("list", setPeople);
 %>
 <%--
 var , 每一次遍历出来的变量
 items, 要遍历的对象
 可选:
 begin, 哪里开始
 end, 到哪里
 step, 步长
 --%>
 <c:forEach var="getPeople" items="${list}" begin="1" end="3" step="1">
 <c:out value="${getPeople}"/>
 <br/>
 </c:forEach>
 </body>
 </html>
JavaBean
- JavaBean(实体类) 有特定的写法: - 一个无参构造 
- 属性必须私有化 
- 必须有对应的 get/set 方法; 
 
- 一般用来和数据库的字段做映射 ORM(对象关系映射) - 表—>类 
- 字段–>属性 
- 行记录---->对象 
 
- 样例 Java 类: - src/main/java/pojo/People.java- package pojo; 
 public class People {
 private int id;
 private String name;
 private int age;
 private String address;
 public People() {
 }
 public People(int id, String name, int age, String address) {
 this.id = id;
 this.name = name;
 this.age = age;
 this.address = address;
 }
 public int getId() {
 return id;
 }
 public void setId(int id) {
 this.id = id;
 }
 public String getName() {
 return name;
 }
 public void setName(String name) {
 this.name = name;
 }
 public int getAge() {
 return age;
 }
 public void setAge(int age) {
 this.age = age;
 }
 public String getAddress() {
 return address;
 }
 public void setAddress(String address) {
 this.address = address;
 }
 
 public String toString() {
 return "People{" +
 "id=" + id +
 ", name='" + name + '\'' +
 ", age=" + age +
 ", address='" + address + '\'' +
 '}';
 }
 public static void main(String[] args) {
 new People(1, "秦疆1号", 3, "西安");
 new People(2, "秦疆2号", 3, "西安");
 new People(3, "秦疆3号", 3, "西安");
 }
 }
- 样例 JSP: - src/main/webapp/javaBean.jsp- 这里只是简单介绍 JavaBean 及其使用,并不推荐用 JSP 标签来操作 JavaBean,还是推荐用 Java 代码操作实体类对象.
 - <%-- Created by IntelliJ IDEA. User: 29845 Date: 2021/4/8 Time: 23:32 To change 
 this template use File | Settings | File Templates. --%> <%@ page
 contentType="text/html;charset=UTF-8" language="java" %>
 <html>
 <head>
 <title>javaBean测试</title>
 </head>
 <body>
 <jsp:useBean id="people" class="pojo.People" scope="page" />
 <jsp:setProperty name="people" property="id" value="1" />
 <jsp:setProperty name="people" property="name" value="狂神说" />
 <jsp:setProperty name="people" property="age" value="3" />
 <jsp:setProperty name="people" property="address" value="西安" />
 id:
 <jsp:getProperty name="people" property="id" />
 name:
 <jsp:getProperty name="people" property="name" />
 age:
 <jsp:getProperty name="people" property="age" />
 address:
 <jsp:getProperty name="people" property="address" />
 </body>
 </html>
本博客所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议。转载请注明来自 ⭐️齐下无贰⭐️!
 评论














