Java Web之六 JSP & JSP标签技术

1.jsp技术
        jsp是sun提供动态web资源开发技术。为了解决在Servlet中拼写html内容css、js内容十分不方便的问题,sun提供了这样一门技术。如果说Servlet是在java中嵌套HTML,则jsp就是在HTML中嵌套java代码,从而十分便于组织html页面
        jsp页面在第一次被访问到时会被jsp翻译引擎翻译成一个Servlet,从此对这个jsp页面的访问都是由这个Servlet执行后进行输出
        
2.jsp语法

    (1)JSP模版元素 :

jsp页面中书写的HTML内容称作JSP的模版元素,在翻译过来的Servlet中直接被out.write()输出到浏览器页面上了

    (2)JSP表达式  <%= java表达式 %>

 在翻译过来的Servlet中,计算java表达式的值后,被out输出到浏览器上

<%= new Date().toLocaleString() %>

    (3)JSP脚本片断  <% 若干java语句 %> 

在翻译过来的Servlet中,直接被复制粘贴到了对应的位置执行.

在一个JSP页面中可以有多个脚本片断,在两个或多个脚本片断之间可以嵌入文本、HTML标记和其他JSP元素
多个脚本片断中的代码可以相互访问,犹如将所有的代码放在一对<%%>之中的情况
单个脚本片断中的Java语句可以是不完整的,但是,多个脚本片断组合后的结果必须是完整的Java语句
<%
for(int i=0;i<5;i++){
num++;
%>
<font color="blue">
<%
out.write(num+"");
%>
</font>
<%
} 
%> 


    (4)JSP声明  <%! 若干java语句 %> 

在翻译过来的Servlet中会被放置到和Service方法同级的位置,变成了类的一个成员

<%! int i = 0; %>
<%! public void method1(){} %>
<%! static{} %>
<%! {} %>
<%! class someclass{} %>


            
    (5)JSP注释 <%-- 注释的内容 --%> 
 <%-- 注释的内容 --%> 被jsp注释注释掉的内容,在jsp翻译引擎将jsp翻译成Servlet的过程中会被丢弃,在翻译过来的Servlet中没有这些信息
<%//java注释%> java注释被当作jsp脚本片段被翻译到了Servlet中,在.java文件被翻译成.class文件的时候注释信息被丢弃
 <!-- HTML注释 --> html注释被当作模版元素输出到了浏览器上,浏览器认识html注释不予显示
       
   (6)JSP指令<%@ 指令名称 属性=... 属性=...%>
JSP指令(directive)是为JSP引擎而设计的,它们并不直接产生任何可见输出,而只是告诉引擎如何处理JSP页面中的其余部分
            page指令
[ language="java" ] 
[ extends="package.class" ] 
[ import="{package.class | package.*}, ..." ] 
[ session="true | false" ] 
[ buffer="none | 8kb | sizekb" ] 
[ autoFlush="true | false" ] 
[ isThreadSafe="true | false" ] 
[ errorPage="relative_url" ] 
[ isErrorPage="true | false" ] 
[ contentType="mimeType [ ;charset=characterSet ]" | "text/html ; charset=ISO-8859-1" ] 
[ pageEncoding="characterSet | ISO-8859-1" ] 
[ isELIgnored="true | false" ]


① import
	<%@page import="java.util.Date"%>
	
② session 
	>取值为false时,当前页面的session隐藏变量不可用。默认为true
		<%@page session="false" %>
	
③ errorPage和isErrorPage
	>errorPage指定若当前的页面错误,则相应什么
		<%@page errorPage="/errorPage.jsp" %>
	>isErrorPage 说明当前页面是否为错误处理页面,为true时,可以使用exception隐藏对象
		<%@page isErrorPage="true" %>
		<body>
			<h1>Error Page!</h1>
			<%=exception.getMessage()%>
		</body>
	>可以在web.xml中配置错误页面
		<error-page>
			<error-code>404</error-code>
			<location>/error.jsp</location>
		</error-page>
		<error-page>
			<exception-type>java.lang.ArithmeticException</exception-type>
			<location>/error.jsp</location>
		</error-page>
		
④ contentType
	>指定当前页面的相应类型。实际调用的是response.setContentType("text/html; charset");
		<%@page contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
		
⑤ pageEncoding
	>指定当前页面的编码,通常和contentType中的charset相同
		<%@page pageEncoding="UTF-8"%>
	
⑥ isELIgnored
	>指定当前页面是否可以使用EL表达式,通常取值为true
		<%@page isELIgnored="true"%>

Include指令:

include指令用于引入其它JSP页面,如果使用include指令引入了其它JSP页面,那么JSP引擎将把这两个JSP翻译成一个servlet

只有include指令进行的包含是静态包含,其他的包含都是动态包含

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

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


   (7)JSP四大作用域及九大隐式对象:

四大作用域:

page -> 页面级别。如果一个数据只在当前jsp页面使用,可以使用page域存储

request -> 请求级别。如果一个数据,除了在当前Servlet中使用,还要在请求转发时带到其他Servlet处理或jsp中显示,这个时候用request域

session -> 会话级别。如果一个数据,除了现在我自己要用,过一会我自己还要用,存在session域

application -> 应用级别,服务器重启时消失。如果一个数据,除了现在我自己要用过一会其他人也要用,存在application 域中


九大隐式对象:在翻译过来的Servlet中Service方法自动帮我们前置定义的九个对象,可以在jsp页面中直接使用

 

对象类型对象(*为常用)类型作用域
请求对象request(*)         javax.servlet.ServletRequest Request
响应对象response(*)        javax.servlet.SrvletResponsePage
页面上下文对象pageContext  javax.servlet.jsp.PageContextPage
会话对象session(*)           javax.servlet.http.HttpSessionSession
应用程序对象application(*)      javax.servlet.ServletContextApplication
输出对象 out(*)                 javax.servlet.jsp.JspWriterPage
配置对象config              javax.servlet.ServletConfigPage
页面对象page                javax.lang.Object  Page
例外对象exception        javax.lang.Throwable Page


          
      out
相当于是response.getWriter得到PrintWriter
out和response.getWriter获取到的流不同在于,在于这个out对象本身就具有一个缓冲区.利用out写出的内容,会先缓冲在out缓冲区中,直到out缓冲区满了或者整个页面结束时out缓冲区中的内容才会被写出到response缓冲区中,最终可以带到浏览器页面进行展示
page指令中的 [buffer="none | 8kb | sizekb" ]可以用来禁用out缓冲区或设置out缓冲区的大小,默认8kb 
[ autoFlush="true | false"]用来设置当out缓冲区满了以后如果在写入数据时out如何处理,如果是true,则先将满了的数据写到response中后再接受新数据,如果是false,则满了再写入数据直接抛异常 
在jsp页面中需要进行数据输出时,不要自己获取response.getWriter,而是要使用out进行输出,防止即用out又用response.getWriter而导致输出顺序错乱的问题
          
      pageContext
            (1)可以作为入口对象获取其他八大隐式对象的引用
                getException方法返回exception隐式对象 
                getPage方法返回page隐式对象
                getRequest方法返回request隐式对象 
                getResponse方法返回response隐式对象 
                getServletConfig方法返回config隐式对象
                getServletContext方法返回application隐式对象
                getSession方法返回session隐式对象 
                getOut方法返回out隐式对象
            (2)域对象,四大作用域的入口,可以操作四大作用域中的域属性
                作用范围: 当前jsp页面
生命周期: 当对jsp页面的访问开始时,创建代表当前jsp的PageContext,当对当前jsp页面访问结束时销毁代表当前jsp的pageContext
                作用:在当前jsp中共享数据
                    public void setAttribute(java.lang.String name,java.lang.Object value)
                    public java.lang.Object getAttribute(java.lang.String name)
                    public void removeAttribute(java.lang.String name)

                    public void setAttribute(java.lang.String name, java.lang.Object value,int scope)
                    public java.lang.Object getAttribute(java.lang.String name,int scope)
                    public void removeAttribute(java.lang.String name,int scope)
                    
                    PageContext.APPLICATION_SCOPE
                    PageContext.SESSION_SCOPE
                    PageContext.REQUEST_SCOPE
                    PageContext.PAGE_SCOPE 

四大作用域的入口,可以操作四大作用域中的域属性:
<%
pageContext.setAttribute("name","赵敏",PageContext.REQUEST_SCOPE);
%>
<%= pageContext.getAttribute("name",PageContext.REQUEST_SCOPE)) %>


findAttribute方法-- 搜寻四大作用域中的属性,如果找到则返回该值,如果四大作用域中都找不到则返回一个null,搜寻的顺序是从最小的域开始向最大的域开始寻找
<%
pageContext.setAttribute("name","赵敏",PageContext.REQUEST_SCOPE);
//request.setAttribute("name","张无忌");
//session.setAttribute("name","周芷若");
//application.setAttribute("name","小昭");
%>
<%= pageContext.findAttribute("name") %>

            (3)提供了请求转发和请求包含的快捷方法
//request.getRequestDispatcher("/index.jsp").include(request,response); 
pageContext.include("/index.jsp");
pageContext.forward("/index.jsp");


        execption: 在声明了isErrorPage后才可使用

       <%@ page isErrorPage="true" %>

3.零散知识
        (1)jsp映射
        <servlet>
       <servlet-name>index</servlet-name>
       <jsp-file>/index.jsp</jsp-file>
       </servlet>
        <servlet-mapping>
       <servlet-name>index</servlet-name>
       <url-pattern>/jsp/*</url-pattern>
       </servlet-mapping>


         

(2)和属性相关的方法

1)方法
Object getAttribute(String name);
Enumeration getAttributeNames();
removeAttribute(String name);
setAttribute(String name, Object o);
2)PageContext、request、session、application都有这些方法  
        
        (3)域的总结
            servletContext (application)
            session (session)
            request (request)
            pageContext

            
如果一个数据只在当前jsp页面使用,可以使用pageContext域存储
如果一个数据,除了在当前Servlet中使用,还要在请求转发时带到其他Servlet处理或jsp中显示,这个时候用request域
如果一个数据,除了现在我自己要用,过一会我自己还要用,存在session域

如果一个数据,除了现在我自己要用过一会其他人也要用,存在ServletContext域中


         

1.jsp标签:sun原生提供的标签直接在jsp页面中就可以使用
     <jsp:include> -- 实现页面包含,动态包含

//pageContext.include("/index.jsp");

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

     <jsp:forward> -- 实现请求转发
     <jsp:forward page="/index.jsp"></jsp:forward>
     <jsp:param> -- 配合上面的两个标签使用,在请求包含和请求转发时用来在路径后拼接一些请求参数
<jsp:forward page="/index.jsp">
<jsp:param name="name" value="lm">

</jsp:forward>


2.EL表达式:${el表达式}
    (1)获取数据
使用中括号的地方都可以使用点号替代,除了中括号中是数字或者中括号中包含特殊字符(-.)的情况除外
在中括号中如果不用双引号引起来则是变量,先找变量的值再拿变量的值使用.如果用双引号则是常量,直接使用常量的值
    

      1)获取常量  字符串/数字/布尔类型,直接写在el表达式中,el直接进行输出

      2)获取域中的变量   (如果el中写的是一个变量的名,则el会调用pageContext的findAttribute方法,在四大作用域中以给定的名字找对应的属性值,找到后进行输出,如果四个域中都找不到,什么都不输出)

      3)获取数组中的数据

      4)获取集合中的数据

      5)获取Map中的数据 

      6)获取javabean的属性 

<h1>获取javabean的属性</h1><hr>
<%
<span style="white-space:pre">	</span>Person p = new Person();
<span style="white-space:pre">	</span>p.setName("芙蓉姐姐");
<span style="white-space:pre">	</span>p.setAge(17);
<span style="white-space:pre">	</span>pageContext.setAttribute("p",p);
%>
${p.name }${p.age } ${p.name }
<img src="${pageContext.request.contextPath }/1.jpg"/>

<h1>获取Map中的数据</h1><hr>
<%
<span style="white-space:pre">	</span>Map<String,String> map = new HashMap();
<span style="white-space:pre">	</span>map.put("name","奥巴马");
<span style="white-space:pre">	</span>map.put("age","17");
<span style="white-space:pre">	</span>map.put("gender","男");
<span style="white-space:pre">	</span>map.put("addr","白宫");
<span style="white-space:pre">	</span>map.put("name.xiao","小黑黑");
<span style="white-space:pre">	</span>pageContext.setAttribute("map",map);
<span style="white-space:pre">	</span>pageContext.setAttribute("name","age");
%>
${map["name"] }${map["addr"] }
${map.gender }${map.age }
${map["name.xiao"] }

<h1>获取集合中的数据</h1><hr>
<%
<span style="white-space:pre">	</span>List<String> list = new ArrayList<String>();
<span style="white-space:pre">	</span>list.add("甄嬛");
<span style="white-space:pre">	</span>list.add("安玲荣");
<span style="white-space:pre">	</span>list.add("凤姐");
<span style="white-space:pre">	</span>pageContext.setAttribute("list",list);
%>
${list[2] }

<h1>获取数组数据</h1><hr>
<%
<span style="white-space:pre">	</span>String [] names = {"容嬷嬷","紫薇","金锁","小燕子"};
<span style="white-space:pre">	</span>pageContext.setAttribute("names",names);
%>
${names[2]}
<h1>获取域中的变量数据</h1><hr>
<%
<span style="white-space:pre">	</span>String name = "灭绝师太";
<span style="white-space:pre">	</span>pageContext.setAttribute("name",name);
%>
${name }
<h1>获取常量数据</h1><hr>
${"阿萨德发射点发" } ${199 } ${true }

   (2)执行运算
        算数运算
            +-*/
        逻辑运算
        比较运算
       
        三元运算符

        empty运算符


<h1>empty 运算符 : 判断一个对象是否为null或字符串是否为空字符串或集合内容是否为空或域中是否没有任何属性</h1><hr>
<%
<span style="white-space:pre">	</span>String str = "aaaa";
<span style="white-space:pre">	</span>pageContext.setAttribute("str",str);
<span style="white-space:pre">	</span>List list = new ArrayList();
<span style="white-space:pre">	</span>list.add("");
<span style="white-space:pre">	</span>pageContext.setAttribute("list",list);
%>
${empty str }
${empty list}
${empty pageScope }
<h1>三元运算</h1><hr>
<span style="white-space:pre">	</span>${10>9? "yes" : "no" }  
<h1>逻辑运算</h1><hr>
<span style="white-space:pre">	</span>&& and 
<span style="white-space:pre">	</span>||  or
<span style="white-space:pre">	</span>!   not
<span style="white-space:pre">	</span>${not (3>2 and 1<2 or 10>3) }
<h1>比较运算</h1><hr>
<span style="white-space:pre">	</span>> gt
<span style="white-space:pre">	</span>< lt
<span style="white-space:pre">	</span>>= ge
<span style="white-space:pre">	</span><= le
<span style="white-space:pre">	</span>!= ne
<span style="white-space:pre">	</span>==eq
<span style="white-space:pre">	</span>${1 eq 1 } ${3 ge 2 } ${5+3 lt 3 }
<h1>算术运算:如果有非数字参与算数运算,el表达式会试图将非数字转换为数字后参与运算</h1><hr>
<span style="white-space:pre">	</span>${1+1 }
<span style="white-space:pre">	</span>${1-2 }
<span style="white-space:pre">	</span>${1+"2" }


        
    (3)获取常用开发对象:el中内置了11个内置对象,这些对象el内置的,不需要提前定义就可以直接在el中使用
        pageContext -- 有了它可以很方便的获取jsp页面中的9大隐式对象
        
        pageScope -- page域中属性组成的Map
        requestScope -- request域中属性组成的Map
        sessionScope -- session域中属性组成的Map
        applicationScope --application域中属性组成的Map
        
        param -- 所有请求参数组成的Map<String,String>
        paramValues -- 所有请求参数组成的Map<String,String[]>

?name=li&age=19&like=footbale&like=basketbale
${param.name}
${paramValues.like[0]}

        header -- 所有请求头组成的Map<String,String>
        headerValues -- 所有请求头组成的Map<String,String[]>

${header.HOST}
        
        cookie -- 所有cookie信息组成的Map<String,Cookie>
${cookie.JSESSIONID.NAME}${cookie.JSESSIONID.VALUE}
        initParam -- 所有web应用的初始化参数组成Map
${initParam.name2}
        
            
    (4)调用java方法: -- 不需要大家自己会写调用方法的过程,只要会调用别人写好的标签库就可以了 fn标签库
   ~写一个类其中包含要被el调用的方法,这个方法必须是静态的方法
   ~写一个tld文件在其中对要被调用的静态方法进行一下描述
   ~在jsp页面中taglib指令将tld文件引入当前jsp页面,从而在jsp页面中就可以调用描述好的方法  
<%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn" %>
${fn:toUpperCase("asdfasdfKJKJKJdsagHIHidsfgGUGu") }

fn:

1、fn:contains(string, substring) 

如果参数string中包含参数substring,返回true。

2、fn:containsIgnoreCase(string, substring) 

如果参数string中包含参数substring(忽略大小写),返回true

3、fn:endsWith(string, suffix) 

如果参数 string 以参数suffix结尾,返回true。

4、fn:escapeXml(string) 

将有特殊意义的XML (和HTML)转换为对应的XML character entity code,并返回。

5、fn:indexOf(string, substring) 

返回参数substring在参数string中第一次出现的位置。

6、fn:join(array, separator) 

将一个给定的数组array用给定的间隔符separator串在一起,组成一个新的字符串并返回。 

7、fn:length(item) 

返回参数item中包含元素的数量。参数Item类型是数组、collection或者String。如果是String类型,返回值是String中的字符数。

8、fn:replace(string, before, after) 

返回一个String对象。用参数after字符串替换参数string中所有出现参数before字符串的地方,并返回替换后的结果。

9、fn:startsWith(string, prefix) 

如果参数string以参数prefix开头,返回true。

10、fn:substring(string, begin, end) 

返回参数string部分字符串, 从参数begin开始到参数end位置。

11、fn:substringAfter(string, substring) 

返回参数substring在参数string中后面的那一部分字符串。

12、fn:substringBefore(string, substring) 

返回参数substring在参数string中前面的那一部分字符串。

13、fn:toLowerCase(string) 

将参数string所有的字符变为小写,并将其返回。

14、fn:toUpperCase(string) 

将参数string所有的字符变为大写,并将其返回。

15、fn:trim(string) 

去除参数string 首尾的空格,并将其返回。

    <%  
        String a[] = {"aa","bb","cc","dd"};  
        request.setAttribute("array",a);  
        request.setAttribute("store","guomei8899");  
        request.setAttribute("user","u1,u2,u3,u4,u5");  
        request.setAttribute("test","aBcDeF   ");  
    %>   
    <c:if test="${fn:contains('guomeiddd','guoMei')}">ok</c:if><br>  
    <c:if test="${fn:containsIgnoreCase(store,'guoMei')}">ok ok</c:if><br>  
    <c:if test="${fn:endsWith(store,'99')}">end</c:if><br>  
    <c:out value="${fn:escapeXml('<>')}"/><br>  
    <c:out value="${fn:indexOf(store,'om')}"/><br>  
    <c:out value="${fn:join(array,'|')}"/><br>  
    <c:out value="${fn:length(array)}"/><br>  
    <c:out value="${fn:replace(store,'8','9')}"/><br>  
    <c:out value="${fn:split(user,',')}"/><br>  
    <c:out value="${fn:startsWith(store,'g')}"/><br>  
    <c:out value="${fn:substring(store,2,5)}"/><br>  
    <c:out value="${fn:substringAfter(store,'mei')}"/><br>  
    <c:out value="${fn:substringBefore(store,'mei')}"/><br>  
    <c:out value="${fn:toLowerCase(test)}"/><br>  
    <c:out value="${fn:toUpperCase(test)}"/><br>  
    <c:out value="${test}hoho"/><br>  
    <c:out value="${fn:trim(test)}hoho"/><br>  


3.JSTL:

1)加入jar包:

jstl.jar、standard.jar

2)增加tagli指令

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


    <c:out> 标签用于输出一段文本内容到pageContext对象当前保存的“out”对象中。
<h1>HTML转义输出</h1><hr>
<a href="#">xxx</a>
<c:out value="<a href='#'>xxx</a>" ></c:out>
${fn:escapeXml('<a href="#">xxx</a>') }
<h1>输出默认值</h1><hr>
<%
<span style="white-space:pre">	</span>String addr = "西二旗";
<span style="white-space:pre">	</span>//pageContext.setAttribute("addr",addr);
%>
<c:out value="${addr}" default="北京"></c:out>
${addr == null?"北京" : addr }
<h1>输出变量</h1><hr>
<%
<span style="white-space:pre">	</span>String name = "无双";
<span style="white-space:pre">	</span>pageContext.setAttribute("name",name);
%>
<c:out value="${name}"></c:out>
${name }
<h1>输出常量</h1><hr>
<c:out value="阿斯蒂芬"></c:out>
${"啦啦啦啦" }


<c:set>标签用于把某一个对象存在指定的域范围内,或者设置Web域中的java.util.Map类型的属性对象或JavaBean类型的属性对象的属性。  
<h1>修改域中的JavaBean的属性的值</h1><hr>
<%
<span style="white-space:pre">	</span>Person p = new Person();
<span style="white-space:pre">	</span>pageContext.setAttribute("p",p);
%>
<c:set target="${p}" property="name" value="克林顿"></c:set>
${p.name }  
<h1>设置或修改域中的Map的值</h1><hr>
<%
<span style="white-space:pre">	</span>Map map = new HashMap();
<span style="white-space:pre">	</span>pageContext.setAttribute("map",map);
%>
<c:set target="${map}" property="cellphone" value="10010"></c:set>  
<c:set target="${map}" property="cellphone" value="10086"></c:set>  
${map.cellphone }
<h1>设置或修改域中的属性值</h1><hr>
<c:set var="name" value="韦小宝"></c:set>
<c:set var="name" value="阿珂"></c:set>
${name }


<c:remove>标签用于删除各种Web域中的属性
 <%
<span style="white-space:pre">	</span>pageContext.setAttribute("name","蓝精灵");
<span style="white-space:pre">	</span>request.setAttribute("name","伏地魔");
<span style="white-space:pre">	</span>session.setAttribute("name","机器猫");
<span style="white-space:pre">	</span>application.setAttribute("name","蜡笔小新");
%>
<c:remove var="name"/>
${name }


<c:catch>标签用于捕获嵌套在标签体中的内容抛出的异常
<c:catch var="e">
<%
<span style="white-space:pre">	</span>int i = 1/0;
%>
</c:catch>
${e.message }


<c:if test="">标签可以构造简单的“if-then”结构的条件表达式 
<c:if test="${2>1}">
确实是这样的....
</c:if>
<c:if test="${2<=1}">
你确定吗?
</c:if>


<c:choose>标签用于指定多个条件选择的组合边界,它必须与<c:when>和<c:otherwise>标签一起使用
<c:choose>
<c:when test="${day == 1}">
星期一
</c:when>
<c:when test="${day == 2}">
星期二
</c:when>
<c:when test="${day == 3}">
星期三
</c:when>
<c:when test="${day == 4}">
星期四
</c:when>
<c:when test="${day == 5}">
星期五
</c:when>
<c:otherwise>
休息日!
</c:otherwise>
</c:choose>


<c:forEach>标签用于对一个集合对象中的元素进行循环迭代操作,或者按指定的次数重复迭代执行标签体中的内容。
<h1>实验:遍历10到100的偶数,如果数字所在的位置是3的倍数,显示成红色</h1><hr>
<c:forEach begin="10" end="100" step="2" var="i" varStatus="stat">
<c:if test="${stat.count % 3 == 0}">
<font color="red">
${i }
</font>
</c:if>
<c:if test="${stat.count % 3 != 0}">
<font color="blue">
${i }
</font>
</c:if>
</c:forEach>
 
<h1>循环执行指定的内容若干次</h1><hr>
<c:forEach begin="0" end="10" step="2" var="i" >
${i },
</c:forEach>
<h1>遍历Map中的数据</h1><hr>
<%
<span style="white-space:pre">	</span>Map map = new LinkedHashMap();
<span style="white-space:pre">	</span>map.put("name","曹操");
<span style="white-space:pre">	</span>map.put("age","59");
<span style="white-space:pre">	</span>map.put("wife","小乔");
<span style="white-space:pre">	</span>map.put("gender","男");
<span style="white-space:pre">	</span>pageContext.setAttribute("map",map);
%>
<c:forEach items="${map}" var="entry" >
${entry.key }:${entry.value }<br>
</c:forEach>
<h1>遍历集合中的数据</h1><hr>
<%
<span style="white-space:pre">	</span>List list = new ArrayList();
<span style="white-space:pre">	</span>list.add("美国");
<span style="white-space:pre">	</span>list.add("中国");
<span style="white-space:pre">	</span>list.add("俄罗斯");
<span style="white-space:pre">	</span>list.add("印度");
<span style="white-space:pre">	</span>list.add("巴西");
<span style="white-space:pre">	</span>pageContext.setAttribute("list",list);
%>
<c:forEach items="${list}" var="c">
${c }<br>
</c:forEach>
 
<h1>遍历数组中的数据</h1><hr>
<%
<span style="white-space:pre">	</span>String []  city = {"北京","上海","广州","铁岭","葫芦岛"};
<span style="white-space:pre">	</span>pageContext.setAttribute("city",city);
%>
<c:forEach items="${city}" var="c">
${c }<br>
</c:forEach>


<c:forTokens>用来浏览一字符串中所有的成员,其成员是由定义符号所分隔的
<c:forTokens items="www.itheima.com" delims="." var="str">
${str }<br>
</c:forTokens>


<c:param>标签 <c:param>标签可以嵌套在<c:import>、<c:url>或<c:redirect>标签内,为这些标签所使用的URL地址附加参数。
<c:redirect url="/index.jsp" context="${pageContext.request.contextPath}">
<c:param name="name" value="zhang"></c:param>
</c:redirect>


<c:import> 标签,实现include操作
from xxxxx....
<c:import url="/index.jsp" var="p" scope="page"></c:import>
xxxx
${p }
zzzz


<c:url>标签用于在JSP页面中构造一个URL地址,其主要目的是实现URL重写。URL重写就是将会话标识号以参数形式附加在URL地址后面 

<c:url value="/index.jsp" context="${pageContext.request.contextPath}" var="url" scope="page"></c:url>
<a href="${url }">xxx</a>


<c:redirect>标签用于实现请求重定向
<c:redirect url="/index.jsp" context="${pageContext.request.contextPath}">
<c:param name="name" value="zhang"></c:param>
</c:redirect>



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值