标签:
BufferedReader的增强
package cn.lcp.damo;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.Reader;
/*
1.实现与被增强对象相同的接口 BufferedReader
2、定义一个变量记住被增强对象
3、定义一个构造器,接收被增强对象
4、覆盖需要增强的方法
5、对于不想增强的方法,直接调用被增强对象(目标对象)的方法
*/
public class BufferedReaderWrapper extends BufferedReader {
private BufferedReader br;
private int linenum = 1;
public BufferedReaderWrapper(BufferedReader in) {
super(in);
this.br = in;
}
@Override
public String readLine() throws IOException {
String line = br.readLine();
if(line==null){
return line;
}
return linenum++ + ":" + line;
}
}
package cn.lcp.damo;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
public class TestBufferedReaderWrapper {
/**
* @param args
* @throws IOException
*/
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new FileReader("src/cn/lcp/demo/BufferedReaderWrapper.java"));
BufferedReaderWrapper wrapper = new BufferedReaderWrapper(br);
FileWriter fw = new FileWriter("c:\\1.java");
String line = null;
while((line=wrapper.readLine())!=null){
fw.write(line + "\r\n");
}
fw.close();
wrapper.close();
br.close();
}
}
为解决全站乱码
package cn.lcp.web.filter;
import java.io.IOException;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletRequestWrapper;
import javax.servlet.http.HttpServletResponse;
//为解决全站乱码
public class CharacterEncodingFilter implements Filter {
public void destroy() {
// TODO Auto-generated method stub
}
public void doFilter(ServletRequest req, ServletResponse resp,
FilterChain chain) throws IOException, ServletException {
HttpServletRequest request = (HttpServletRequest) req;
HttpServletResponse response = (HttpServletResponse) resp;
// 对get提交的数据还是没有办法对其改变编码,所以要对request的getParameter方法缺少功能了。
request.setCharacterEncoding("utf-8");
response.setCharacterEncoding("utf-8");
response.setContentType("text/html;charset=utf-8");
//放行的是自己的包装类
MyCharacterEncodingRequest requestWrapper = new MyCharacterEncodingRequest(
request);
chain.doFilter(requestWrapper, response);
}
public void init(FilterConfig filterConfig) throws ServletException {
// TODO Auto-generated method stub
}
}
/*
* 1.实现与被增强对象相同的接口 2、定义一个变量记住被增强对象 3、定义一个构造器,接收被增强对象 4、覆盖需要增强的方法
* 5、对于不想增强的方法,直接调用被增强对象(目标对象)的方法
*/
// 包装类。
class MyCharacterEncodingRequest extends HttpServletRequestWrapper {
private HttpServletRequest request;
// 接受被增强对象
public MyCharacterEncodingRequest(HttpServletRequest request) {
super(request);
this.request = request;
}
@Override
public String getParameter(String name) {
try {
// 客户机提交的值
String value = this.request.getParameter(name);
if (value == null) {
return null;
}
// 判断客户机的提交方式是不是get。这里忽略了get和GET方式
if (!this.request.getMethod().equalsIgnoreCase("get")) {
return value;
}
//客户机的原始码表
value = new String(value.getBytes("ISO8859-1"),
this.request.getCharacterEncoding()); //获取上面的request设定的编码
return value;
} catch (Exception e) {
throw new RuntimeException(e);
}
}
}
package cn.lcp.servlet;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class ServletDemo1 extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
System.out.println(request.getParameter("username"));
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
}
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
<display-name>day18_4</display-name>
<filter>
<filter-name>CharacterEncodingFilter</filter-name>
<filter-class>cn.lcp.web.filter.CharacterEncodingFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>CharacterEncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<servlet>
<description>This is the description of my J2EE component
</description>
<display-name>This is the display name of my J2EE component
</display-name>
<servlet-name>ServletDemo1</servlet-name>
<servlet-class>cn.lcp.servlet.ServletDemo1</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>ServletDemo1</servlet-name>
<url-pattern>/servlet/ServletDemo1</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>My JSP ‘test.jsp‘ starting page</title>
</head>
<body>
<c:url value="/servlet/ServletDemo1" scope="page" var="servletDemo1">
<c:param name="username" value="吊死扶伤"></c:param>
</c:url>
<a href="${servletDemo1 }">点点</a>
<form action="${pageContext.request.contextPath }/servlet/ServletDemo1" method="post">
用户名:<input type="text" name="username">
<input type="submit">
</form>
<%
String param = request.getParameter("param");
%>
<a href="URLCharset.jsp?param=‘中文‘">请点击这个链接</a><br>
你提交的参数为:<%=param%>
</body>
</html>
脏话过滤器
package cn.lcp.web.filter;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletRequestWrapper;
import javax.servlet.http.HttpServletResponse;
//脏话过滤器
public class DirtyFilter implements Filter {
public void doFilter(ServletRequest req, ServletResponse resp,
FilterChain chain) throws IOException, ServletException {
HttpServletRequest request = (HttpServletRequest) req;
HttpServletResponse response = (HttpServletResponse) resp;
DirtyRequest dirtyrequest = new DirtyRequest(request);
chain.doFilter(dirtyrequest, response);
}
public void init(FilterConfig filterConfig) throws ServletException {
}
public void destroy() {
}
}
//记住所有的脏话
class DirtyRequest extends HttpServletRequestWrapper{
private List<String> dirtyWords = Arrays.asList("傻B","操蛋","畜生");
private HttpServletRequest request;
public DirtyRequest(HttpServletRequest request) {
super(request);
this.request = request;
}
@Override
public String getParameter(String name) {
//获取数据
String value = this.request.getParameter(name);
if(value==null){
return null;
}
//检测是否有脏话
for(String dirtyWord : dirtyWords){
if(value.contains(dirtyWord)){
value = value.replace(dirtyWord, "****");
}
}
return value;
}
}
package cn.lcp.web.filter;
import java.io.IOException;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletRequestWrapper;
import javax.servlet.http.HttpServletResponse;
//为解决全站乱码
public class CharacterEncodingFilter implements Filter {
public void destroy() {
// TODO Auto-generated method stub
}
public void doFilter(ServletRequest req, ServletResponse resp,
FilterChain chain) throws IOException, ServletException {
HttpServletRequest request = (HttpServletRequest) req;
HttpServletResponse response = (HttpServletResponse) resp;
// 对get提交的数据还是没有办法对其改变编码,所以要对request的getParameter方法缺少功能了。
request.setCharacterEncoding("utf-8");
response.setCharacterEncoding("utf-8");
response.setContentType("text/html;charset=utf-8");
//放行的是自己的包装类
MyCharacterEncodingRequest requestWrapper = new MyCharacterEncodingRequest(
request);
chain.doFilter(requestWrapper, response);
}
public void init(FilterConfig filterConfig) throws ServletException {
// TODO Auto-generated method stub
}
}
/*
* 1.实现与被增强对象相同的接口 2、定义一个变量记住被增强对象 3、定义一个构造器,接收被增强对象 4、覆盖需要增强的方法
* 5、对于不想增强的方法,直接调用被增强对象(目标对象)的方法
*/
// 包装类。
class MyCharacterEncodingRequest extends HttpServletRequestWrapper {
private HttpServletRequest request;
// 接受被增强对象
public MyCharacterEncodingRequest(HttpServletRequest request) {
super(request);
this.request = request;
}
@Override
public String getParameter(String name) {
try {
// 客户机提交的值
String value = this.request.getParameter(name);
if (value == null) {
return null;
}
// 判断客户机的提交方式是不是get。这里忽略了get和GET方式
if (!this.request.getMethod().equalsIgnoreCase("get")) {
return value;
}
//客户机的原始码表
value = new String(value.getBytes("ISO8859-1"),
this.request.getCharacterEncoding()); //获取上面的request设定的编码
return value;
} catch (Exception e) {
throw new RuntimeException(e);
}
}
}
package cn.lcp.web.servlet;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class ServletDemo2 extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String message = request.getParameter("message"); //<script>
response.getWriter().write("您上次的留言是:<br/>" + message);
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
}
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
id="WebApp_ID" version="3.0">
<display-name>day18_5</display-name>
<filter>
<filter-name>DirtyFilter</filter-name>
<filter-class>cn.lcp.web.filter.DirtyFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>DirtyFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<servlet>
<description>This is the description of my J2EE component
</description>
<display-name>This is the display name of my J2EE component</display-name>
<servlet-name>ServletDemo2</servlet-name>
<servlet-class>cn.lcp.web.servlet.ServletDemo2</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>ServletDemo2</servlet-name>
<url-pattern>/servlet/ServletDemo2</url-pattern>
</servlet-mapping>
<filter>
<filter-name>CharacterEncodingFilter</filter-name>
<filter-class>cn.lcp.web.filter.CharacterEncodingFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>CharacterEncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>My JSP ‘test2.jsp‘ starting page</title>
</head>
<body>
<form action="${pageContext.request.contextPath }/servlet/ServletDemo2" method="post">
留言:
<textarea rows="8" cols="60" name="message"></textarea>
<input type="submit" value="发表">
</form>
</body>
</html>
html转义过滤器
public class HtmlFilter implements Filter {
public void doFilter(ServletRequest req, ServletResponse resp,
FilterChain chain) throws IOException, ServletException {
HttpServletRequest request = (HttpServletRequest) req;
HttpServletResponse response = (HttpServletResponse) resp;
MyHtmlRequest myrequest = new MyHtmlRequest(request);
chain.doFilter(myrequest, response);
}
public void destroy() {
// TODO Auto-generated method stub
}
public void init(FilterConfig filterConfig) throws ServletException {
// TODO Auto-generated method stub
}
}
class MyHtmlRequest extends HttpServletRequestWrapper{
private HttpServletRequest request;
public MyHtmlRequest(HttpServletRequest request) {
super(request);
this.request = request;
}
@Override
public String getParameter(String name) {
//tomcat帮获取值。
String value = this.request.getParameter(name);
if(value==null){
return null; //检查值是否存在
}
return filter(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());
}
}
标签:
原文地址:http://www.cnblogs.com/lcpholdon/p/4417789.html