Java Tutorial/JSP/PageContext
Содержание
Check Page Exception
index.xml
<%@ page errorPage="processError.jsp" %>
<%-- Declare the page to send errors to --%>
<%-- This scriptlet checks a hidden field to see whether or not to throw an exception --%>
<%
String hiddenField = request.getParameter("hiddenValue");
if ( hiddenField !=null && !hiddenField.equals(""))
throw new java.lang.NullPointerException();
%>
<HTML>
<HEAD><TITLE>Generate Error</TITLE></HEAD>
<BODY>
This page generates an error when you click the button.<P>
<FORM METHOD="POST" ACTION="index.jsp">
<INPUT TYPE="HIDDEN" NAME="hiddenValue" VALUE="Bang!">
<INPUT TYPE="SUBMIT" VALUE="Generate exception!">
</FORM>
</BODY>
</HTML>
Compare Attribute in PageContext
<%--
Copyright (c) 2002 by Phil Hanna
All rights reserved.
You may study, use, modify, and distribute this
software for any purpose provided that this
copyright notice appears in all copies.
This software is provided without warranty
either expressed or implied.
--%>
<%@ page session="false" %>
<%@ page import="java.util.*" %>
<%
Map map = new HashMap();
map.put("speed", new Float(500));
map.put("power", new Float(1000));
pageContext.setAttribute("flyingObject", map);
pageContext.setAttribute("speedingBullet", new Float(400));
pageContext.setAttribute("locomotive", new Float(800));
%>
${
(flyingObject.speed gt speedingBullet)
and
(flyingObject.power gt locomotive)
}
Dispatch Page Based on Form Input
Do Calculation on Value Saved in PageContext
<%--
Copyright (c) 2002 by Phil Hanna
All rights reserved.
You may study, use, modify, and distribute this
software for any purpose provided that this
copyright notice appears in all copies.
This software is provided without warranty
either expressed or implied.
--%>
<%
pageContext.setAttribute("a", new Integer(10));
pageContext.setAttribute("b", new Integer(3));
%>
a = ${a}, b = ${b}:
<table>
<tr> <td>a + b = </td> <td>${a + b}</td> </tr>
<tr> <td>a - b = </td> <td>${a - b}</td> </tr>
<tr> <td>a * b = </td> <td>${a * b}</td> </tr>
<tr> <td>a / b = </td> <td>${a / b}</td> </tr>
<tr> <td>a % b = </td> <td>${a % b}</td> </tr>
<tr> <td>a mod b = </td> <td>${a mod b}</td> </tr>
</table>
Save Value to PageContext
<%--
Copyright (c) 2002 by Phil Hanna
All rights reserved.
You may study, use, modify, and distribute this
software for any purpose provided that this
copyright notice appears in all copies.
This software is provided without warranty
either expressed or implied.
--%>
<%@ page session="false" %>
<%@ page import="java.util.*" %>
<%
Map map = new HashMap();
map.put("speed", new Float(500));
map.put("power", new Float(1000));
pageContext.setAttribute("flyingObject", map);
pageContext.setAttribute("speedingBullet", new Float(400));
pageContext.setAttribute("locomotive", new Float(800));
%>
${
(flyingObject.speed gt speedingBullet)
and
(flyingObject.power gt locomotive)
}
Set PageContext Attribute in JSP Page
MyTag.java
package taglib;
import javax.servlet.jsp.*;
import javax.servlet.jsp.tagext.*;
import java.io.IOException;
public class MyTag extends TagSupport
{
private int counter = 0;
private String[] names = null;
public int doStartTag()
{
names = (String[]) pageContext.getAttribute("names");
return EVAL_BODY_INCLUDE;
}
public int doAfterBody() throws JspException
{
try{
pageContext.getOut().print(" " + names[counter] + "<BR>");
} catch(Exception e){
throw new JspException(e.toString());
}
counter++;
if(counter >= names.length) {
return SKIP_BODY;
}
return EVAL_BODY_AGAIN;
}
}