[J2EE AntiPattern----Servlet]
1
Servlet J2EE Refactoring Patterns/AntiPatterns 徐迎晓 复旦大学软件学院 xuyingxiao@126.com
2
e Including Common Functionality in Every Servlet ● Introduce Filters e Using Strings for Content Generation ● Use jDom e Template Text in Servlet Use JSPs 反模式及重构方案 如果尽量 Use jsP,如果无法使用JSP,则 Use jdom重构
反模式及重构方案 如果尽量Use JSP,如果无法使用JSP,则Use Jdom重构 3
AntiPattern e Including Common Functionality in Every Servlet
5
Before Refactoring uh1ev4ddot( trier.etR的at工的et enm, ethene数 goet. finetbuL. tostrlegt)S 图中的注释: l实际项日中 中的字符串应该放在 static fina public变量中,并导入(impo含有该变量的类 ggercserweParameterLog9er中的字符串应该放在 static final publie变量中,并导入含有该变量的类 public void doGe(Hlp ServetRequest request, HttpservletreSponse response throws Serve:Exception, OExce 0: i <sues ength: i+
图中的注释: //实际项目中Logger.getLogger(“ServletParameterLogger”);中的字符串应该放在static final public 变量中,并导入(import)含有该变量的类 ----- public class BuzzyServlet extends HttpServlet { // In a real app you should keep strings like the // name of this logger in a static final public // variable and import that class. //实际项目中Logger.getLogger(“ServletParameterLogger”);中的字符串应该放在static final public 变量中,并导入含有该变量的类 private static Logger logger = Logger.getLogger(“ServletParameterLogger”); /** * Constructor for BuzzyServlet. */ public BuzzyServlet() { super(); } public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { logParameters(request); // Do the buzzy business process. } private void logParameters(HttpServletRequest request) { Enumeration enum = request.getParameterNames(); StringBuffer buf = new StringBuffer(128); while (enum.hasMoreElements()) { String name = (String) enum.nextElement(); String values[] = request.getParameterValues(name); buf.append(¡°parameter = ¡°); buf.append(name); buf.append(¡° values = {¡°); for (int i = 0; i < values.length; i++) { buf.append(values[i]); } buf.append(¡°}\n¡±); } logger.fine(buf.toString()); } } 6
. public class Buzzy Servlet extends Httpservlet ( private static Logger logger Logger. getLogger("ServletParameterLogger' public Buzzy servlet blic void doGet(.)throws ServletException, IOEXception l Do the buzzy business process privatevoidlogparamEters(httpservletrequestrequest 将与处理代码——日志记录嵌在 doGet()方法中了——日志记录是硬编码的」 没有灵活性。 要调整日志记录格式怎么办?其他地方也要进行日志怎么办?——多个地方要用 日志的话需要拷贝代码,维护困难 实际应用中,除了日志还会有很多其他公共功能→引入过滤器,见下页
将与处理代码——日志记录 嵌在doGet( )方法中了——日志记录是硬编码的, 没有灵活性。 要调整日志记录格式怎么办? 其他地方也要进行日志怎么办?——多个地方要用 日志的话需要拷贝代码,维护困难 实际应用中,除了日志,还会有很多其他公共功能引入过滤器,见下页 7
Refactoring--lntroduce Filters pubi le la a tea ap yoe how ia wetp stiring. 1x wariable and import that servlet reponse restore wh41e(和num,上ae1指ent们) Ingl enu, nexe() string valueat- request. getParanetervaluesiname) Iint .s 0t .< va luen. 1ength: 14)( but,app f. appendl)” gex, .notbut, costrng》 public vold init(1ltencontig contig) c 重构后放在过滤器中,然后构建部署描述文件 重构 1找出多个Sere中所共有的预处理和后处理代码 2.对每个分别 创建过滤器实现类 构建部署描述文件 相应 Serve中删除预处理和后处理代码 部署和测试
重构后放在过滤器中,然后构建部署描述文件 重构 1.找出多个Servlet中所共有的预处理和后处理代码 2. 对每个分别 创建过滤器实现类 构建部署描述文件 相应Servlet中删除预处理和后处理代码 部署和测试 8
public class Logging Filter implements Filter private static Logger logger= Logger getLogger("ServletParameterLogger) public void doFilter( ServletRe ServletResponse response Filter Chain chain)t public void destroyo( public void init(FilterConfig config)t public class LoggingFilter implements Filter In a real app you should keep strings like the ∥ ame of this logger in a static final public variable and import that class private static Logger logger Logger getLogger("ServletParameterLogger) ServletRequest request, ServletResponse response Filter Chain chain)( Enumeration enum request getParameterNameso: StringBuffer buf new String Buffer(128) while(enum. hasMoreElementso)( String name =(String)enum nextElement o: buf append(name): buf append( values=t) for(int i=0, i< values. length; i++) buf append(values) buf append(A\n) logger. fine(buf. to 0) public void destroyo t
9 public class LoggingFilter implements Filter { // In a real app you should keep strings like the // name of this logger in a static final public // variable and import that class. private static Logger logger = Logger.getLogger("ServletParameterLogger"); public void doFilter( ServletRequest request, ServletResponse response, FilterChain chain) { Enumeration enum = request.getParameterNames(); StringBuffer buf = new StringBuffer(128); while (enum.hasMoreElements()) { String name = (String) enum.nextElement(); String values[] = request.getParameterValues(name); buf.append("parameter = "); buf.append(name); buf.append(" values = {"); for (int i = 0; i < values.length; i++) { buf.append(values[i]); } buf.append("}\n"); } logger.fine(buf.toString()); } public void destroy() { } public void init(FilterConfig config) { } }
Intercepting Filter Pattern Core j2 EE Patterns拦截过滤器模式 10
Core J2EE Patterns 拦截过滤器模式 10
creates Clent invokes FitterManager m FiMterChain invokes Target ordered) Filter creates
11