jsp

来源:互联网 发布:javascript知识点总结 编辑:程序博客网 时间:2024/06/05 04:43

一、jsp隐式对象:

JSP隐式对象是JSP容器为每个页面提供的Java对象,开发者可以直接使用它们而不用显式声明,使用时直接.方法即可。JSP隐式对象也被称为预定义变量。jsp的九大内置对象:



作用域:pageContext(页面有效)<  request(response)<  session  <application

二、JSP指令标签:



三、JSP 动作元素:

JSP动作元素在请求处理阶段起作用。JSP动作元素是用XML语法写成的。利用JSP动作可以动态地插入文件、重用JavaBean组件、把用户重定向到另外的页面、为Java插件生成HTML代码。动作元素只有一种语法,它符合XML标准:

这里讲一下<jsp:include page=""></jsp:include>和<%  include>的区别,前者会生成两个servlet文件,它的过程是首先单独编译返回html代码,再和另一个文件的html组合在一起。而<% include>编译时是只生成一个servlet文件,它是把页面内容组合在一起,再使用jsp引擎进行解析成xxjsp.java。如果在两个文件中都定义了int   a=10;那么<% include>会报错,因为重复定义了,而前者则不会。

四、jstl标签库和el标签库:这两个标签库是解决jsp页面中出现java代码的地方(<% %>标签是写java代码的地方)。这里讲下jstl标签库:引用JSTL标签库,首先要在lib目录下添加jar包,其次需要在代码中引入相应的标签库。

1、核心标签库
核心标签是最常用的JSTL标签。引用核心标签库的语法如下:

<%@ taglib prefix="c"            uri="http://java.sun.com/jsp/jstl/core" %>
解决页面上出现java代码的地方。


2、格式化标签
JSTL格式化标签用来格式化并输出文本、日期、时间、数字。引用格式化标签库的语法如下:

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

3、JSTL函数
JSTL包含一系列标准函数,大部分是通用的字符串处理函数。引用JSTL函数库的语法如下:

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



例1:<c:set>和<c:out>

<%@ page language="java" contentType="text/html; charset=utf-8"    pageEncoding="utf-8"%><%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %><!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8"><title>Insert title here</title></head><body><c:set var="key1" value="hello" scope="session"></c:set><c:out value="${sessionScope.key1}"></c:out></body></html>
这里sessionScope.key1其实完全可以直接写key1,它会根据这个Key值自动按作用域从小到大去搜索。pageScope < requestScope  <sessionScope  <application。
例2:<c:if>

<%@ page language="java" contentType="text/html; charset=utf-8"    pageEncoding="utf-8"%><%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %><!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8"><title>Insert title here</title></head><body><c:set var="key1" value="hello" scope="session"></c:set><c:if test="${key1=='hello'}">     <h1>结果是对的</h1></c:if></body></html>

例3:<c:choose>、<c:when>和 <c:otherwise>相当于switch、default

<%@ page language="java" contentType="text/html; charset=utf-8"    pageEncoding="utf-8"%><%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %><!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8"><title>Insert title here</title></head><body><c:set var="key1" value="hello" scope="session"></c:set><c:choose>   <c:when test="${key1=='hello'}">       <h1>是hello</h1>   </c:when>   <c:when test="${key1=='hello1'}">       <h1>是hello1</h1>   </c:when>   <c:otherwise>         <h1>不是hello1</h1>   </c:otherwise></c:choose></body></html>
例4:循环<c:forEach>

<%@page import="java.util.ArrayList"%><%@page import="java.util.List"%><%@ page language="java" contentType="text/html; charset=utf-8"    pageEncoding="utf-8"%><%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %><!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8"><title>Insert title here</title></head><body><%List<String> list=new ArrayList<String>();list.add("hello");list.add("world");list.add("java");pageContext.setAttribute("list",list );%><c:forEach items="${list}" var="li">   <c:out value="${li}"></c:out></c:forEach></body></html>

<%@page import="com.jspdemo.User"%><%@page import="java.util.ArrayList"%><%@page import="java.util.List"%><%@ page language="java" contentType="text/html; charset=utf-8"    pageEncoding="utf-8"%><%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %><!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8"><title>Insert title here</title></head><body><%List<User> list=new ArrayList<User>();User user=new User("zs","123");list.add(user);user=new User("lisi","123");list.add(user);pageContext.setAttribute("list",list );%><c:forEach items="${list}" var="per">   ${per.name}<br>   ${per.pwd}<br></c:forEach></body></html>

例5:<c:url>给页面带参数,防止手写出错

<%@page import="com.jspdemo.User"%><%@page import="java.util.ArrayList"%><%@page import="java.util.List"%><%@ page language="java" contentType="text/html; charset=utf-8"    pageEncoding="utf-8"%><%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %><!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8"><title>Insert title here</title></head><body><c:url var="baidu" value="https://www.baidu.com/">  <c:param name="name" value="zs"></c:param>  <c:param name="id" value="10001"></c:param></c:url><c:out value="${baidu}"></c:out></body></html>
运行结果为:



例6:<f:formatDate>

package com.jspdemo;import java.io.IOException;import java.util.Date;import javax.servlet.ServletException;import javax.servlet.annotation.WebServlet;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;@WebServlet(name="DateServlet",urlPatterns="/time")public class DateServlet extends HttpServlet{@Overrideprotected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {Date date=new Date();req.setAttribute("time", date);req.getRequestDispatcher("index1.jsp").forward(req, resp);}@Overrideprotected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {doGet(req, resp);}}


<%@ page language="java" contentType="text/html; charset=utf-8"    pageEncoding="utf-8"%><%@ taglib prefix="f" uri="http://java.sun.com/jsp/jstl/fmt" %><!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8"><title>Insert title here</title></head><body><f:formatDate value="${time}" pattern="yyyy-MM-dd hh:mm:ss"/></body></html>

运行:


例7:函数fn:length

<%@ page language="java" contentType="text/html; charset=UTF-8"    pageEncoding="UTF-8"%>    <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>    <%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %><!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><title>Insert title here</title></head><body><c:out value="${fn:length('1234')}"></c:out></body></html>

输出4