`
rcyl2003
  • 浏览: 240180 次
  • 性别: Icon_minigender_1
  • 来自: 北京
文章分类
社区版块
存档分类
最新评论

一个JSP开发中有用又好用的类

阅读更多

package util;

import java.io.*;
import java.util.*;
import javax.servlet.*;

/** *//** 
 * A class to simplify parameter handling.  It can return parameters of
 * any primitive type (no casting or parsing required), can throw an 
 * exception when a parameter is not found (simplifying error handling),
 * and can accept default values (eliminating error handling).
 * 


 * It is used like this:
 * 

 * ParameterParser parser = new ParameterParser(req); *   * float ratio = parser.getFloatParameter("ratio", 1.0); *   * int count = 0; * try { *   count = parser.getIntParameter("count"); * } * catch (NumberFormatException e) { *   handleMalformedCount(); * } * catch (ParameterNotFoundException e) { *   handleNoCount(); * } * 

 *
 * There's also a capability to find out if any required parameters are
 * missing from a request:
 * 
 * ParameterParser parser = new ParameterParser(req); * String[] required = { "fname", "lname", "account" }; * String[] missing = parser.getMissingParameters(required); * 

 *
 * The default charset for input parameters is ISO-8859-1 (Latin-1).  
 * If the parameter values are encoded in another format, specify that using
 * setCharacterEncoding() before parsing.  The parameter names currently
 * have to be in the Latin-1 character set:
 * 
 * ParameterParser parser = new ParameterParser(req); * parser.setCharacterEncoding("Shift_JIS"); * String japaneseValue = parser.getStringParameter("latinName"); * 

 *
 * 
@see com.oreilly.servlet.ParameterNotFoundException
 *
 * 
@author Jason Hunter, Copyright © 1998, 1999
 * 
@version 1.4, 2000/12/14, better checking the selected encoding is valid in 
 *                           setCharacterEncoding() thanks to Dewayne McNair
 * 
@version 1.3, 2000/05/17, added setCharacterEncoding()
 * 
@version 1.2, 2000/05/17, getBooleanParameter() now recognizes "on" and "yes"
 * 
@version 1.1, 1999/12/20, added getMissingParameters() method
 * 
@version 1.0, 1998/09/18
 
*/

public class ParameterParser ...{

  
private ServletRequest req;
  
private String encoding;

  
/** *//**
   * Constructs a new ParameterParser to handle the parameters of the
   * given request.
   *
   * 
@param req the servlet request
   
*/

  
public ParameterParser(ServletRequest req) ...{
    
this.req = req;
  }


  
/** *//**
   * Sets the character encoding (charset) of the request to help the parser 
   * properly decode parameter values.  The default is to return undecoded values,
   * the same as would be returned by getParameter().
   *
   * 
@param encoding the charset of the request
   * 
@exception UnsupportedEncodingException if the charset is not supported 
   * on this sytem
   
*/

  
public void setCharacterEncoding(String encoding) 
                 
throws UnsupportedEncodingException ...{
    
// Test the encoding is valid
    new String("".getBytes("8859_1"), encoding);
    
// Getting here means we're valid, so set the encoding
    this.encoding = encoding;
  }


  
/** *//**
   * Gets the named parameter value as a String
   *
   * 
@param name the parameter name
   * 
@return the parameter value as a String
   * 
@exception ParameterNotFoundException if the parameter was not found
   * or was the empty string
   
*/

  
public String getStringParameter(String name)
      
throws ParameterNotFoundException ...{
    String[] values 
= req.getParameterValues(name);
    
if (values == null...{
      
throw new ParameterNotFoundException(name + " not found");
    }

    
else if (values[0].length() == 0...{
      
throw new ParameterNotFoundException(name + " was empty");
    }

    
else ...{
      
if (encoding == null...{
        
return values[0];
      }

      
else ...{
        
try ...{
          
return new String(values[0].getBytes("8859_1"), encoding);
        }

        
catch (UnsupportedEncodingException e) ...{
          
return values[0];  // should never happen
        }

      }

    }

  }


  
/** *//**
   * Gets the named parameter value as a String, with a default.
   * Returns the default value if the parameter is not found or 
   * is the empty string.
   * 
   * 
@param name the parameter name
   * 
@param def the default parameter value
   * 
@return the parameter value as a String, or the default
   
*/

  
public String getStringParameter(String name, String def) ...{
    
try ...return getStringParameter(name); }
    
catch (Exception e) ...return def; }
  }


  
/** *//**
   * Gets the named parameter value as a boolean, with true indicated by
   * "true", "on", or "yes" in any letter case, false indicated by "false", 
   * "off", or "no" in any letter case.
   *
   * 
@param name the parameter name
   * 
@return the parameter value as a boolean
   * 
@exception ParameterNotFoundException if the parameter was not found
   * 
@exception NumberFormatException if the parameter could not be converted 
   * to a boolean
   
*/

  
public boolean getBooleanParameter(String name)
      
throws ParameterNotFoundException, NumberFormatException ...{
    String value 
= getStringParameter(name).toLowerCase();
    
if ((value.equalsIgnoreCase("true")) ||
        (value.equalsIgnoreCase(
"on")) ||
        (value.equalsIgnoreCase(
"yes"))) ...{
        
return true;
    }

    
else if ((value.equalsIgnoreCase("false")) ||
             (value.equalsIgnoreCase(
"off")) ||
             (value.equalsIgnoreCase(
"no"))) ...{
        
return false;
    }

    
else ...{
      
throw new NumberFormatException("Parameter " + name + " value " + value + 
                                      
" is not a boolean");
    }

  }


  
/** *//**
   * Gets the named parameter value as a boolean, with a default.
 &
分享到:
评论

相关推荐

    JSP开发中常用的包

    6. **standard.jar**:这是Tomcat中的另一个重要组件,包含了JSP标准工具包(JSP Standard Tag Library, JSP.1.2),提供了一些基本的标签库,如HTML标签、URL重写、脚本控制等,是JSP开发的基础。 7. **xalan.jar*...

    jsp开发插件2

    DisplayTag是一个开源的Java服务器端表格标签库,它为JSP开发者提供了一种优雅的方式来展示数据表,大大简化了在Web应用中创建复杂表格的工作。这个"jsp开发插件2",特别是关于"displaytag插件"的部分,是针对JSP...

    jsp开发销售管理系统

    【标题】"jsp开发销售管理系统"揭示了这个项目的核心是使用Java Server Pages(JSP)技术来构建一个销售管理的Web应用。JSP是一种在服务器端运行的动态网页技术,它允许开发者将HTML代码与Java代码相结合,以生成...

    java jsp 系统开发

    - **RDF (Resource Description Framework)**:这是一个标准模型,用于在Web上表示信息。了解RDF可以帮助开发者更好地理解如何在Web应用中组织和处理数据。 - **规则引擎与递归查询**:书中提到了RPL能够实现有限...

    JSP案例开发集锦

    错误处理也是JSP开发中的一个重要方面,它通过错误页面和异常处理来实现。开发者可以在JSP页面中使用try-catch语句块来捕获和处理异常。同时,通过配置web.xml文件或使用page指令,可以指定错误页面来优雅地处理运行...

    ACCP5.0 使用JSP开发Web应用系统(JSP)

    ACCP5.0 是一个专门针对初学者和专业人士的IT培训课程,旨在教授如何使用JavaServer Pages(JSP)技术来构建Web应用程序。本课程涵盖了从基础到进阶的JSP知识,通过一系列的课件、DLC(Distance Learning Course)和...

    Openfire插件开发 访问jsp servlet

    在这个“Openfire插件开发 访问jsp servlet”项目中,我们将探讨如何在Openfire环境中开发一个插件,同时利用JavaServer Pages(JSP)和Servlet技术来处理HTTP请求。 首先,让我们理解Openfire插件开发的基本流程:...

    完全免费的Java/jsp开发编辑工具FirstJava2

    经过一个月的努力终于开发出来,大家来祝贺喔! 是我们java初学者一大帮助,可以告别可爱的记事本了! 这个工具是一个Java开发编辑工具,当然比记事本要好,主要给java初学者和一般的个人开发者使用,使他们更快学好JAVA...

    jsp的四个作用域

    jsp作为一个动态网页技术,在web开发中起着重要的作用。了解jsp的四个作用域是mastering jsp技术的基础。本文将对jsp的四个作用域:pageScope、requestScope、sessionScope和applicationScope进行详细的解释和比较。...

    JSP WEB 开发实践

    JSP本质上是Servlet的一种简化表示形式,当JSP页面被首次请求时,服务器会将其转换为一个Servlet类,并进行编译。因此,JSP可以利用所有的Servlet功能,如请求处理、响应发送等。 **5. JSP生命周期** JSP页面有自己...

    JSP几个应用实例

    7. **自定义标签库**:高级JSP开发中,常常使用自定义标签库(如JSTL)来增强可读性和复用性。开发者可以创建自己的标签库,封装复杂的业务逻辑,让JSP页面更加简洁。 以上就是JSP应用的一些基本实例,涵盖了用户...

    JSP连接Oracle公共类!未测试!感觉可以用!

    本文将深入探讨一个JSP项目中的Oracle数据库连接公共类,该类旨在提供一套标准化、可复用的数据库操作接口。尽管标题表明这个类未经实际测试,但从代码结构和逻辑来看,它覆盖了基本的数据库操作,如连接、查询、...

    一个简单的JSP图书管理系统

    在这个"一个简单的JSP图书管理系统"中,JSP被用来构建一个交互式的用户界面,允许用户进行图书的查询、添加、删除和修改等操作。 系统的基础架构通常包括以下几个关键部分: 1. **前端展示**:JSP页面负责用户界面...

    完全免费的Java/jsp编辑开发工具FirstJava2.2完整版

    2.Struts action向导:根据jsp文件中的表单内容自动生成的form类和action类的代码.并修改struts-config.xml配置文件. 3.直接创建action类,自动生成action类的代码.并修改struts-config.xml配置文件. 编辑辅助方式比...

    JSP之使用jsp:forward实现用户信息验证的页面跳转

    在Java服务器页面(JSP)开发中,页面跳转是一个常见的需求,特别是在处理用户交互和信息验证时。`jsp:forward`元素是JSP中的一种动作标签,用于将请求转发到另一个页面,而不是生成一个新的HTTP请求。这在处理用户...

    jsp动态生成图像

    这个例子中,JSP页面返回了一个PNG格式的图像,图像内容是一个蓝色的正方形。 6. **动态生成验证码** 一个常见的应用场景是动态生成验证码。开发者可以随机生成一些字符,然后用Java2D绘制到图像上,同时添加一些...

    JSP经典例子教程-100例

    在这个示例中,我们首先创建了一个`Date`对象来获取当前日期时间,然后使用`SimpleDateFormat`类格式化时间字符串,最后通过`<%= currentTime %>`输出到页面上。 #### 4. 页面包含 页面包含是指在一个JSP页面中...

    自定义JSP标签

    自定义JSP标签是JavaServer Pages (JSP) 技术的一个强大特性,它允许开发者扩展JSP语言,创建自己的可重用组件。这在构建大型Web应用程序时尤其有用,因为它提高了代码的可读性和可维护性。在JSP中,自定义标签的...

    一个有用的jsp+servlet(mvc)框架

    【标题】:“一个有用的jsp+servlet(mvc)框架” 在Web开发领域,JSP(JavaServer Pages)和Servlet是两种非常关键的技术,它们通常结合使用,构建基于Java的动态Web应用。本框架集成了这两者,实现了MVC(Model-...

    纯jsp登录界面自动跳转

    在IT行业中,Web开发是至关重要的领域,而JavaServer Pages(JSP)是其中一种流行的服务器端脚本语言,用于创建动态网页。本教程将详细讲解如何利用JSP实现一个登录界面的自动跳转功能,并结合MySQL数据库进行用户...

Global site tag (gtag.js) - Google Analytics