๐Ÿ’ป

JSP ํ”„๋กœ๊ทธ๋ž˜๋ฐ - ๋กœ๊ทธ๋ฉ”์‹œ์ง€/ ํ”Œ๋Ÿฌ๊ทธ์ธ ๋‹ค์šด ๋ณธ๋ฌธ

KITRI/JSP

JSP ํ”„๋กœ๊ทธ๋ž˜๋ฐ - ๋กœ๊ทธ๋ฉ”์‹œ์ง€/ ํ”Œ๋Ÿฌ๊ทธ์ธ ๋‹ค์šด

๋˜ํšจ๋‹ˆ 2020. 6. 30. 15:44

Command.java

package com.java.mvc05;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public interface Command {
    //์š”์ฒญ์— ๋Œ€ํ•ด์„œ FrontController(์„œ๋ธ”๋ฆฟ)์—์„œ ์ง์ ‘ ์ฒ˜๋ฆฌํ•˜์ง€ ์•Š๊ณ  ํ•ด๋‹น ํด๋ž˜์Šค๊ฐ€ ์ฒ˜๋ฆฌํ•˜๋„๋กํ•œ๋‹ค.
    public String actionDo(HttpServletRequest request, 
	    HttpServletResponse response) throws Exception;
}

FrontCnt.java

package com.java.mvc05;

import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Properties;
import java.util.logging.Logger;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class FrontCnt extends HttpServlet {
    private static final long serialVersionUID = 1L;
    private HashMap<String, Object> commandMap = new HashMap<String, Object>();
    public static final Logger logger = Logger.getLogger(FrontCnt.class.getName());
    public static String logMsg = "logMsg----------------";

    public FrontCnt() {
	super();
	// TODO Auto-generated constructor stub
    }

    @Override
    public void init(ServletConfig config) throws ServletException {
	String configFile = config.getInitParameter("configFile");
	System.out.println(configFile);

	FileInputStream fis = null;
	BufferedInputStream bis = null;
	Properties pro = new Properties();

	try {
	    fis = new FileInputStream(configFile);
	    bis = new BufferedInputStream(fis, 1024);
	    pro.load(bis);

	} catch (Exception e) {
	    e.printStackTrace();
	} finally {
	    try {
		if (bis != null)
		    bis.close();
		if (fis != null)
		    fis.close();
	    } catch (IOException e) {
		e.printStackTrace();
	    }
	}

	Iterator<Object> keyIter = pro.keySet().iterator();
	while (keyIter.hasNext()) {
	    String key = (String) keyIter.next();
	    String value = pro.getProperty(key);
	    logger.info(logMsg + key + "\t" + value);
	}

    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response)
	    throws ServletException, IOException {
	String cmd = request.getServletPath();
	System.out.println(cmd);

//      String view = null;
//      try {
//         Command com = (Command) commandMap.get(cmd);
//         view = com.actionDo(request, response);
//      }catch(Throwable e) {
//         e.printStackTrace();
//      }
//      
//      if(view!=null) {
//         RequestDispatcher rd = request.getRequestDispatcher(view);
//         rd.forward(request, response);
//      }
//      

    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response)
	    throws ServletException, IOException {
	// TODO Auto-generated method stub
	doGet(request, response);
    }

}

ListAction.java

package com.java.mvc05;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class ListAction implements Command {

    @Override
    public String actionDo(HttpServletRequest request, HttpServletResponse response) throws Exception {
	// TODO Auto-generated method stub
	
	request.setAttribute("message", "๋ฆฌ์ŠคํŠธ");
	return "/mvc/list.jsp";
    }

}

WriteAction.java

package com.java.mvc05;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class WriteAction implements Command {

    @Override
    public String actionDo(HttpServletRequest request, HttpServletResponse response) throws Exception {

	request.setAttribute("message", "์“ฐ๊ธฐ");
	return "/mvc/write.jsp";
    }

}

 

index.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
	<a href="http://localhost:8181/mvcExample/write.kitri">Controller - ๊ธ€์“ฐ๊ธฐ</a>
	<a href="http://localhost:8181/mvcExample/list.kitri">Controller - ๊ธ€๋ชฉ๋ก</a>
	<br /><br />
	
	<a href="http://localhost:8181/mvcExample/write.do">SampleController - ๊ธ€์“ฐ๊ธฐ</a>
	<a href="http://localhost:8181/mvcExample/list.do">SampleController - ๊ธ€๋ชฉ๋ก</a>
	<br /><br />

	<a href="http://localhost:8181/mvcExample/write.action">CommandController - ๊ธ€์“ฐ๊ธฐ</a>
	<a href="http://localhost:8181/mvcExample/list.action">CommandController - ๊ธ€๋ชฉ๋ก</a>
	<br /><br />
	
	<a href="http://localhost:8181/mvcExample/write.happy">FrontController - ๊ธ€์“ฐ๊ธฐ</a>
	<a href="http://localhost:8181/mvcExample/list.happy">FrontController - ๊ธ€๋ชฉ๋ก</a>
</body>
</html>

 

 

 

 

commandURI.properties

/write.happy=com.java.mvc05.WriteAction
/list.happy=com.java.mvc05.ListAction

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd" id="WebApp_ID" version="4.0">
  <display-name>mvcExample</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>
  	<servlet-name>Directory</servlet-name>
  	<servlet-class>com.java.mvc01.Directory</servlet-class>
  </servlet>

  <servlet-mapping>
  	<servlet-name>Directory</servlet-name>
  	<url-pattern>*.nhn</url-pattern>
  </servlet-mapping>

  <servlet>
  	<servlet-name>Controller</servlet-name>
  	<servlet-class>com.java.mvc02.Controller</servlet-class>
  </servlet>

  <servlet-mapping>
  	<servlet-name>Controller</servlet-name>
  	<url-pattern>*.kitri</url-pattern>
  </servlet-mapping>
  
  <servlet>
  	<servlet-name>SampleCnt</servlet-name>
  	<servlet-class>com.java.mvc03.Controller</servlet-class>
  </servlet>

  <servlet-mapping>
  	<servlet-name>Controller</servlet-name>
  	<url-pattern>*.do</url-pattern>
  </servlet-mapping>
  
  <servlet>
  	<servlet-name>CommandController</servlet-name>
  	<servlet-class>com.java.mvc04.CommandCnt</servlet-class>
  </servlet>

  <servlet-mapping>
  	<servlet-name>CommandController</servlet-name>
  	<url-pattern>*.action</url-pattern>
  </servlet-mapping>
  
  <servlet>
  	<servlet-name>FrontCnt</servlet-name>
  	<servlet-class>com.java.mvc05.FrontCnt</servlet-class>
  	
  	<init-param>
	  	<param-name>configFile</param-name>
	  	<param-value>C:\sohyunkim\mvc\workspace\mvcExample\WebContent\WEB-INF\commandURI.properties</param-value>
	  </init-param>
  </servlet>
  
  <servlet-mapping>
  	<servlet-name>FrontCnt</servlet-name>
  	<url-pattern>*.happy</url-pattern>
  </servlet-mapping>
</web-app>

 


๋กœ๊ทธ ๋ฌธ์ž ์ƒ‰์ƒ ๋ณ€๊ฒฝ , ๋ฐฐ๊ฒฝ ์„ค์ • ํ”„๋กœ๊ทธ๋žจ ๋‹ค์šด๋กœ๋“œ

 

 

 Help >> Install New Software ...

2. console ์ฐฝ์— INFO ๋กœ๊ทธ๋ฅผ ๋ธ”๋Ÿญ์œผ๋กœ ์„ ํƒํ•œ ํ›„ ์˜ค๋ฅธ์ชฝ ๋งˆ์šฐ์Šค 

 

Add Expression >> Log Output >> Styles & Links >> ์›ํ•˜๋Š” ์Šคํƒ€์ผ ์„ ํƒ or New๋กœ ์ƒ์„ฑ

 

์ž˜ ์ ์šฉ์ด ๋จ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋‹ค.

๋ฐ˜์‘ํ˜•
Comments