JSP课堂笔记

课堂笔记

1.JSP

JSP(全称Java Server Pages)是由 Sun Microsystems 公司倡导和许多公司参与共同创建的一种使软件开发者可以响应客户端请求,而动态生成 HTML、XML 或其他格式文档的Web网页的技术标准。

JSP 技术是以 Java 语言作为脚本语言的,JSP 网页为整个服务器端的 Java 库单元提供了一个接口来服务于HTTP的应用程序。

JSP文件后缀名为 *.jsp

jsp的组成:html + java

<%@ page import="java.util.Date" %><%--
  Created by IntelliJ IDEA.
  User: ming's
  Date: 2023/8/2
  Time: 16:33
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
<%
    //java代码
    Date date = new Date();
    System.out.println(date.toLocaleString());
%>

<p>声明变量和方法,使用!</p>
<%!
    //声明变量和方法,使用!
    int a = 1;
    public void method1(){
    System.out.println("hello world");
}
%>

<p>页面显示,使用=</p>
<%=Math.random() %><br/>
<%="hello ???" %><br/>
<%=a %><br/>

<%
    //调用属性和方法
    System.out.println(a);
    method1();
%>

</body>
</html>

2023-8-7 11:14:26
1
hello world

在这里插入图片描述

2. 注释

HTML的注释虽然在浏览器中不显示,但是通过查看源码还是可以看到,这是不安全的。JSP中有一种隐藏注释,不管是在浏览器还是查看HTML源码时都看不到,所以更安全

2.1 html注释

<html>
<head>
    <title>Title</title>
</head>
<body>
<div>
    <form action="user" method="get">
        username : <input  type="text" name="username"/>
        password : <input  type="password" name="password"/>
        <input type="submit" value="submit"/>
    </form>
</div>
    /* 
    	多行注释
    多行注释
    多行注释
    */
    </body>
</html>

2.2 JSP注释

<html>
<head>
    <title>Title</title>
</head>
<body>
<div>
    <form action="user" method="get">
        username : <input  type="text" name="username"/>
        password : <input  type="password" name="password"/>
        <input type="submit" value="submit"/>
    </form>
</div>
   	<%--注释内容--%>
    </body>
</html>

3. JSP原理

当第一次访问jsp页面时,会向一个servlet容器(tomcat等)发出请求,servlet容器先要把 jsp页面转化为servlet代码(.java),再编译成.class 文件 再进行调用。当再次访问jsp页面时 跳过翻译和编译的过程 直接调用。

执行过程
1、 客户端发出请求
2、web容器将jsp转化为servlet代码(.java)
3、web容器将转化为servlet代码编译(.class)
4、web容器加载编译后的代码并执行
5、将执行结果响应给客户端

在这里插入图片描述

4. JSP指令

JSP指令用来设置与整个JSP页面相关的属性

指令描述
<%@ page … %>定义页面的依赖属性,比如脚本语言、error页面、缓存等等
<%@ include … %>包含其他文件
<%@ taglib … %>引入标签库的定义,可以是自定义标签

5. Page指令

page指令
为容器提供当前页面的使用说明。一个JSP页面可以包含多个page指令
语法: <%@ page attribute1=“value1” attribute2="value2” %>

属性描述
contentType指定当前JSP页面的MIME类型和字符编码格式
errorPage指定当JSP页面发生异常时需要转向的错误处理页面
isErrorPage指定当前页面是否可以作为另一个JSP页面的错误处理页面
import导入要使用的Java类
language定义JSP页面所用的脚本语言,默认是Java
session指定JSP页面是否使用session。默认为true立即创建,false为使用时创建
pageEncoding指定JSP页面本身的编码
<%@ page import="java.util.Date" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>

6. Error

当前页面有异常的时候,跳转到处理页面; -给用户更加友好的提示

<%--
  Created by IntelliJ IDEA.
  User: ming's
  Date: 2023/8/7
  Time: 15:30
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ page errorPage="error.jsp" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
   <%
    int i = 1/0;
    %>
</body>
</html>

7. include 包含

把头部或其他公共信息给抽取出来,形成一个新的也,其他页面如果需要使用的话,给引用进来

<%@ include file="top.jsp"%>  静态包含
<jsp:include page=""></jsp:include>动态包含

在这里插入图片描述

在这里插入图片描述

8. JSP动作指令

include

<%@ include file="top.jsp"%>  静态包含
<jsp:include page=""></jsp:include>动态包含

在这里插入图片描述

静态包含【先包含,在编译】

动态包含【先编译,再包含】

在这里插入图片描述

9. javaBean

javabean是一种用java语言写的可重用的组件;

javaBean一般是用来封装数据模型的;封装好的模型用来在数据传递的时候使用;

数据模型:

用户表 用户ID,用户姓名,用户年龄,用户email,用户密码

package com.ming.pojo;

public class User implements Serializable {

   private int id;

   private String name;

   private int age;

   private String email;

   private String password;

   public User() {
   }

   public User(int id, String name, int age, String email, String password) {
       this.id = id;
       this.name = name;
       this.age = age;
       this.email = email;
       this.password = password;
   }

   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 getEmail() {
       return email;
   }

   public void setEmail(String email) {
       this.email = email;
   }

   public String getPassword() {
       return password;
   }

   public void setPassword(String password) {
       this.password = password;
   }

   @Override
   public String toString() {
       return "User{" +
               "id=" + id +
               ", name='" + name + '\'' +
               ", age=" + age +
               ", email='" + email + '\'' +
               ", password='" + password + '\'' +
               '}';
   }

}

javaBean的写法:

1.必须是公共的类;

2.必须有一个无参的构造函数;

3.属性是私有的,同时必须提供公共getter和setter方法;

4.必须序列化

10. 动作标签

在这里插入图片描述

<%--
  Created by IntelliJ IDEA.
  User: ming's
  Date: 2023/8/7
  Time: 15:30
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
<p>相当于java代码的 User use = new User();</p>
<jsp:useBean id="user" class="com.ming.pojo.User"></jsp:useBean>
<p>相当于java代码的user.setAge(18);</p>
<jsp:setProperty name="user" property="age" value="18"></jsp:setProperty><br/>
<jsp:setProperty name="user" property="id" value="666"></jsp:setProperty><br/>
<jsp:setProperty name="user" property="name" value="ming"></jsp:setProperty><br/>
<p>相当于java代码的user.getName();</p>
<jsp:getProperty name="user" property="name"/><br/>
<jsp:getProperty name="user" property="id"/><br/>
<jsp:getProperty name="user" property="age"/><br/>

<%
    pageContext.setAttribute("pageContextKey" , "pageContextValue");
    request.setAttribute("requestKey" , "requestValue");
    session.setAttribute("sessionKey" , "sessionValue");
    application.setAttribute("applicationKey" , "applicationValue");
%>

<%=pageContext.getAttribute("pageContextKey")%><br/>
<%=request.getAttribute("requestKey")%><br/>
<%=session.getAttribute("sessionKey")%><br/>
<%=application.getAttribute("applicationKey")%><br/>


</body>
</html>

11. 请求转发

在这里插入图片描述

//请求转发 同时携带了两个参数
<jsp:forward page="5.jsp">
    <jsp:param name="name" value="xiaoming"/>
    <jsp:param name="age" value="11"/>
</jsp:forward>
<%=request.getParameter("name") %>

12. 内置对象

对象名类型说明
requestjavax.servlet.http.HttpServletRequest
responsejavax.servlet.http.HttpServletResponse
sessionjavax.servlet.http.HttpSession由session=“true”开关
applicationjavax.servlet.ServletContext
configjavax.servlet.ServletConfig
exceptionjava.lang.Throwable由isErrorPage=“false”开关
outjavax.servlet.jsp.JspWriter
pageContextjavax.servlet.jsp.PageContext
pagejava.lang.Object当前对象this当前servlet实例

13. 域对象

在这里插入图片描述

<%--
  Created by IntelliJ IDEA.
  User: ming's
  Date: 2023/8/7
  Time: 15:30
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>

<%
    pageContext.setAttribute("pageContextKey" , "pageContextValue");
    request.setAttribute("requestKey" , "requestValue");
    session.setAttribute("sessionKey" , "sessionValue");
    application.setAttribute("applicationKey" , "applicationValue");
%>

<%=pageContext.getAttribute("pageContextKey")%><br/>
<%=request.getAttribute("requestKey")%><br/>
<%=session.getAttribute("sessionKey")%><br/>
<%=application.getAttribute("applicationKey")%><br/>


</body>
</html>

14. EL表达式

在这里插入图片描述

<%@ page import="java.util.Date" %><%--
  Created by IntelliJ IDEA.
  User: ming's
  Date: 2023/8/2
  Time: 16:33
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
<%
request.setAttribute("key1","v1");
  //对象
  User user= new User();
  user.setName("ming");
  request.setAttribute("user",user);
  //list
  List<user> userList=new ArrayList<>();
  userList.add(user);
  userList.add(user);
  request.setAttribute("userList",userList);
  //数组  map  set
  int [] arrInt={1,2,3,4,5};
  request.setAttribute("arrInt",arrInt);

  //map
  Map<String,String>  map=new HashMap<>();
  map.put("a","a1");
  map.put("b","b1");
  request.setAttribute("map",map);
%>

<%=request.getAttribute("key1")%>
${key1}
//获取对象
${user}  -${user.name} -${user.des}
<hr/>
//获取list
${userList}   -${userList[0]}  -${userList[0].name}
<hr/>
//获取数组
${arrInt}  -${arrInt[3]}
<hr/>
//获取map
${map.a}  ${map.b}
如果没有值:  request.getAttribute()  ==null
           el表达式  什么都不显示

</body>
</html>

算术运算符

<%@ page import="java.util.Date" %><%--
  Created by IntelliJ IDEA.
  User: ming's
  Date: 2023/8/2
  Time: 16:33
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
request.setAttribute("key1",1);
  //对象
  User user=new User();
  user.setName("ming");
  request.setAttribute("user",user);
  //list
  List<User> userList=new ArrayList<>();
  userList.add(user);
  userList.add(user);
  request.setAttribute("userList",userList);
  //数组  map  set
  int [] arrInt={1,2,3,4,5};
  request.setAttribute("arrInt",arrInt);

  //map
  Map<String,String>  map=new HashMap<>();
  map.put("a","a1");
  map.put("b","b1");
  request.setAttribute("map",map);
%>

<%=request.getAttribute("key1")%>
${key1}
//获取对象
${book}  -${book.name} -${book.des}
<hr/>
//获取list
${bookList}   -${bookList[0]}  -${bookList[0].name}
<hr/>
//获取数组
${arrInt}  -${arrInt[3]}
<hr/>
//获取map
${map.a}  ${map.b}
如果没有值:  request.getAttribute()  ==null
           el表达式  什么都不显示

${sessionScope.k1}

<br/>
//算术运算符
${1+1}  ${key1+1}  ${2-1}  ${2*2}  ${3/2}  ${3%2}
//条件运算符
${1>2}  ${1>=2}  ${1>2 && 1>3}  ${1>2 || 3>2}
//三目运算符
${1>2?1:2}
//是否为空
${empty key1}

</body>
</html>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值