过滤输入字符串中的危险字符
生活随笔
收集整理的這篇文章主要介紹了
过滤输入字符串中的危险字符
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
在文本框中輸入&;<>/%=#等字符時,在處理頁中會把這些字符過濾掉然后顯示出過濾后的字符串
應用String類提供大的replaceAll()方法,過濾字符串中指定的子字符串
創建StringUtil的JavaBean類,實現過濾危險字符串的方法
創建index.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"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Insert title here</title> </head> <body> <form action="filterstr.jsp" method="post"><table><tr><td align="right">請輸入字符串:</td><td><input type="text" name="sourceStr" size="40"/></td></tr><tr><td colspan="2" align="center"><input type="submit" value="過濾"/></td></tr></table> </form> </body> </html>創建filterstr.jsp頁。獲取表單信息,過濾
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <%request.setCharacterEncoding("UTF-8");%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html><head> <title>處理過濾</title><meta http-equiv="pragma" content="no-cache"><meta http-equiv="cache-control" content="no-cache"><meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"><meta http-equiv="description" content="This is my page"><!--<link rel="stylesheet" type="text/css" href="styles.css">--><style type="text/css">table{border: 1px solid;border-color: green;color: green;font-size: 13px;font-family: 華文細黑;}</style></head><body><%String sourceStr = request.getParameter("sourceStr");%><!-- 使用useBean動作標簽導入JavaBean對象 --><jsp:useBean id="strBean" class="com.cn.zj.bean.StringUtil10"></jsp:useBean><!-- 對StringUtil類的longValue屬性賦值 --><jsp:setProperty property="sourceStr" name="strBean" value="<%=sourceStr %>"/><table><tr><td>過濾之前的字符串:</td><td align="left"> <jsp:getProperty property="sourceStr" name="strBean"/></td></tr><tr ><td>過濾之后的字符串:</td><td align="left"> <jsp:getProperty property="targetStr" name="strBean"/></td> </tr></table> </body> </html>總結
以上是生活随笔為你收集整理的过滤输入字符串中的危险字符的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 将长整形的数字分位显示
- 下一篇: 判断字符串是否以指定字符开头