Servlet3.0 自定义表达式方法

package com.lee.elfunction;

public class Functions {

	//字符串反转
	public static String reverse(String  text){
		return new StringBuffer(text).reverse().toString();
	}
	//统计字符串的个数
	public static int countChar(String text){
		return text.length();
	}
	
}

<?xml version="1.0" encoding="UTF-8"?>
<taglib 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/j2eeweb-jsptaglibrary_2_0.xsd"
      version="2.0">
	<tlib-version>1.0</tlib-version>
	<short-name>crazyit</short-name>
	<uri>http://www.crazyit.org/tags</uri>
	<function>
		<name>reverse</name>
		<function-class>com.lee.elfunction.Functions</function-class>
		<function-signature>
			java.lang.String reverse(java.lang.String)
		</function-signature>
	</function>
	
	<function>
		<name>countChar</name>
		<function-class>com.lee.elfunction.Functions</function-class>
		<function-signature>
			int countChar(java.lang.String)
		</function-signature>
	</function>
</taglib>


<%@page import="java.util.Date"%>
<%@page import="java.util.Enumeration"%>
<%@page import="java.net.URLDecoder"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ page isELIgnored="false"%>
<%@ taglib uri="http://java.sun.com/jstl/core_rt" prefix="c"%>
<%@ taglib uri="http://www.crazyit.org/tags" prefix="myfun"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<%
	out.println(request.getParameter("name"));
	
	String method=request.getMethod();
	if("get".equalsIgnoreCase(method)){
	String rawQueryStr=request.getQueryString();
		if(rawQueryStr!=null && rawQueryStr.length()>0){
			out.println("原始查询字符串为:"+rawQueryStr+"<br/>");
			String queryStr=URLDecoder.decode(rawQueryStr,"UTF-8");
			out.println("解码后的查询字符串:"+queryStr+"<br/>");
			
			//以& 分割查询字符串
			String[]paramPairs=queryStr.split("&");
			for(String param:paramPairs){
				out.println("每个请求的参数名,值对为:"+param+"<br/>");
				String[] nameValue=param.split("=");
				out.println(nameValue[0]+"参数值是:"+nameValue[1]+"<br/>");
			}
		}
	}else{
		Enumeration<String> headNames=request.getHeaderNames();
		while(headNames.hasMoreElements()){
			String headname=headNames.nextElement();
			out.println(headname+"--->"+request.getHeader(headname)+"<br/>");
		}
	}
%>
<body>
<form action="getPostParam.jsp" method="post">
	<input name="name"/>
	<input name="age"/>
	<input type="submit" value="提交"/>
</form>
<c:set var="a" value="1"/>
<c:forEach  begin="0" end="3">         
    <c:set var="a" value="${a+1}"/>
</c:forEach> 
${a}

<%
	out.println("现在的时间是:"+new Date());

%>
使用自定义表达式函数
${myfun:reverse(param["name"])}
</body>
</html>

你可能感兴趣的:(servlet3.0)