Java Tutorial/JSP/PageContext

Материал из Java эксперт
Перейти к: навигация, поиск

Check Page Exception

index.xml



   <source lang="java">

<%@ 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.

<FORM METHOD="POST" ACTION="index.jsp"> <INPUT TYPE="HIDDEN" NAME="hiddenValue" VALUE="Bang!"> <INPUT TYPE="SUBMIT" VALUE="Generate exception!"> </FORM> </BODY> </HTML></source>

Compare Attribute in PageContext

   <source lang="java">

<%--

 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)

}</source>





Dispatch Page Based on Form Input

Do Calculation on Value Saved in PageContext

   <source lang="java">

<%--

 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}:

a + b = ${a + b}
a - b = ${a - b}
a * b = ${a * b}
a / b = ${a / b}
a % b = ${a % b}
a mod b = ${a mod b}
</source>





Save Value to PageContext

   <source lang="java">

<%--

 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)

}</source>





Set PageContext Attribute in JSP Page

<p>MyTag.java



   <source lang="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] + "
"); } catch(Exception e){ throw new JspException(e.toString()); } counter++; if(counter >= names.length) { return SKIP_BODY; } return EVAL_BODY_AGAIN; }

}</source>