jsp笔记

jsp笔记

jsp与asp,php

jsp:Java server pages,运行在服务器段java页面程序,也就是说jsp是在服务器端生成java程序,能接收请求,返回数据,还可以写java代码,以及html+css+js等。这种方式非常复杂,也非常乱。

<!-- html,css,js -->
<h1 style="background-color:red;">这是jsp页面</h1>
<script>alert("弹窗")</script>
<!-- java代码 -->
<%
System.out.print("java代码");
 //打印到页面上
out.print("通过jsp打印数据");
%>

jsp是为了解决servlet的显示页面数据的。

jsp本质是一个servlet,本质还是java代码(因为servlet本质是java)。
运行过程:jsp必须通过tomcat服务器,先把xxx.jsp转换为xxx_jsp.java,然后调用javac.java的编译器,对程序xxx_jsp.java进行编译,生成xxx_jsp.class文件,使用时执行器java.exe,当执行器启动时,它就创建虚拟机,程序就在虚拟机中运行。

asp:active server pages,是动态的服务页面,主要在c#中实现web,受平台的限制,只能在windows平台上使用

php:hypertext preprocessor,超文本预处理器。也可以写html等相关的内容

jsp的特点

1.是运行服务端,在浏览器端无法解析,html是可以直接在浏览器中打开的。jsp需要服务器的转换。

2.它能够接收请求,也能进行响应。可以把前端的数据接收,也能返回数据。

jsp的基本结构

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
    <!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
</head>
<body>

</body>
</html>

写入内容之后部署到服务器中,进行运行。其实就是把jsp转换为java代码,再把java代码编译为class文件。

tomcat的目录结构

在这里插入图片描述

bin:二进制执行文件。里面最常用的文件是startup.bat,如果是 Linux 或 Mac 系统启动文件为 startup.sh

conf:配置目录。最核心的文件是server.xml。可以在里面改端口号等。默认端口号是8080,此端口号不能被其他应用程序占用。

lib:库文件。tomcat运行时需要的jar包所在的目录

logs:日志文件

temp:临时产生的文件,也就是缓存。

webapps:web的应用程序。web应用放置到此目录下浏览器可以直接访问

work:编译以后的class文件。

URI与URL

URI,uniform resource identifier,统一资源标识符,用来唯一的标识一个资源。

Web上可用的每种资源如HTML文档、图像、视频片段、程序等都是一个来URI来定位的.

URI一般由三部组成:①访问资源的命名机制 ②存放资源的主机名 ③资源自身的名称,由路径表示,着重强调于资源。

URL是uniform resource locator,统一资源定位符,它是一种具体的URI,即URL可以用来标识一个资源,而且还指明了如何locate这个资源。

URL是Internet上用来描述信息资源的字符串,主要用在各种WWW客户程序和服务器程序上,特别是著名的Mosaic。采用URL可以用一种统一的格式来描述各种信息资源,包括文件、服务器的地址和目录等。

URL一般由三部组成:①协议(或称为服务方式)②存有该资源的主机IP地址(有时也包括端口号)③主机资源的具体地址。如目录和文件名等

jsp生命周期

jsp的生命周期就是从创建到销毁的整个过程,类似于servlet生命周期,区别在于JSP生命周期还包括将JSP文件编译成servlet。

  • 编译阶段:

servlet容器编译servlet源文件,生成servlet类

  • 初始化阶段:

加载与JSP对应的servlet类,创建其实例,并调用它的初始化方法

  • 执行阶段:

调用与JSP对应的例的服务方法

  • 销毁阶段:

调用与JSP对应的servlet实例的销毁方法,然后销毁servlet实例

在这里插入图片描述

3大指令

作用:设置与jsp页面相关的属性

1.page指令

通过page指令可以设置当前页面上的基本属性,它可以引入java的类,也可以设置一些页面相关属性值。

<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8" autoFlush="true" %>
<%@page import="java.util.Date" %>

2.include指令

当前页面上包含哪些内容,包含指令

在当前页面导入第二个页面

<%@ include file="index02.jsp" %>

3.taglib指令

导入引入标签库的定义,可以是自定义标签。

jsp中有一个标签库,jstl标签库,当使用的时候需要引入

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>

引入标签库之后就可以直接使用了

10大行为动作

jsp行为动作标签就是通过特殊的动作标签,可以直接插入到文件中,使用这些动作标签,就可以完成一些功能。

常用的为前五个

1.jsp:include—用于在当前页面上包含其他的页面,和三大指令中的include一样,写法不同

<jsp:include page="index03.jsp"></jsp:include>
<h1>--------------------------</h1>
<jsp:include page="index.jsp"></jsp:include>
<h1>--------------------------</h1>
<%@include file="index02.jsp" %><!--指令  -->

2.jsp:useBean—可以把自己定义好的类加入到jsp页面上来用。

使用时自己创建一个javaBean

<jsp:useBean id="stu" class="com.zll.bean.Student">
</jsp:useBean>
<%
	stu.setId(222);  
    stu.setNameString("lisi");
    stu.setAge(19);
    stu.setSex("男");
%>
<%=stu.getId()+"\t"+stu.getNameString()+"\t"+stu.getAge()+"\t"+stu.getSex()%>

3.jsp:setProperty—通过这个动作来对Bean对象赋值

<jsp:useBean id="stu01" class="com.zll.bean.Student"></jsp:useBean>
<jsp:setProperty property="id" name="stu01" value="234"/>
<jsp:setProperty property="nameString" name="stu01" value="zll"/>
<jsp:setProperty property="age" name="stu01" value="18"/>
<jsp:setProperty property="sex" name="stu01" value="女"/>
<%=stu01.getId()+"\t"+stu01.getNameString()+"\t"+stu01.getAge()+"\t"+stu01.getSex()%>

4.jsp:getProperty—通过这个动作来获取赋给对象的值

<h3>用户id:<jsp:getProperty property="id" name="stu01"/></h3>
<h3>用户姓名:<jsp:getProperty property="nameString" name="stu01"/></h3>
<h3>用户年龄:<jsp:getProperty property="age" name="stu01"/></h3>
<h3>用户性别:<jsp:getProperty property="sex" name="stu01"/></h3>

5.jsp:forward—请求转发到别的页面上

<jsp:forward page="index.jsp"></jsp:forward>

6.jsp:plugin—用于在生成的html页面中包含applet和javaBean对象

7.jsp:element—动态创建一个xml元素

8.jsp:attribute—定义动态创建的xml元素的属性

9.jsp:body—定义动态创建的xml元素的主体

10.jsp:text—用于封装模板数据

9大内置对象

jsp提前给我们已经定义好的内置的对象,这些对象已经创建好了,我们用的时候不需要new,直接调用。

前四个常用

1.request:HttpServletRequest类的实例,和servlet中使用的httpServletRequest一样,可以接收其他页面传过来的数据。

传递数据到index04.jsp上

<center>
	<form action="index04.jsp">
	<label>用户名称:</label><input type="text" name="username">
	<label>用户密码:</label><input type="password" name="password">
	<input type="submit">
	</form>
</center>

接收

<%
	String username=request.getParameter("username");
	String password=request.getParameter("password");
	//请求转发
	request.getRequestDispatcher("index.jsp").forward(request, response);
	//重定向
	response.sendRedirect("index.jsp");
%>
<h1><%=username %></h1>
<h1><%=password %></h1>

2.response:HttpServletResponse类的实例,进行数据的响应

    response.getWriter().println(username);
	response.getWriter().println(password);

3.out:PrintWriter类的实例,直接打印数据到页面上

	out.print(username);
	out.print(password);

4.session:一次会话,存储在服务器端,默认是30分钟

	session.setAttribute("username", username);
	session.setAttribute("password", password);

	<h1><%=session.getAttribute("username") %></h1>
	<h1><%=session.getAttribute("password") %></h1>

5.application:

application.setAttribute("sex", "男");
<h1><%=application.getAttribute("sex") %></h1>

6.config:获取配置信息

<h1><%=config.getServletName() %></h1>

7.pageConText:获取JSP页面中的request、response、session、application等其他内置对象。

8.page:当前页面,可以写入数据,直接使用即可

9.exception:异常操作的时候使用

el表达式

为了让jsp写起来更简单,el是jsp内置的表达式语言,取值时比较方便,快,为了在jsp页面上不写java代码,一般和jstl一起使用

el替代的是<%=…%> 只能做输出

当使用el表达式的时候,我们的页面访问时,如果出现原样输出时,加isELIgnored=“false”,设为true时它是一个字符串,原样打印,false就是进行运算。

获取数据:${标识符}

全域查找:${xxx},如果不存在,输出空字符串,而不是null

获取指定域属性: p a g e S c o p e . x x x , {pageScope.xxx}, pageScope.xxx,{sessionScope.xxx}, r e q u e s t S c o p e . x x x , {requestScope.xxx}, requestScope.xxx,{applicationScope.xxx}

取值的顺序(由前到后):pageScope.xxx,sessionScope.xxx,requestScope.xxx,applicationScope.xxx

<%
	request.setAttribute("name", "zll1");
	session.setAttribute("name", "zll2");
	application.setAttribute("name", "zll3");
	pageContext.setAttribute("name","zll4");
%>
<h1>${name }</h1>

获取对象里的值

<%
    Student student=new Student(123,"zll",20,"男");
	request.setAttribute("name", student);
%>
<h1>${requestScope.name.id }</h1>
//通过插入对象中的值
<%
    Teacher teacher=new Teacher();
	teacher.setName("张三丰");
	Student student=new Student(123,"zll",20,"男",teacher);
	request.setAttribute("name", student);
%>
<h1>${requestScope.name.teacher.name }</h1>

jstl

jstl—jsp标准标签库,就是jsp在取值等操作时方便,做了一些标签,通过这些标签来进行操作,更加的简单化

jstl支持通用的,结构化的任务,比如迭代,条件判断,xml文档操作,国际化标签,sql标签,除了这些,它还提供了一个框架来使用集成jstl的自定义标签。

根据jstl标签提供的功能,将其分为5个类别

1.核心标签

引入核心标签库

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>

<c:out> 在页面打印内容 <c:set> 设置值 <c:remove> 删除值

<c:set var="number" scope="session" value="${100*5 }"></c:set>
<c:out value="${number }"></c:out>
<c:remove var="number"/>
<c:out value="${number }"></c:out>

<c:catch> 异常

<c:catch var="catchException">
<% int a=10/0; %>
</c:catch>
<c:if test="${catchException!=null }">
 <h3>异常是:${catchException }</h3>
 	<h3>异常是:${catchException.message }</h3>
</c:if>

<c:if> <c:when> <c:choose>

<c:set var="salary" scope="session" value="${3000*12 }"></c:set>
<c:if test="${salary>20000 }">
<h1>年收入为:${salary }</h1>
</c:if>

<c:foreach> 循环操作

<c:forEach begin="1" var="i" end="100" step="2">
<h3>你取的循环中的值:${i }</h3>
</c:forEach>
<c:redirect url="http://www.baidu.com">重定向</c:redirect>

2.格式化标签

主要是格式化数字以及日期

引入格式化标签

//引入核心标签
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
 //让${xxx }不原样输出
<%@ page isELIgnored="false" %>
 //引入核心
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>

3.SQL标签

引入SQL标签

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ page isELIgnored="false" %>
<%@ taglib prefix="sql" uri="http://java.sun.com/jsp/jstl/sql" %>

数据库的查询

<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ page isELIgnored="false" %>
<%@ taglib prefix="sql" uri="http://java.sun.com/jsp/jstl/sql" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>SQL</title>
</head>
<body>
<sql:setDataSource var="data01" driver="com.mysql.cj.jdbc.Driver"
url="jdbc:mysql://localhost:3306/bigdata06?useUnicode=true&characterEncoding=utf8&useSSL=false&&serverTimezone=UTC" user="root" password="1234"/>
<sql:query var="result" sql="select * from user" dataSource="${data01 }"></sql:query>
<table border="1" width="100%">
<tr>
<th>userid</th>
<th>username</th>
<th>password</th>
<th>phone</th>
</tr>

<c:forEach var="row" items="${result.rows }">
<tr>
<td><c:out value="${row.userid}"></c:out></td>
<td><c:out value="${row.username}"></c:out></td>
<td><c:out value="${row.password}"></c:out></td>
<td><c:out value="${row.phone}"></c:out></td>
</tr>
</c:forEach>
</table>
</body>
</html>
<c:set var="userid" value="112"></c:set>
<sql:query var="result" sql="select *from user where userid=?" dataSource="${data01 }">
<sql:param value="${userid }"></sql:param>
</sql:query>

插入

<sql:update var="count" dataSource="${data01 }" sql="insert into user(userid,username,password,phone) value('000','111','222','333')"></sql:update>

删除

<sql:update dataSource="${data01 }" sql="delete from user where userid='117'"></sql:update>

修改

<sql:update dataSource="${data01 }" sql="update user set username='zll' where userid='099'"></sql:update>

4.XML标签

5.JSTL标签

XML标签,JSTL标签基本上不会使用

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值