您的位置:首页 > 编程语言 > Java开发

JSP详解与JavaBean

2022-02-16 17:46 861 查看

8、JSP

8.1、什么是JSP

Java Server Pages:Java服务器端页面,也和Servlet一样,用于开发动态web技术!

最大的特点:

  • 写JSP就像在写HTML
  • 区别: HTML只给用户提供静态的数据
  • JSP页面中可以嵌入JAVA代码,为用户提供动态数据;

8.2、JSP原理

思路:JSP到底怎么执行的!

  • 代码层面没有任何问题

  • 服务器内部工作

    tomcat中有一个work目录;

    IDEA中使用Tomcat会在IDEA的tomcat中生成一个work目录

    IDEA中tomcat工作空间

我电脑的地址:

C:\Users\wangyudong\AppData\Local\JetBrains\IntelliJIdea2021.2\tomcat\cb2d2cf8-e51b-4fec-b4b5-e8a48935f7b4\work\Catalina\localhost\ROOT\org\apache\jsp

发现页面转变成了Java程序!

浏览器向服务器发送请求,不管访问什么资源,其实都是在访问Servlet!

JSP最终也会被转换成为一个Java类!

JSP本质上就是一个Servlet

//初始化
public void _jspInit() {
}
//销毁
public void _jspDestroy() {
}
//JSPService
public void _jspService(HttpServletRequest request, HttpServletResponse response)
  1. 判断请求

  2. 内置一些对象

    final javax.servlet.jsp.PageContext pageContext;  //页面上下文
    javax.servlet.http.HttpSession session = null;  //session
    final javax.servlet.ServletContext application;  //applicationContext
    final javax.servlet.ServletConfig config;  //config
    javax.servlet.jsp.JspWriter out = null;  //out
    final java.lang.Object page = this;  //page:当前页
    HttpServletRequest request  //请求
    HttpServletResponse response  //响应
  3. 输出页面前增加的代码

    response.setContentType("text/html");  //设置响应的页面类型
    pageContext = _jspxFactory.getPageContext(this, request, response,
    null, true, 8192, true);
    _jspx_page_context = pageContext;
    application = pageContext.getServletContext();
    config = pageContext.getServletConfig();
    session = pageContext.getSession();
    out = pageContext.getOut();
    _jspx_out = out;
  4. 以上这些个对象我们可以在JSP页面中直接使用!

在JSP页面中;

只要是JAVA代码就会原封不动的输出;

如果是HTML代码,就会被转换为:

out.write("<html>\r\n");

这样的格式,输出到前端!

8.3、JSP基础语法

任何语言都有自己的语法,JAVA中有,JSP作为Java技术的一种应用,它拥有一些自己扩充的语法(了解,知道即可!),Java所有语法都支持!

JSP表达式

<%--JSP表达式
作用:用来将程序的输出,输出到客户端
<%= 变量或者表达式%>
--%>
<%= new java.util.Date()%>

JSP脚本片段

<%--jsp脚本片段--%>
<%
int sum = 0;
for (int i = 1; i <= 100; i++) {
sum+=i;
}
out.println("<h1>Sum="+sum+"</h1>");
%>

脚本片段的再实现

<%
int x = 10;
out.println(x);
%>
<p>这是一个JSP文档</p>
<%
int y = 2;
out.println(y);
%>

<hr>

<%--在代码中嵌入HTML元素--%>
<%
for (int i = 0; i < 5; i++) {

%>
<h1>Hello,World <%= i%></h1>
<%
}
%>

JSP声明

<%!
static {
System.out.println("Loading Servlet!");
}
private int globalVar = 0;
public void kuang(){
System.out.println("进入了方法Kuang!");
}
%>

JSP声明:会被编译到JSP生成的Java的类中!其他的,就会被生成到_jspService方法中!

在JSP中嵌入Java代码即可!

<%%>
<%=%>
<%!%>

<%--注释--%>

JSP的注释,不会在客户端显示,HTML就会!

8.4、JSP指令

<%@page args.... %>
<%include file=""%>

<%--@include会将两个页面合二为一--%>

<%@include file="common/header.jsp"%>
<h1>网页主体</h1>

<%@include file="common/footer.jsp"%>

<hr>

<%--jsp标签
jsp:include: 拼接页面,本质还是三个
--%>
<jsp:include page="/common/header.jsp"/>
<h1>网页主体</h1>
<%
int i = 10;
%>
<jsp:include page="/common/footer.jsp"/>
==================================================================
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<h1>我是Header</h1>
==================================================================
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<h1>我是Footer</h1>
<%--@include会将两个页面合二为一,如果有重复的变量会报错--%>

<%@include file="common/header.jsp"%>
<h1>网页主体</h1>
<%
int i = 10;
%>
<%@include file="common/footer.jsp"%>

<hr>
==================================================================
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<h1>我是Footer</h1>
<%
int i = 0;
%>
<%--jsp标签
jsp:include: 拼接页面,本质还是三个,三个页面互不影响
--%>
<jsp:include page="/common/header.jsp"/>
<h1>网页主体</h1>
<%
int i = 10;
%>
<jsp:include page="/common/footer.jsp"/>
=====================================================================
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<h1>我是Footer</h1>
<%
int i = 0;
%>

8.5、9大内置对象

  • PageContext 存东西
  • Request 存东西
  • Response
  • Session 存东西
  • Application [ServletContext] 存东西
  • config [ServletConfig]
  • out
  • page,不用了解
  • exception
pageContext.setAttribute("name1","秦疆1号"); //保存的数据只在一个页面中有效
request.setAttribute("name2","秦疆2号"); //保存的数据只在一次请求中有效,请求转发会携带这个数据
session.setAttribute("name3","秦疆3号"); //保存的数据只在一次会话中有效,从打开浏览器到关闭浏览器
application.setAttribute("name4","秦疆4号"); //保存的数据只在服务器中有效,从打开服务器到关闭服务器

request:客户端向服务器发送请求,产生的数据,用户看完就没用了,比如:新闻,用户看完没用的!

session:客户端向服务器发送请求,产生的数据,用户用完一会还有用,比如:购物车;

application:客户端向服务器发送请求,产生的数据,一个用户用完了,其他用户还可能使用,比如:聊天数据;

示例:

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>

<%--内置对象--%>

<%
pageContext.setAttribute("name1","秦疆1号"); //保存的数据只在一个页面中有效
request.setAttribute("name2","秦疆2号"); //保存的数据只在一次请求中有效,请求转发会携带这个数据
session.setAttribute("name3","秦疆3号"); //保存的数据只在一次会话中有效,从打开浏览器到关闭浏览器
application.setAttribute("name4","秦疆4号"); //保存的数据只在服务器中有效,从打开服务器到关闭服务器%>

<%--脚本片段中的代码,会被原封不动的生成到.JSP.java中
要求:这里面的代码:必须保证Java语法的正确性
--%>
<%
//从pageContext取出,我们通过寻找的方式来
//从底层到高层 (作用域):page-->request-->session-->application
//JVM:双亲委派机制;
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"); //不存在

pageContext.forward("/pageDemo02.jsp");
%>

<%--使用EL表达式输出    ${}--%>

<h1>取出的值为:</h1>
<h3>${name1}</h3>
<h3>${name2}</h3>
<h3>${name3}</h3>
<h3>${name4}</h3>
<%--<h3>${name5}</h3>--%>
<h3><%=name5%></h3>

</body>
</html>
====================================================================
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>

<%--脚本片段中的代码,会被原封不动的生成到.JSP.java中
要求:这里面的代码:必须保证Java语法的正确性
--%>
<%
//从pageContext取出,我们通过寻找的方式来
//从底层到高层 (作用域):
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表达式输出    ${}--%>

<h1>取出的值为:</h1>
<h3>${name1}</h3>
<h3>${name2}</h3>
<h3>${name3}</h3>
<h3>${name4}</h3>
<%--<h3>${name5}</h3>--%>
<h3><%=name5%></h3>

</body>
</html>
=====================================================================
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<%--
public static final int PAGE_SCOPE = 1;
public static final int REQUEST_SCOPE = 2;
public static final int SESSION_SCOPE = 3;
public static final int APPLICATION_SCOPE = 4;
//scope: 作用域
public void setAttribute(String name, Object attribute, int scope) {
switch(scope) {
case 1:
this.mPage.put(name, attribute);
break;
case 2:
this.mRequest.put(name, attribute);
break;
case 3:
this.mSession.put(name, attribute);
break;
case 4:
this.mApp.put(name, attribute);
break;
default:
throw new IllegalArgumentException("Bad scope " + scope);
}
}
--%>
<%
pageContext.setAttribute("hello1","hello1",PageContext.SESSION_SCOPE);
//session.setAttribute("hello1","hello1");等价于
%>
</body>
</html>

8.6、JSP标签、JSTL标签、EL表达式

<!--standard标签库-->
<dependency>
<groupId>taglibs</groupId>
<artifactId>standard</artifactId>
<version>1.1.2</version>
</dependency>
<!--JSTL表达式的依赖-->
<dependency>
<groupId>javax.servlet.jsp.jstl</groupId>
<artifactId>jstl-api</artifactId>
<version>1.2</version>
</dependency>

EL表达式: $

  • 获取数据
  • 执行运算
  • 获取web开发的常用对象

JSP标签

<%--jsp:include--%>

<%--
http://localhost:8080/jsptag.jsp?name=kuangshen&age=12
--%>

<jsp:forward page="/jsptag2.jsp">
<jsp:param name="name" value="kuangshen"/>
<jsp:param name="age" value="12"/>
</jsp:forward>

JSTL表达式

JSTL标签库的使用就是为了弥补HTML标签的不足;它自定义许多标签,可以供我们使用,标签的功能和Java代码一样!

格式化标签

SQL标签

XML标签

核心标签 (掌握部分)

JSTL标签库使用步骤

  • 引入对应的taglib
  • 使用其中的方法
  • 在Tomcat中也需要引入jstl的包,否则会报错:JSTL解析错误

c:if

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%--引入JSTL核心标签库,我们才能使用JSTL标签 core--%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html>
<head>
<title>Title</title>
</head>
<body>

<h4>if测试</h4>

<hr>

<form action="coreif.jsp" method="get">
<%--
EL表达式获取表单中的数据
${param.参数名}
--%>
<input type="text" name="username" value="${param.username}">
<input type="submit" value="登录">
</form>

<%--判断如果提交的用户名是管理员,则登录成功--%>
<c:if test="${param.username=='admin'}" var="isAdmin">
<c:out value="管理员欢迎您!"/>
</c:if>

<%--自闭合标签--%>
<c:out value="${isAdmin}"/>

</body>
</html>

c:choose c:when

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>

<%--定义一个变量score,值为85--%>
<c:set var="score" value="55" />

<c:choose>
<c:when test="${score>=90}">
你的成绩为优秀
</c:when>
<c:when test="${score>=80}">
你的成绩为良好
</c:when>
<c:when test="${score>=70}">
你的成绩为一般
</c:when>
<c:when test="${score<=60}">
你的成绩为不及格
</c:when>
</c:choose>
</body>
</html>

c:foreach

<%
ArrayList<String> people = new ArrayList<>();
people.add(0,"张三");
people.add(1,"李四");
people.add(2,"王五");
people.add(3,"赵六");
people.add(4,"田七");
request.setAttribute("list",people);
%>

<%--
var , 每一次遍历出来的变量
items , 要遍历的对象
begin , 哪里开始
end , 到哪里
step , 步长
--%>

<c:forEach var="people" items="${list}">
<c:out value="${people}"/> <br>
</c:forEach>

<hr>

<c:forEach var="people" items="${list}" begin="2" end="4" step="2">
<c:out value="${people}"/> <br>
</c:forEach>

9、JavaBean

实体类

javaBean有特定的写法:

  • 必须要有一个无参构造
  • 属性必须私有化
  • 必须有对应的get/set方法

一般用来和数据库的字段做映射 ORM;

ORM:对象关系映射

  • 表--->类
  • 字段--->属性
  • 行记录--->对象

people表

id name age address
1 秦疆1号 3 西安
2 秦疆2号 18 西安
3 秦疆3号 100 西安
class People{
private int id;
private String name;
private int age;
private String address;
}

class A{
new People(1,"秦疆1号",3,"西安");
new People(2,"秦疆2号",18,"西安");
new People(3,"秦疆3号",100,"西安");
}
<%@ page import="com.kuang.pojo.People" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>

<%
//    People people = new People();
//    people.setAddress();
//    people.setAge();
//    people.setId();
//    people.setName();
%>

<jsp:useBean id="people" class="com.kuang.pojo.People" scope="page"/>

<jsp:setProperty name="people" property="address" value="西安"/>
<jsp:setProperty name="people" property="id" value="1"/>
<jsp:setProperty name="people" property="age" value="3"/>
<jsp:setProperty name="people" property="name" value="小狂神"/>

<%--<%=people.getAddress()%>--%>

姓名:<jsp:getProperty name="people" property="name"/>
id:<jsp:getProperty name="people" property="id"/>
年龄:<jsp:getProperty name="people" property="age"/>
地址:<jsp:getProperty name="people" property="address"/>
</body>
</html>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: