[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