博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
对request进行装饰
阅读量:2396 次
发布时间:2019-05-10

本文共 1950 字,大约阅读时间需要 6 分钟。

实现html标签转义功能

package cn.itcast.web.decorator;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletRequestWrapper;//对HttpServletRequest对象包装/装饰public class MyRequest extends HttpServletRequestWrapper{ 	//HttpServletRequestWrapper是适配器	//HttpServletRequestWrapper已经重写了request的所有方法,只不过是空方法,继承这个类既不用自己重写很多方法又能让自定义的类继承于request	private HttpServletRequest request;	public MyRequest(HttpServletRequest request) {		super(request);   //继承后必须的操作        可以不管		this.request = request;	}	//重写父类的方法	public String getParameter(String name) {//表单项的名字		String value = null;		//取得客户端的请求方式[GET/POST]		String method = request.getMethod();   //返回的必须是大写的		if("GET".equals(method)){   			try {				value = request.getParameter(name);//乱码				byte[] buf = value.getBytes("ISO8859-1");				value = new String(buf,"UTF-8");//正码			} catch (Exception e) {				e.printStackTrace();			}		}else if("POST".equals(method)){			try {				request.setCharacterEncoding("UTF-8");				value = request.getParameter(name);//正码			} catch (Exception e) {				e.printStackTrace();			}		}		value = filter(value);		return value;	}	//转义方法	public String filter(String message) {        if (message == null)            return (null);        char content[] = new char[message.length()];        message.getChars(0, message.length(), content, 0);        StringBuffer result = new StringBuffer(content.length + 50);        for (int i = 0; i < content.length; i++) {            switch (content[i]) {            case '<':                result.append("<");                break;            case '>':                result.append(">");                break;            case '&':                result.append("&");                break;            case '"':                result.append(""");                break;            default:                result.append(content[i]);            }        }        return (result.toString());    }}

转载于:https://my.oschina.net/u/2356176/blog/468016

你可能感兴趣的文章
SSHException: Incompatible ssh peer (no acceptable kex algorithm)
查看>>
shell切换用户
查看>>
session机制详解
查看>>
《算法导论》学习总结——第二部分1堆排序
查看>>
linux下进程的一些总结
查看>>
强大的g++呢还是强大的C++?太假了吧
查看>>
C++中的内联函数inline总结
查看>>
C++中的函数指针的一些总结
查看>>
ubuntu下为postgresql添加ODBC驱动过程
查看>>
linux下的su,su -,以及cd,cd - ,cd ~总结
查看>>
CAS锁为什么是乐观锁呢
查看>>
Argument of type '(Foo::)(int,int)' does not match 'void (*)(int,int)'以及静态函数问题
查看>>
今天遇到的postgresql中的备份和恢复
查看>>
正好碰到了C++的函数对象,查各路资料,总结写下来吧
查看>>
今天试vi遇到的“Sorry,the command is not available in this version : syntax on”
查看>>
今天又搞到个libDTL.so is not an ELF file - it has the wrong magic bytes at the start.
查看>>
MinGW和vc6中编译DTL的过程
查看>>
Fedora13下为postgresql添加ODBC驱动过程
查看>>
Bridge模式学习
查看>>
Virtual的一些总结
查看>>