Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1797859
  • 博文数量: 438
  • 博客积分: 9799
  • 博客等级: 中将
  • 技术积分: 6092
  • 用 户 组: 普通用户
  • 注册时间: 2012-03-25 17:25
文章分类

全部博文(438)

文章存档

2019年(1)

2013年(8)

2012年(429)

分类: Java

2013-07-12 13:46:40

JSP技术在1999年由Sun引入,作为对ASP和PHP的回应。Java Server Page在被请求时,会被自动翻译为一个servlet的java文件并编译。当JSP比产生的servlet新时,则会重新翻译生成新的servlet,否则继续重用已有的servlet。载入并初始化servlet实例后,容器会调用jspInit方法。


内嵌java代码

<%@ page import= "java.util.Calendar" %>
<%@ page import= "java.util.Date" %>
<%@ page language= "java" contentType ="text/html; charset=ISO-8859-1"
    pageEncoding= "ISO-8859-1" %>
 DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "" >
<%! Date now = Calendar.getInstance().getTime(); %>
< html>
< head>
< meta http-equiv ="Content-Type" content= "text/html; charset=ISO-8859-1" >
< title> Embeded Java  title>
 head>
< body>
<%= now %>
 body>
 html>

注意:<%! ... %>里的声明代码,在JSP页面没有改动的情况下,只会被执行一次。



使用bean

usebean.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding= "ISO-8859-1"%>
DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "" >
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title> Use Bean title>
head>
<body>
<jsp:useBean id= "simpleBean" scope ="application" class= "zp.javaee.SimpleBean"/>
<p> ${simpleBean.currentDate}p >
body>
html>

SimpleBean.java
package zp.javaee;

import java.util.Calendar;
import java.util.Date;


public class SimpleBean {
        public Date getCurrentDate() {
               return Calendar.getInstance().getTime();
       }
}

设置bean的值

SimpleSetValue.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding= "ISO-8859-1"%>
<html>
    <head >
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title> Yielding and Setting Valuestitle >
    head >
    <body >
        <jsp:useBean id= "simpleBean" scope ="page" class="zp.javaee.SimpleSetValue" />
        <jsp:setProperty name= "simpleBean" property ="*"/>
        <form method= "post">
        Use the input text box below to set the value, and then hit submit.
        <br/>< br/>
        Set the field value:
        <input id= "simpleValue" name ="simpleValue" type= "text" size ="30"/>
        <br/>
        The value contained within the field is currently:
        <jsp:getProperty name= "simpleBean" property ="simpleValue"/>
        
        <input type= "submit">
        form>
    body >
html>

SimpleSetValue.java
package zp.javaee;

import java.io.Serializable;

public class SimpleSetValue implements Serializable {

        /**
        *
        */
        private static final long serialVersionUID = 4901398509868273864L;

        public String getSimpleValue() {
               return SimpleValue ;
       }

        public void setSimpleValue(String simpleValue) {
               SimpleValue = simpleValue;
       }
       
        private String SimpleValue ;
}

提示:jsp:setProperty里的通配符*使得表单所有的input都会提交给bean。jsp:getProperty等价于EL(Expression Language)表达式${simpleBean.simpleValue}


条件判断

SimpleFunction.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding= "ISO-8859-1"%>
DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "" >
<%@ taglib uri="" prefix ="c" %>
<%@ taglib uri="SimpleFunction.tld" prefix= "simple" %>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title> Simple Function title >
head>
<body>
<c:if test= "${simple:simpleFunction(\"\") }">
is empty
c:if>
body>
html>

SimpleFunction.tld
xml version="1.0" encoding="UTF-8"?>
 xmlns="">
  1.0
  1.0
  simple-methods
 
       simpleFunction
       zp.javaee.SimpleFunctionClass
       boolean simpleFunction(java.lang.String)
 

SimpleFunctionClass.java
package zp.javaee;

public class SimpleFunctionClass {
        public static boolean simpleFunction(String s) {
               return s.isEmpty();
       }
}

注意:方法必须是static的,返回boolean。需要使用Java Standard Tag Library (JSTL),它的"test"属性用于判断条件。

JSPX

JSP的xml版本。

Tag Description
Defines attributes for a JSP page.
Defines an element body.
Defines page declarations.
Defines page includes and page directives.
Executes the body of the JSP tag that is used by the calling JSP page to invoke the tag.
Generates an XML element dynamically.
Inserts the value of a scripting language expression, converted into a string.
Forwards a request to another page.  The new page can be HTML,  JSP, or servlet.
Obtains the value of a bean property and places it in the page.
Includes another JSP or web resource in the page.
Invokes a specified JSP fragment.
Specifies the document type declaration.
Executes an applet or bean with the specified plug-in.
Defines standard elements and tag library namespaces.
Embeds code fragment into a page if necessary.
Sets specified value(s) into a bean property.
Encloses template data.
References and instantiates (if needed) a JavaBean class using a name and providing a scope.


获得请求参数

< jsp:expression>request.getParameter( "a") jsp:expression>
等价于
${param.a }


自定义tag

SimpleTag.java
xml version= "1.0" encoding ="ISO-8859-1" ?>
<jsp:root xmlns:jsp="" version="2.0">
        <jsp:directive.page contentType="text/html; charset=ISO-8859-1"
               pageEncoding="ISO-8859-1" session="false"/>
        <jsp:output doctype-root-element="html"
               doctype-public="-//W3C//DTD XHTML 1.0 Transitional//EN"
               doctype-system=""
               omit-xml-declaration="true" />
<html xmlns= ""
               xmlns:simple="simple" >
<head>
<title> Simple Tag title >
head>
<body>
<simple:simple-tag owner= "zp"/>
body>
html>
jsp:root>

SimpleTag.tld
xml version="1.0" encoding="UTF-8"?>
 taglib PUBLIC "-//Sun Microsystems, Inc.//DTD JSP Tag Library 1.2//EN" "" >
  1.0
  2.0
  simple-tag
  simple 
 
    simple-tag
    zp.javaee.SimpleTag
    empty
   
       owner
       false
        true
   
 

SimpleTag.jspx
xml version= "1.0" encoding ="ISO-8859-1" ?>
<jsp:root xmlns:jsp="" version="2.0">
        <jsp:directive.page contentType="text/html; charset=ISO-8859-1"
               pageEncoding="ISO-8859-1" session="false"/>
        <jsp:output doctype-root-element="html"
               doctype-public="-//W3C//DTD XHTML 1.0 Transitional//EN"
               doctype-system=""
               omit-xml-declaration="true" />
<html xmlns= ""
               xmlns:simple="simple" >
<head>
<title> Insert title heretitle >
head>
<body>
<simple:simple-tag owner= "zp"/>
body>
html>
jsp:root>

提示:tld文件可以放在任何地方,比如WEB-INF/tlds里。


引用别的JSP

xml version= "1.0" encoding ="ISO-8859-1" ?>
<jsp:root xmlns:jsp="" version="2.0">
        <jsp:directive.page contentType="text/html; charset=ISO-8859-1"
               pageEncoding="ISO-8859-1" session="false"/>
        <jsp:output doctype-root-element="html"
               doctype-public="-//W3C//DTD XHTML 1.0 Transitional//EN"
               doctype-system=""
               omit-xml-declaration="true" />
<html xmlns= "" >
<head>
<title> Include JSPtitle >
head>
<body>
<h2> include: h2>
<jsp:include page= "SimpleTag.jspx">
<jsp:param value= "zp" name ="owner"/>
jsp:include>
body>
html>
jsp:root>


错误页面

当一个jsp的脚本运行抛出异常时,可以用来转到专门显示错误的页面。
在xxx.jsp里,可以用pageContext.errorData来得到错误信息。比如${pageContext.errorData.statusCode}。
Expression Value
pageContext.errorData Provides access to the error information
pageContext.exception Returns the current value of the exception object
pageContext.errorData.requestURI Returns the request URI
pageContext.errorData.servletName Returns the name of the servlet invoked
pageContext.errorData.statusCode Returns the error status code
pageContext.errorData.throwable Returns the throwable that caused the error

禁用内嵌java

在web.xml里,配置:

   
        true
   



忽略EL

方法1
\${elBean.myProperty}
方法2
在web.xml里

     true
方法3

<%@ page isELIgnored="true" %>
阅读(3539) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~