您的位置:首页 > Web前端 > JavaScript

JSP中特殊字符处理

2015-09-04 16:34 465 查看
JSP中用form表单(Post请求)可以传送特殊字符到服务器,但当使用get请求或者通过链接地址(也是get请求)时,如遇到+、-等特殊字符就会截断其后的值。本文没有解决get请求中含这些字符的问题,但给出了一种转换方法。

如上所说,post请求可以传送特殊字符。所以,可以将get请求转换成post请求。又js中可以获取form元素并提交请求,所以可以将get请求需要传送的数据放在type类型为hidden的input中,然后通过js代码和form表单实现传送特殊字符。示例代码如下:

one.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<%
request.setCharacterEncoding("utf-8");
%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>one</title>
</head>
<body>
<form action="two.jsp" method="post" id="1">
<input type="hidden" name="value" value="one+-*/">
</form>
<form action="two.jsp" method="post" id="2">
<input type="hidden" name="value" value="two+-*/">
</form>
<a href="javascript:send('1')">one</a>
<a href="javascript:send('2')">tow</a>
</body>

<script type="text/javascript">
function send(formId){
document.getElementById(formId).submit();
}
</script>
</html>


two.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<%
request.setCharacterEncoding("utf-8");
String value = request.getParameter("value");
%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>two</title>
</head>
<body>
传过来的值为<%=value %>
</body>
</html>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: