Framework/Ajax2011. 9. 21. 19:52

package Opertion;

import java.io.FileInputStream;
import java.io.IOException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Properties;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
// 이 파일은 톰캣이 동작하면 실행하는 클래스로
//@Override
//public void init(ServletConfig config)
//이 파일을 먼저 읽습니다.
//init에서는 .forums의 내용을 읽어서 처리를 합니다
// 즉 Model의 역할을 합니다.
//doGet = get방식       doPost = post방식 들어올때 처리하는 Method며
//extradite(request, response); 이 메소드에서는
//View역할을 합니다. 
 

public class Controller extends HttpServlet {

	private Map conversion = new HashMap();

	@Override
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {

		extradite(request, response);
	}

	@Override
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		extradite(request, response);
	}

	@Override
	public void init(ServletConfig config) throws ServletException {
		String readProperties = config.getInitParameter("forums");
		Properties passProperties = new Properties();
		FileInputStream wantProperties = null;

		try {
			wantProperties = new FileInputStream(readProperties);
			passProperties.load(wantProperties);

		} catch (IOException e) {
			throw new ServletException(e);
		} finally {
			if (wantProperties != null)
				try {
					wantProperties.close();
				} catch (IOException ex) {
				}
		}

		Iterator takeKeyValue = passProperties.keySet().iterator();

		while (takeKeyValue.hasNext()) {
			String keyValue = (String) takeKeyValue.next();
			String className = passProperties.getProperty(keyValue);

			try {
				Class makeClass = Class.forName(className);
				Object makeObject = makeClass.newInstance();
				conversion.put(keyValue, makeObject);
			} catch (Exception e) {
				System.out.println("클래스가 안만들어졌나보네요");
				e.printStackTrace();
			}
		}
	}

	public void extradite(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		JustAvatar lmpl = null;

		try {
			String takeUri = request.getRequestURI();
			if (takeUri.indexOf(request.getContextPath()) == 0) {
				takeUri = takeUri.substring(request.getContextPath().length());
			}
			lmpl = (JustAvatar) conversion.get(takeUri);

		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}

'Framework > Ajax' 카테고리의 다른 글

AJAX 예제(9) (Testlmpl.java(2))  (0) 2011.09.21
AJAX 예제(8) (Testlmpl.java(1))  (0) 2011.09.21
AJAX 예제(6) (forums & ⓜⓥⓒ ⓒ=Control 설정)  (0) 2011.09.21
AJAX 예제(5) (Servlet 설정)  (0) 2011.09.21
AJAX 예제(3) (test.js)  (0) 2011.09.21
Posted by NeverTry