关于js的encodeURI和java的URLDecoder

根据项目的需要写了一个加密的标签,代码如下:
encrypt.tld
<?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/j2ee web-jsptaglibrary_2_0.xsd"  
version="2.0">   
<tlib-version>1.0</tlib-version>   
<short-name>mytaglib</short-name>   
<uri>/encrypt-tags</uri>               

   <tag>
   <name>encrypt</name>
   <tag-class>com.ctitc.analysis.util.Encrypt</tag-class>
   <attribute>
   <name>encryptValue</name>
   <required>true</required>
   <rtexprvalue>true</rtexprvalue>
   </attribute>
   <body-content>empty</body-content>
   </tag>
                  
</taglib>



下面是java部分
Encrypt.java
package com.ctitc.analysis.util;

import java.io.IOException;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.SimpleTagSupport;

@SuppressWarnings("serial")
public class Encrypt extends SimpleTagSupport {
	private String encryptValue;
	
	public void doTag() throws JspException {
		// TODO Auto-generated method stub 
		try {
			encryptValue = java.net.URLEncoder.encode(encryptValue,"UTF-8");
			getJspContext().getOut().write(encryptValue);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}   
		
	}

	public String getEncryptValue() {
		return encryptValue;
	}

	public void setEncryptValue(String encryptValue) {
		this.encryptValue = encryptValue;
	}
	
	
}



下面是jsp部分
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
	String path = request.getContextPath();
	String basePath = request.getScheme() + "://"
			+ request.getServerName() + ":" + request.getServerPort()
			+ path + "/";
%>
<%@taglib prefix="s" uri="/struts-tags" %>
<%@taglib prefix="e" uri="/encrypt-tags" %>
<HTML>
	<HEAD>
		<title>main</title>
	    <base href="<%=basePath%>">
		<style type="text/css">
	   <!--
	   a:link { text-decoration: none;color: blue}
	   a:active { text-decoration:blink}
	   a:hover { text-decoration:underline;color: red} 
	   a:visited { text-decoration: none;color: green}
	
		body {
			margin-left: 0px;
			margin-top: 0px;
			margin-right: 0px;
			margin-bottom: 0px;
			background-color: #E6F4FF;
		}
	   --> 
 .STYLE1 {font-size: 10pt}
        </style> 
		<link href="styles/sty.css" rel="stylesheet" type="text/css">
		<script type="text/javascript">
        function DoCheckAll(){
			var chkItemArray = document.getElementsByName("chkItem");
			var chkallstatus = document.getElementById("chkall").checked;
			for(var i=0;i<chkItemArray.length;i++){
				chkItemArray[i].checked = chkallstatus;
			}
		}
		function deleteItem(cubeName){
		var chkItemArray = document.getElementsByName("chkItem");
		var result = "";
			for(var i=0;i<chkItemArray.length;i++){
				if(chkItemArray[i].checked){
					if(result == ""){
						result += chkItemArray[i].id;
					}else{
						result += ","+chkItemArray[i].id;
					}
				}
			}
			if(result == ""){
				alert("请选择您要删除的记录!");
			}else{
				result = encodeURI(result);
				document.location.href="measuresharedelete.action?fileName=${fileName }&measureName="+result+"&cubeName=<e:encrypt encryptValue='${cubeName }'></e:encrypt>";
			}		
		}
        </script>
	</HEAD>
	<body bgcolor="#eeeeee" topmargin="0" leftmargin="0" rightmargin="0">
	<table width="100%" border="0" cellspacing="0" cellpadding="0">
      <tr>
        <td height="25" align="center" bgcolor="#AFDFFE">
        <table width="97%" border="0" cellspacing="0" cellpadding="0">
          <tr>
            <td width="59%">用户 <strong>sys</strong> 您现在的位置是: <a href="#"><strong>规费主题分析</strong></a>&gt;&gt;<a href="#"><strong>共享度量</strong></a>&gt;&gt;度量列表</td>
            <td width="41%" align="right">搜索内容:
            <input name="textfield2"  type="text" style="height=18" size="15"> <a href="#"><img src="images/tb_soso.gif" width="15" height="15" border="0"></a></td>
          </tr>
        </table>
        </td>
      </tr>
	</table>
	<br/>
	
	<table width="97%"  border="0" align="center" cellpadding="0" cellspacing="1" bgcolor="#8C9FEB">
          <tr bgcolor="#D4ECF8">
            <td colspan="7"><div align="left"><strong>共享度量</strong></div>
            <div align="left">
            <a href="measureshare.action?fileName=${fileName }&cubeName=<e:encrypt encryptValue='${cubeName }'></e:encrypt> " ><img src="images/btn_zj.jpg" border="0"></a> 
            <a href="javascript:if(confirm('确认要删除选定的记录?'))deleteItem();"><img src="images/btn_sc.jpg" border="0"></a>
            </div></td>
          </tr>
          <tr align="center" bgcolor="#91C6FC">
          	<td>全选<input type="checkbox" name="chkall" value="checkbox" onclick="DoCheckAll()"></td>
            <td><div align="center"><strong>度量名称</strong></div></td>
            <td><div align="center"><strong>度量列</strong></div></td>
            <td><div align="center"><strong>度量描述</strong></div></td>
            <td><div align="center"><strong>聚合</strong></div></td>
            <td><strong>字符串格式</strong></td>
            <td><div align="center"><strong>操作</strong></div></td>
          </tr>
          <s:iterator value="shareMeasureList">
          <tr align="center" bgcolor="#FFFFFF">
          	<td bgcolor="#D4ECF8" >&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<input type="checkbox" id="${measureName }" name="chkItem" value="checkbox"></td>
            <td><div align="center">${measureName }</div></td>
            <td><div align="center">${column }</div></td>
            <td><div align="center">
              <div align="center">${caption }</div>
            </div></td>
            <td><div align="center">${aggregator }</div></td>
            <td><div align="center">${formatString }</div></td>
            <td><div align="center"><a href="measureshareinfo.action?measureName=<e:encrypt encryptValue='${measureName }'></e:encrypt>&fileName=${fileName }&cubeName=<e:encrypt encryptValue='${cubeName }'></e:encrypt>">修改</a></div></td>
          </tr>
          </s:iterator>
        </table>
		</body>
</HTML>



在js中标签无法将"<e:encrypt encryptValue='"+result +"'></e:encrypt>"
这种加密,因为里面有特殊字符。所有用result = encodeURI(result);来加密。
后台解析统一用java.net.URLDecoder.decode(result , "UTF-8");来解密。

这个可以解决传递中文参数乱码问题。

你可能感兴趣的:(java,.net,jsp,struts,servlet)