论坛首页 入门技术论坛

JSP资料辅导(5--2)

浏览 1062 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
作者 正文
   发表时间:2008-12-04  

cookie对象

所谓的cookie是一个小小的文本文件,它是以key、value的方式将Session Tracking的内容记录在这个文本文件内,这个文本文件通常存在于浏览器的暂存区内。JSTL并没有提供设定cookie的动作,因为这个动作通常都是后端开发者必须去做的事情,而不是交给前端的开发者。如果我们在cookie中设定一个名称为userCountry的值,那么可以使用${cookie.userCountry}来取得它。

header和headerValues(请求报头对象)

header储存用户浏览器和服务端用来沟通的数据,当用户要求服务端的网页时,会送出一个记载要求信息的标头文件,例如:用户浏览器的版本、用户计算机所设定的区域等其他相关数据。如果要取得用户浏览器的版本,即${header["User-Agent"]}。另外在很少机会下,有可能同一标头名称拥有不同的值,此时必须改为使用headerValues来取得这些值。
注意:因为User-Agent中包含“-”这个特殊字符,所以必须使用“[]”,而不能写成${header.User-Agent}。

initParam

就像其他属性一样,我们可以自行设定web应用的环境参数(Context),当我们想取得这些参数时,可以使用initParam隐含对象去取得它,例如:当我们在web.xml中设定如下:

<?xml version="1.0" encoding="ISO-8859-1"?>
<web-app xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"
version="2.4">
  <context-param>
     <param-name>userid</param-name>
     <param-value>mike</param-value>
  </context-param>
</web-app>
那么我们就可以直接使用 ${initParam.userid}来取得名称为userid,其值为mike的参数。下面是之前的做法:String userid = (String)application.getInitParameter("userid");

pageContext对象

我们可以使用 ${pageContext}来取得其他有关用户要求或页面的详细信息。下面列出了几个比较常用的部分。

Expression                                说 明
${pageContext.request}                  |取得请求对象
${pageContext.session}                  |取得session对象  
${pageContext.request.queryString}      |取得请求的参数字符串
${pageContext.request.requestURL}       |取得请求的URL,但不包括请求之参数字符串
${pageContext.request.contextPath}      |服务的web application的名称
${pageContext.request.method}           |取得HTTP的方法(GET、POST)
${pageContext.request.protocol}         |取得使用的协议(HTTP/1.1、HTTP/1.0)
${pageContext.request.remoteUser}       |取得用户名称
${pageContext.request.remoteAddr }      |取得用户的IP地址
${pageContext.session.new}              |判断session是否为新的,所谓新的session,表示刚由server产生而client尚未使用
${pageContext.session.id}               |取得session的ID
${pageContext.servletContext.serverInfo}|取得主机端的服务信息


JSTL(JSP标准标签库)

JSTL由核心标签,<c:... > ,xml解析标签 <x:...>,国际化标签 <fmt:....>,数据库访问标签<sql:...>,函数标签<fn:...>

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

属性设置
<c:set> 设置属性
<c:remove> 移除设置的属性

过程控制

<c:if test="..."> 条件标签 只有在test属性的值为true是才会执行标签体 
例:
<c:if test="${!(empty user.age)}">
      <h1>hello</h1>
</c:if>
  
<c:choose>choose和when是组合在一起使用的,有点类似于swith case的语法  。
<c:when test="...">when也是条件判断标签,test属性的值为true是才会执行标签体。
例:
<c:choose>
 <c:when test="${param.age<18}">
   <h1>you is a child<h1>
 </c:when>
 <c:when test="${param.age>18 and param.age<50 }">
    <h1>you is a young person</h1>
 </c:when>
 <c:when test="${param.age>50}">
     <h1>you is a old person</h1>
 </c:when>
</c:choose> 

<c:forEach>迭代标签
例:
<c:forEach var="book" item="${store.books}" varStatus="status">
    <h1>${book.parice}</h1>
</c:forEach>
<c:forEach begin="1" end="5" step="1">
     <h1>hello</h1>
</c:forEach>

<c:forTokens>字符串迭代标签

<c:import>引入标签
<c:import url="引入内容的url" var="别名">
${别名}

<c:url>url标签
<c:url value="...">
 <c:param name="..." value="..."/>
</c:url>
<c:url value="...">
使用url标签可以实现URL回写

<c:redirect uri="xxx/xxx/xxx.xx"/>

国际化标签
<fmt:lauguage>
<fmt:bundel>资源指定标签
<fmt:message>消息标签
例:
<fmt:setLocale value="zh"/>
<fmt:bundel basename="message.MessageResources">
  <fmt:message>name</fmt:message>
</fmt:bundel>

xxxx.properties
name=\0060\0700\

论坛首页 入门技术版

跳转论坛:
Global site tag (gtag.js) - Google Analytics