한글필터
util/CharacterEncodingFilter
package util;
import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import java.io.IOException;
@WebFilter(urlPatterns = "/*")
public class CharacterEncodingFilter implements Filter {
private String encoding;
@Override
public void init(FilterConfig filterConfig) throws ServletException {
encoding = filterConfig.getInitParameter("encoding");
if(encoding==null){
encoding="UTF-8";
}
}
@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
request.setCharacterEncoding(encoding);
chain.doFilter(request,response);
}
}
인코딩 테스트
/test/encodingtest.jsp
<h2>인코딩 테스트 </h2>
<form action="to.jsp" method="post">
<input type="text" name="test">
<input type="submit" value="전송">
</form>
/test/to.jsp
<h2>인코딩 테스트</h2>
<%
String test = request.getParameter("test");
%>
결과 : <%= test %>
/test/testMain.jsp
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ page contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>
<c:set var="context_path" value="${ pageContext.request.contextPath }"/>
<!DOCTYPE html>
<html lang="ko">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<a href="${ context_path }/test/dbconnTest.jsp">커넥션풀 테스트</a> <br>
<a href="${ context_path }/test/encodingtest.jsp">인코딩 테스트</a> <br>
<a href="${ context_path }">메인으로</a>
</body>
</html>
MVC 컨트롤러
mvc/command/CommandHandler
package mvc.command;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public interface CommandHandler {
public String process(HttpServletRequest request, HttpServletResponse response) throws Exception;
}
mvc/command/NullHandler
package mvc.command;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class NullHandler implements CommandHandler{
@Override
public String process(HttpServletRequest request, HttpServletResponse response) throws Exception {
System.out.println("null 핸들러");
response.sendError(HttpServletResponse.SC_NOT_FOUND);
return null;
}
}
mvc/controller/ControllerUsingURI
package mvc.controller;
import mvc.command.CommandHandler;
import mvc.command.NullHandler;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.lang.reflect.InvocationTargetException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Properties;
public class ControllerUsingURI extends HttpServlet {
private Map<String, CommandHandler> commandHandlerMap = new HashMap<>();
@Override
public void init() throws ServletException {
Properties properties = new Properties();
String configFile = getInitParameter("configFile");
String configFilePath = getServletContext().getRealPath(configFile);
try (Reader fileReader = new FileReader(configFilePath);){
properties.load(fileReader);
} catch (IOException e) {
throw new ServletException(e);
}
Iterator<Object> keyIter = properties.keySet().iterator();
while(keyIter.hasNext()){
String command = (String) keyIter.next();
String handlerClassName = properties.getProperty(command);
try {
Class<?> handlerClass = Class.forName(handlerClassName);
CommandHandler commandHandler = (CommandHandler) handlerClass.getDeclaredConstructor().newInstance();
commandHandlerMap.put(command,commandHandler);
} catch (ClassNotFoundException | InstantiationException |
InvocationTargetException | NoSuchMethodException | IllegalAccessException e) {
throw new ServletException(e);
}
}
}
private void proceedRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String command = request.getRequestURI();
if(command.indexOf(request.getContextPath())==0){
command = command.substring(request.getContextPath().length());
}
CommandHandler handler = commandHandlerMap.get(command);
if(handler==null){
handler = new NullHandler();
}
String viewPage = null;
try {
viewPage = handler.process(request,response);
} catch (Throwable e) {
throw new ServletException(e);
}
if (viewPage != null) {
RequestDispatcher dispatcher = request.getRequestDispatcher(viewPage);
dispatcher.forward(request,response);
}
}
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
proceedRequest(request,response);
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
proceedRequest(request,response);
}
}
resources/commandHandler.properties
/testhandler.do = mvc.command.TestHandler
web.xml 설정
<servlet>
<servlet-name>ControllerUsingURI</servlet-name>
<servlet-class>mvc.controller.ControllerUsingURI</servlet-class>
<init-param>
<param-name>configFile</param-name>
<param-value>
/resources/commandHandler.properties
</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>ControllerUsingURI</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
MVC 컨트롤러 테스트
mvc/command/TestHandler
package mvc.command;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class TestHandler implements CommandHandler{
@Override
public String process(HttpServletRequest request, HttpServletResponse response) throws Exception {
System.out.println("TEST 핸들러 실행");
return "/test/handlerTest.jsp";
}
}
/test/handlerTest.jsp
<%@ page contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>
<!DOCTYPE html>
<html lang="ko">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
핸들러 테스트
</body>
</html>
/testmain.jsp
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ page contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>
<c:set var="context_path" value="${ pageContext.request.contextPath }"/>
<!DOCTYPE html>
<html lang="ko">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<a href="${ context_path }/test/dbconnTest.jsp">커넥션풀 테스트</a> <br>
<a href="${ context_path }/test/encodingtest.jsp">인코딩 테스트</a> <br>
<a href="${ context_path }/testHandler.do">컨트롤러 및 핸들러 테스트</a> <br>
<a href="${ context_path }">메인으로</a>
</body>
</html>
애노테이션을 이용한 컨트롤러 맵핑
@WebServlet(
urlPatterns = {"*.do"},
initParams = { @WebInitParam(name="configFile",value = "/resources/commandHandler.properties") }
)
public class ControllerUsingURL extends HttpServlet {
// 생략
}
jsp 회원제 게시판(1) : DB, 의존성, 커넥션 풀 (0) | 2020.07.06 |
---|---|
jsp filter 한글 필터, 캐릭터 인코딩 필터 (0) | 2020.07.06 |
Filter interface, filter setting, 필터 설정, 필터 인터페이스 (0) | 2020.07.06 |
서블릿 컨트롤러, 커맨드 패턴, URI 사용 (0) | 2020.07.06 |
서블릿 리스너 : ServletContextListener 인터페이스 @WebListener (0) | 2020.07.06 |
댓글 영역