1. 브라우저에서 aaa.do 호출
<?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" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
<display-name>day13</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<servlet>
<description></description>
<display-name>FrontController</display-name>
<servlet-name>FrontController</servlet-name>
<servlet-class>org.thinker.web.FrontController</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>FrontController</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
</web-app>
2. FrontController 에서 받는다.
aaa.do path값을 저장.
CommandFactory lookup메소드 호출 path 파라미터값으로..
CommandFactory에서 넘어온 cmd를 excute(ACommandImpl 모델로 넘어감) aaa.jsp를 리턴.
package org.thinker.web;
import java.io.IOException;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.thinker.util.CommandFactory;
public class FrontController extends HttpServlet {
/**
*
*/
private static final long serialVersionUID = 1L;
private CommandFactory commandFactory;
public FrontController() {
super();
// TODO Auto-generated constructor stub
}
@Override
public void init(ServletConfig config) throws ServletException {
// TODO Auto-generated method stub
super.init(config);
try{
commandFactory = new CommandFactory();
}catch (Exception e){
e.printStackTrace();
throw new ServletException();
}
}
protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
System.out.println("service.............");
String path = request.getServletPath();
path = path.substring(0, path.lastIndexOf("."));
System.out.println("path:"+path);
try{
Command cmd = commandFactory.lookup(path);
String nextPage = null;
nextPage = cmd.execute(request, response);
RequestDispatcher dispatcher = request.getRequestDispatcher(nextPage);
dispatcher.forward(request, response);
}catch (Exception e){
e.printStackTrace();
}
}
}
3. CommandFactory
생성자에서 init() 메소드 호출.
map의 값중 path값에 맞는 값 cmd에 넣기.
package org.thinker.util;
import java.net.URL;
import java.util.HashMap;
import java.util.Map;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.thinker.web.Command;
import org.thinker.web.DefaultCommand;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
public class CommandFactory {
private Map<String, Command> map;
public CommandFactory()throws Exception{
map = new HashMap<String, Command>();
init();
}
private void init()throws Exception{
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
URL url = this.getClass().getClassLoader().getResource("config.xml");
Document document = builder.parse(url.openStream());
NodeList nodeList = document.getElementsByTagName("command");
for(int i =0; i< nodeList.getLength() ; i++){
Element element = (Element)nodeList.item(i);
System.out.println(element.getAttribute("id"));
System.out.println(element.getAttribute("class"));
Command cmd = null;
if( element.getAttribute("class") == null || element.getAttribute("class").trim().length() == 0){
cmd = new DefaultCommand();
}else {
cmd = (Command)(Class.forName(element.getAttribute("class")).newInstance());
}
map.put(element.getAttribute("id"), cmd);
}//end for
}
public Command lookup(String path)throws Exception{
System.out.println("lookup:" + path);
Command cmd = map.get(path);
return cmd;
}
}
3.1. config.xml
4. ACommandImpl