zoukankan      html  css  js  c++  java
  • Servlet 5 JSP EL JSTL

    JSP:
    JSP的java代码片段写法,声明变量,获取值
    JSP的三种注释
    JSP的session对象,内置out对象
    JSP <%@page 指令 错误页面
    JSP <%@page 指令 不对EL进行解析
    JSP <%@include 指令 包含目标页面
    使用 pageContext向jsp四个范围存取数据
    使用out输出数据
    使用<jsp:include 包含页面
    EL函数(用来获取四大域中数据):
    使用EL 获得四个 JSP数据范围 page request session application的数据
    EL 获得复杂对象数据 EL必须使用[] 情况
    EL 执行运算
    使用EL 快速读取cookie 使用EL 获得工程名
    EL 不能直接调用 java静态代码 自定义EL函数
    EL标签库(需要引入官方tld文件)
    JSTL(JAVA标准标签库):
    使用标签前,需要引入tld文件,导入jar包 jstl.jar standard.jar
    <c:catch 捕获异常标签
    <c:choose 选择标签
    <c:forEach 循环标签
    <c:if 判断标签
    <c:import 效果类似包含
    <c:out 输出标签
    <c:redirect 重定向标签
    <c:remove 删除属性标签
    <c:set 设置属性标签
    <c:forTokens 循环标记标签
    <c:url url标签,结合<c:param标签 对中文完成URL编码

    JSP:
    JSP的java代码片段写法,声明变量,获取值

    <%@ page language="java" contentType="text/html; charset=UTF-8"
        pageEncoding="UTF-8"%>
    <!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>
    <!-- 由多个% 共同组合为一个java代码片段 -->
    <%
        for(int i=0; i<10 ;i++){
            out.println(i);// i会被输出浏览器 html源码中,显示在页面上
        }
    %>
    
    <!-- 下面这种写法,for输出到页面内容 便于被HTML代码修饰 -->
    <%
        for(int i=0; i<10 ;i++){
    %>
        <h3><%=i %></h3>
    <%     
        }
    %>
    <hr/>
    <%=x %> <!-- 输出内容 0 ,成员变量值 0 -->
    <% x=5; %><!-- 将成员变量x赋值5 -->
    <% int x = 10; %> <!-- 定义局部变量x 值 10-->
    <%! int x = 0; %> <!-- 声明成员变量x 值 0 -->
    <% this.x = 20; %> <!-- 将成员变量x的值 赋值 20  -->
    <%=x %> <!-- 输出局部变量 x的值 ,10  -->
    </body>
    </html>

    JSP的三种注释

    <%@ page language="java" contentType="text/html; charset=UTF-8"
        pageEncoding="UTF-8"%>
    <!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>
    <h1>JSP 支持三种注释</h1>
    <%-- JSP注释 --%>
    <%!
        /** java文档注释 */
        public int add(int a,int b){
            return a+ b;
        }
    %>
    <%
        /* 多行注释 */
        // 单行注释
    %>
    <!-- HTML注释   -->
    
    <hr/>
    <!-- HTML注释 不能阻止java代码的执行 -->
    <% 
        /*
        String s = "abc";
        System.out.print(s);
        int a  // 编译错误
        */
    %>
    
    </body>
    </html>

    JSP的session对象,内置out对象

    <%@ page language="java" contentType="text/html; charset=UTF-8"
        pageEncoding="UTF-8"%>
    <%@ page session="false" %>
    <%@ page buffer="16kb" autoFlush="false" %>
    <%@page import="java.util.List"%>
    <!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>
    <h1>如果session属性值为true ,可以直接使用内置session 对象</h1>
    <%
        request.getSession().setAttribute("name","itcast");
    %>
    <%=request.getSession().getAttribute("name") %>
    <h1> javax.servlet包 、javax.servlet.http包、javax.servlet.jsp包 不需要导包</h1>
    <%
        Cookie[] cookie = request.getCookies();
        List list ;
    %>
    <h1>通过jsp 的 内置out对象,生成HTML源代码</h1>
    <% out.println("Hello ,jsp"); %>
    </body>
    </html>

    JSP <%@page 指令 错误页面

    <%@ page language="java" contentType="text/html; charset=UTF-8"
        pageEncoding="UTF-8"%>
    <!-- 通过 errorPage 属性 控制页面发生错误后,跳转到哪里 -->
    <%--<%@page errorPage="/jsp/5.jsp" %>--%>
    <!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>
    <!-- 发生错误的页面 -->
    <%
        int d = 1/0;
    %>
    </body>
    </html>
    <%@ page language="java" contentType="text/html; charset=UTF-8"
        pageEncoding="utf-8"%>
    <!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>
    <!-- 解决错误的页面 (友好页面) -->
    <h1>对不起 服务器出问题了,请稍后访问!</h1>
    </body>
    </html>

    JSP <%@page 指令 不对EL进行解析

    <%@ page language="java" contentType="text/html; charset=UTF-8"
        pageEncoding="UTF-8"%>
    <!-- 设置true 不对EL 进行解析  -->
    <%@ page isELIgnored="true" %>
    <!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>
    <!-- 通过设置 page指令 isELIgnored 使服务器解析或者忽略 EL表达式 -->
    <%
        request.setAttribute("age",20);
    %>
    ${requestScope.age }
    </body>
    </html>

    JSP <%@include 指令 包含目标页面

    <%@ page language="java" contentType="text/html; charset=UTF-8"
        pageEncoding="UTF-8"%>
    <!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>
    <!-- 主页 -->
    <!-- LOGO图片、菜单栏、版权信息都是相同页面,抽取公共页面 ,通过include 指令包含目标页面-->
    <%@ include file="/jsp/7/logo.jsp" %>
    <%@ include file="/jsp/7/menu.jsp" %>
    <h1>页面正文</h1>
    <%@ include file="/jsp/7/footer.jsp" %>
    </body>
    </html>
    <%@ page language="java" contentType="text/html; charset=UTF-8"
        pageEncoding="UTF-8"%>
    <h1>LOGO图片</h1>
    <%@ page language="java" contentType="text/html; charset=UTF-8"
        pageEncoding="UTF-8"%>
    <h1>菜单栏</h1>
    <%@ page language="java" contentType="text/html; charset=UTF-8"
        pageEncoding="UTF-8"%>
    <h1>版权信息</h1>

    使用 pageContext向jsp四个范围存取数据

    <%@ page language="java" contentType="text/html; charset=UTF-8"
        pageEncoding="UTF-8"%>
    <!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>
    <!-- 使用page 时候,可以直接使用this   -->
    <!-- 使用 pageContext向jsp四个范围存取数据  -->
    <%
        // 等价于 pageContext.setAttribute("info","page");
        pageContext.setAttribute("info","page",PageContext.PAGE_SCOPE);
    
        // 等价于 request.setAttribute("info","request");
        pageContext.setAttribute("info","request",PageContext.REQUEST_SCOPE);
    
        // 等价于 session.setAttribute("info","session");
        pageContext.setAttribute("info","session",PageContext.SESSION_SCOPE);
        
        // 等价于 application.setAttribute("info","application");
        pageContext.setAttribute("info","application",PageContext.APPLICATION_SCOPE);
    %>
    <%=pageContext.getAttribute("info",PageContext.PAGE_SCOPE) %>
    <%=pageContext.getAttribute("info",PageContext.REQUEST_SCOPE) %>
    <!-- 按照 page - request - session - application 顺序查找 -->
    <%=pageContext.findAttribute("info") %>
    </body>
    </html>

    使用out输出数据

    <%@ page language="java" contentType="text/html; charset=UTF-8"
        pageEncoding="UTF-8"%>
    <!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>
    <h1>使用out输出数据</h1>
    <%="Hello" %>
    <%
        out.println("AAAA");
        out.flush();// 输出内容
        response.getWriter().println("BBBB");
        out.println("CCCC");
    %>
    </body>
    </html>

    使用<jsp:include 包含页面

    <%@ page language="java" contentType="text/html; charset=UTF-8"
        pageEncoding="UTF-8"%>
    <!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>
    <!-- 主页 -->
    <!-- LOGO图片、菜单栏、版权信息都是相同页面,抽取公共页面 ,通过include 标签包含目标页面-->
    <jsp:include page="/jsp/7/logo.jsp"></jsp:include>
    <jsp:include page="/jsp/7/menu.jsp"></jsp:include>
    <h1>JSP 标签 include 页面正文</h1>
    <jsp:include page="/jsp/7/footer.jsp"></jsp:include>
    </body>
    </html>

    EL函数(用来获取四大域中数据):
    使用EL 获得四个 JSP数据范围 page request session application的数据

    <%@ page language="java" contentType="text/html; charset=UTF-8"
        pageEncoding="UTF-8"%>
    <!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>
    <h1>使用EL 获得四个 JSP数据范围 page request session application的数据 </h1>
    <%
        pageContext.setAttribute("pageinfo","my page");
        request.setAttribute("requestinfo","my request");
        session.setAttribute("sessioninfo","my session");
        application.setAttribute("applicationinfo","my application");
    %>
    ${pageScope.pageinfo }
    ${requestScope.requestinfo }
    ${sessionScope.sessioninfo }
    ${applicationScope.applicationinfo }
    <hr/>
    <!-- 如果不指定 scope, 默认调用 pageContext.findAttribute方法 -->
    <!-- findAttribute依次从 page request session application 中进行查找 -->
    ${pageinfo }
    ${requestinfo }
    ${sessioninfo }
    ${applicationinfo }
    <hr/>
    <!-- 使用EL 获取不存在属性,返回"" 而不是null -->
    传统: <%=request.getAttribute("city") %> <br/>
    EL : ${ requestScope.city}<br/>
    
    </body>
    </html>

    EL 获得复杂对象数据 EL必须使用[] 情况

    <%@ page language="java" contentType="text/html; charset=UTF-8"
        pageEncoding="UTF-8"%>
    <%@page import="java.util.List"%>
    <%@page import="java.util.ArrayList"%>
    <%@page import="java.util.Map"%>
    <%@page import="java.util.HashMap"%>
    <%@page import="cn.itcast.domain.Person"%>
    <!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>
    <!-- EL 获得复杂对象数据 -->
    <!-- 1 数组 -->
    <%
        pageContext.setAttribute("arr",new String[] {"中国","美国"});
    %>
    ${pageScope.arr[0] }
    <!-- 2 List用法和数组相同 -->
    <%
        List list = new ArrayList();
        list.add("北京");
        list.add("上海");
        pageContext.setAttribute("list",list);
    %>
    ${pageScope.list[1] }
    <!-- 3、Map集合 通过.key获得value值 -->
    <%
        Map map = new HashMap();
        map.put("aaa","111");
        map.put("bbb","222");
        pageContext.setAttribute("map",map);
    %>
    ${pageScope.map.bbb }
    <!-- 4、获得java对象 和 当中属性 -->
    <%
        Person person = new Person();
        person.setName("张三");
        person.setAge(20);
        pageContext.setAttribute("person",person);
    %>
    ${pageScope.person.name }
    ${pageScope.person.age }
    <hr/>
    <!--必须使用[] 情况 -->
    <%
        // 数组和list集合 必须用[]取值
        pageContext.setAttribute("arr",new String[] {"中国","美国"});
    %>
    ${pageScope.arr[1]}
    <%
        // 如果属性名称 含有. 特殊字符,必须用[]
        pageContext.setAttribute("aaa.bbb","itcast");                       
    %>
    ${pageScope["aaa.bbb"] }
    </body>
    </html>

    EL 执行运算

    <%@ page language="java" contentType="text/html; charset=UTF-8"
        pageEncoding="UTF-8"%>
    <%@page import="java.util.ArrayList"%>
    <%@page import="java.util.HashMap"%>
    <!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>
    <!-- EL 执行运算 -->
    <%
        pageContext.setAttribute("n1",10);
        pageContext.setAttribute("n2",20);
    %>
    ${n1+n2 }
    <%
        pageContext.setAttribute("n3","10");
        pageContext.setAttribute("n4","20");
    %>
    ${n3+n4+n5 }
    <h1>empty运算符号</h1>
    <!-- 因为user不存在,所以返回true -->
    ${empty user }
    <%
        pageContext.setAttribute("list",new ArrayList());
        pageContext.setAttribute("map",new HashMap());
    %>
    ${empty list }
    ${empty map }
    ${not empty map }
    <h1>二元表达式 </h1>
    ${(empty username)? "用户名不存在":username }
    </body>
    </html>

    使用EL 快速读取cookie 使用EL 获得工程名

    <%@ page language="java" contentType="text/html; charset=UTF-8"
        pageEncoding="UTF-8"%>
    <!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>
    <%
        //向客户端写出一个cookie
        Cookie cookie = new Cookie("company","itcast");
        cookie.setPath("/");
        cookie.setMaxAge(120);
        
        response.addCookie(cookie);
    %>
    
    <!-- 使用EL 快速读取cookie -->
    ${cookie.company.value } 
    <!-- cookie.get("company").getValue() -->
    
    <%
        // 读取cookie
        Cookie[] cookies = request.getCookies();
        if(cookies == null){
            out.print("cookie不存在!");
        }else{
            for(Cookie c : cookies){
                if(c.getName().equals("company")){
                    out.print(c.getValue());
                    return;
                }
            }
            
            out.print("cookie不存在!");
        }
    %>
    <!-- 编写一个链接 -->
    <a href="/day8/el/1.jsp" >link</a>
    <!-- 使用EL 获得工程名 -->
    <a href="${pageContext.request.contextPath }/el/1.jsp" >link</a>
    </body>
    </html>

    EL 不能直接调用 java静态代码 自定义EL函数

    <%@ page language="java" contentType="text/html; charset=UTF-8"
        pageEncoding="UTF-8"%>
    <%@page import="cn.itcast.utils.URLOperation"%>
    <!-- 通过taglib 引用 tld文件 -->
    <%@taglib uri="http://www.itcast.cn/myel" prefix="myel" %>
    <!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>
    <!-- %中直接调用 java代码 -->
    <%=URLOperation.urlencode("中国") %>
    <hr/>
    <!-- EL 不能直接调用 java静态代码 -->
    ${myel:urlencode("中国") }
    </body>
    </html>

    EL标签库(需要引入官方tld文件)

    <%@ page language="java" contentType="text/html; charset=UTF-8"
        pageEncoding="UTF-8"%>
    <!-- 引用EL 官方tld 文件 -->
    <%@taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn" %>
    <!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>
    ${fn:toLowerCase("sdfadsfASDFADSASdasDFDSFDSFADSFASDA") }
    <!-- HTML转义 -->
    ${fn:escapeXml("<a href='/day8/index.jsp'>link</a>") }
    ${fn:split("aaa,bbb,ccc",",")[2] }
    </body>
    </html>

    JSTL(JAVA标准标签库):
    使用标签前,需要引入tld文件,导入jar包 jstl.jar standard.jar
    <c:catch 捕获异常标签

    <%@ page language="java" contentType="text/html; charset=UTF-8"
        pageEncoding="UTF-8"%>
    <%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
    <!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>
    <%
        try {
            int d = 1/0;
        }catch(Exception e){
            e.printStackTrace();
        }
    %>
    <!-- 异常就会被捕获,保存page范围 e 对象中 -->
    <c:catch var="e">
        <% 
            String s = "abc";
            s.charAt(4);
        %>
    </c:catch>
    ${e.message }
    </body>
    </html>

    <c:choose 选择标签

    <%@ page language="java" contentType="text/html; charset=UTF-8"
        pageEncoding="UTF-8"%>
    <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>    
    <!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>
    <%
        int weekday = 1;
        if(weekday == 1){
            out.print("星期一");
        }else if(weekday==2){
            out.print("星期二");
        }else if(weekday==3){
            out.print("星期三");
        }else if(weekday==4){
            out.print("星期四");
        }else{
            out.print("输入无效!");
        }
    %>
    <c:set var="weekday" value="1" scope="page"></c:set>
    <c:choose>
        <c:when test="${weekday==1}">
            星期一
        </c:when>
        <c:when test="${weekday==2}">
            星期二
        </c:when>
        <c:when test="${weekday==3}">
            星期三
        </c:when>
        <c:otherwise>
            输入无效
        </c:otherwise>
    </c:choose>
    </body>
    </html>

    <c:forEach 循环标签

    <%@ page language="java" contentType="text/html; charset=UTF-8"
        pageEncoding="UTF-8"%>
    <%@page import="java.util.List"%>
    <%@page import="java.util.ArrayList"%>
    <%@page import="java.util.Map"%>
    <%@page import="java.util.HashMap"%>
    <%@taglib uri="http://java.sun.com/jsp/jstl/core"  prefix="c"%>
    <!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>
    <!-- 遍历10到100的偶数,如果数字是第3的倍数个 显示红色 -->
    <c:forEach begin="10" end="100" step="2" var="i" varStatus="status">
        <c:if test="${status.count % 3 == 0}">
            <font color="red">${i }</font>
        </c:if>
        <c:if test="${status.count % 3 != 0}">
            <font color="blue">${i }</font>
        </c:if>
    </c:forEach>
    <hr/>
    <!-- 通过foreach控制指定次数循环 -->
    <!-- 从1 遍历 10 步长 1 ,每个值保存i中 -->
    <!-- 1 - 10求和 -->
    <c:set var="sum" value="0" scope="page"></c:set>
    <c:forEach begin="1" end="10" step="1" var="i">
        ${i }
        <c:set var="sum" value="${sum + i}" scope="page"></c:set>
    </c:forEach>
    ${sum }
    <hr/>
    <!-- Map遍历 -->
    <%
        Map map = new HashMap();
        map.put("aa","11");
        map.put("bb","22");
        pageContext.setAttribute("map",map);
    %>
    <c:forEach var="entry" items="${map}">
        ${entry.key } ${entry.value }<br/>
    </c:forEach>
    <hr/>
    <!-- List遍历 -->
    <%
        List list = new ArrayList();
        list.add("111");
        list.add("222");
        pageContext.setAttribute("list",list);
    %>
    <c:forEach var="e" items="${list}">
        ${e }
    </c:forEach>
    <hr/>
    <!-- 数组遍历 -->
    <%
        int[] arr = {3,54,45,546,56,23,4};
        pageContext.setAttribute("arr",arr);
    %>
    <!-- for(int i:arr ) 这里 arr 就是 item,i就是var -->
    <!-- 将arr每个元素取出,保存page范围的i中 -->
    <c:forEach var="i" items="${arr}">
        ${i }
    </c:forEach>
    </body>
    </html>

    <c:if 判断标签

    <%@ page language="java" contentType="text/html; charset=UTF-8"
        pageEncoding="UTF-8"%>
    <!-- 引用了一个tld 中所有标签 -->
    <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
    <!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:if标签  -->
    <h1>c:if标签作用 用来取代 页面中 if条件语句的</h1>
    <%
        int m = 10;
        if(m>5){
            out.print("m的值大于5!<br/>");
        }
    %>
    <hr/>
    <!-- 使用标签 -->
    <c:set var="n" value="10" scope="page"></c:set>
    <c:if test="${n>5}"> 
        n的值大于5!
    </c:if>
    </body>
    </html>

    <c:import 效果类似包含

    <%@ page language="java" contentType="text/html; charset=UTF-8"
        pageEncoding="UTF-8"%>
    <%@taglib uri="http://java.sun.com/jsp/jstl/core"  prefix="c"%>
    <!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>
    <h1>Import</h1>
    <!-- 引入foreach.jsp 效果类似包含-->
    <c:import url="/jstl/foreach.jsp" context="/day8"></c:import>
    <hr/>
    <!-- 引入不显示,将内容保存另一个变量中 -->
    <c:import url="/jstl/foreach.jsp" context="/day8" var="content" scope="page"></c:import>
    ${content }
    </body>
    </html>

    <c:out 输出标签

    <%@ page language="java" contentType="text/html; charset=UTF-8"
        pageEncoding="UTF-8"%>
    <!-- 使用标签库前 必须先导入 tld 中标签 -->
    <%@taglib uri="http://java.sun.com/jsp/jstl/core"  prefix="c"%>
    <%@taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn"%>
    <!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 输出数据到浏览器 -->
    <c:out value="Hello c out "></c:out>
    Hello c out 
    <!-- 输出一个变量 -->
    <c:set var="m" value="10" scope="page"/>
    <c:out value="${m}"></c:out>
    ${m }
    <!-- 转义HTML 默认转义,通过设置escapeXml 为false 不进行转义-->
    <c:out value="<a href='xxx'>link</a>" />
    ${fn:escapeXml("<a href='xxx'>link</a>") }
    <!-- 允许输出默认值 ,如果city不存在,输出北京-->
    <c:out value="${city}" default="北京"></c:out>
    ${empty city?"北京":city }
    </body>
    </html>

    <c:redirect 重定向标签

    <%@ page language="java" contentType="text/html; charset=UTF-8"
        pageEncoding="UTF-8"%>
    <%@taglib uri="http://java.sun.com/jsp/jstl/core"  prefix="c"%>
    <!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>
    <%    
        // 以前重定向
        // response.sendRedirect("/day8/index.jsp");
    %>
    <c:redirect url="/index.jsp" context="/day8"></c:redirect>
    </body>
    </html>

    <c:remove 删除属性标签

    <%@ page language="java" contentType="text/html; charset=UTF-8"
        pageEncoding="UTF-8"%>
    <%@taglib uri="http://java.sun.com/jsp/jstl/core"  prefix="c"%>
    <!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>
    <%
        request.setAttribute("age",20);
        // 删除age
        request.removeAttribute("age");
    %>
    
    <c:set var="age" value="20" scope="request"></c:set>
    <c:remove var="age" scope="request"/>
    </body>
    </html>

    <c:set 设置属性标签

    <%@ page language="java" contentType="text/html; charset=UTF-8"
        pageEncoding="UTF-8"%>
    <%@page import="cn.itcast.domain.Person"%>
    <%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
    <!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>
    <!-- 保存一个数据到四个数据范围 -->
    <%
        request.setAttribute("name","itcast");
    %>
    <c:set var="name" value="itcast" scope="request"></c:set>
    
    <!-- 设置一个保存在四个范围的java对象的属性值 -->
    <%
        Person person = new Person();
        person.setName("张三");
        pageContext.setAttribute("person",person);
    %>
    <c:set target="${person}" property="name" value="李四"></c:set>
    ${person.name }
    </body>
    </html>

    <c:forTokens 循环标记标签

    <%@ page language="java" contentType="text/html; charset=UTF-8"
        pageEncoding="UTF-8"%>
    <%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
    <!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="s" value="aaa,bbb,ccc" scope="page"></c:set>
    <c:forTokens items="${s}" delims="," var="e">
        ${e }
    </c:forTokens>
    </body>
    </html>

    <c:url url标签,结合<c:param标签 对中文完成URL编码

    <%@ page language="java" contentType="text/html; charset=UTF-8"
        pageEncoding="UTF-8"%>
    <%@taglib uri="http://java.sun.com/jsp/jstl/core"  prefix="c"%>
    <!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>
    <%
        String url = "/day8/index.jsp";
        url = response.encodeURL(url);
    %>
    <!-- 将/day8/index.jsp 进行url重写,保存page范围 myurl中 -->
    <c:url value="/index.jsp" context="/day8" var="myurl" scope="page" />
    
    <%=url %>
    ${myurl }
    
    <!-- 通过c:url 结合 c:param 对中文完成URL编码 -->
    <c:url value="/login" context="/day8" var="myurl2" scope="page">
        <c:param name="username" value="张三"></c:param>
    </c:url>
    ${myurl2 }
    </body>
    </html>

     通用404页面

    <%@ page language="java" contentType="text/html; charset=UTF-8"
        pageEncoding="UTF-8"%>
    <!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>
    <h1>通用404错误页面</h1>
    </body>
    </html>

    通用500页面

    <%@ page language="java" contentType="text/html; charset=UTF-8"
        pageEncoding="UTF-8"%>
    <!-- 获得错误信息  isErrorPage="true" -->
    <%@page isErrorPage="true" %>
    <!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>
    <!-- 通用错误页面 -->
    <h1>通用500错误页面</h1>
    <h2>错误原因 <% 
        if(exception !=null){
            out.println(exception.getMessage());
            if(exception.getCause()!=null){
                out.println(exception.getCause().getMessage());
            }
        }
    %></h2>
    </body>
    </html>

    Person类:

    package cn.itcast.domain;
    
    public class Person {
        private String name;
        private int age;
    
        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;
        }
    
    }

    StringCutter类:

    package cn.itcast.utils;
    
    import java.util.StringTokenizer;
    
    /**
     * 切割字符串
     * 
     * @author seawind
     * 
     */
    public class StringCutter {
        public static void main(String[] args) {
            String url = "http://mail.itcast.cn?name=zhangsan&pwd=123";
            // 切出zhangsan 和 123
            // 第一种 indexOf substring
            String part1 = url.substring(url.indexOf("?") + 1);
            String part2 = part1.substring(part1.indexOf("&") + 1);
            String part3 = part1.substring(0, part1.indexOf("&"));
            System.out.println(part2 + "," + part3);
    
            // 第二种 split
            String[] arr = url.split("\?|&");
            System.out.println(arr[1] + "," + arr[2]);
    
            // 第三种 StringTokenizer
            StringTokenizer stringTokenizer = new StringTokenizer(url, "?&");
            while (stringTokenizer.hasMoreTokens()) {
                String s = stringTokenizer.nextToken();
                System.out.println(s);
            }
        }
    }

    URLOperation类:

    package cn.itcast.utils;
    
    import java.io.UnsupportedEncodingException;
    import java.net.URLEncoder;
    
    public class URLOperation {
        /**
         * 将内容进行URL编码
         * 
         * @param content
         * @return
         */
        public static String urlencode(String content) {
            try {
                return URLEncoder.encode(content, "utf-8");
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
                throw new RuntimeException(e);
            }
        }
    }
  • 相关阅读:
    LIBSVM
    tf-idf
    DIV+CSS例子
    网页背景设置
    获取JDBC中的ResultSet的记录的条数
    SQL 基本语句
    经典SQL语句大全
    JS(截取字符串,显示当前系统时间yyyy-MM-dd,从文本框得到的数值计算)
    JavaScript实现全排列
    Java发送邮件
  • 原文地址:https://www.cnblogs.com/vaer/p/3929838.html
Copyright © 2011-2022 走看看