[index.jsp]
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<c:set var="contextPath" value="${pageContext.request.contextPath}" />
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<script src="https://code.jquery.com/jquery-3.7.1.min.js" integrity="sha256-/JqT3SQfawRcv/BIHPThkBvs0OEvtFFmqPF/lYI/Cxo=" crossorigin="anonymous"></script>
<script>
$(function(){
$('#btn').click(function(){
if($('#type').val() === '1'){
location.href = '${contextPath}/getDate.do';
} else if($('#type').val() === '2'){
location.href = '${contextPath}/getTime.do';
} else if($('#type').val() === '3'){
location.href = '${contextPath}/getDatetime.do';
}
})
})
</script>
</head>
<body>
<div>
<form method="get">
<select id="type">
<option value="1">현재날짜</option>
<option value="2">현재시간</option>
<option value="3">현재날짜시간</option>
</select>
<button id="btn" type="button">요청</button>
</form>
</div>
</body>
</html>
id=type의value가 1일때) location.href = '${contextPath}/getDate.do';
id=type의value가 2일때) location.href = '${contextPath}/getTime.do';
id=type의value가 3일때)location.href ='${contextPath}/getDatetime.do';
[MvcController.java]
request.setCharacterEncoding("UTF-8");
response.setContentType("text/html; charset=UTF-8");
String requestURI = request.getRequestURI(); /* /mvc/getDate.do */
String contextPath = request.getContextPath(); /* /mvc */
String urlMapping = requestURI.substring(contextPath.length()); /* /getDate.do */
// 서비스 객체 생성(MVC Pattern에서 Model에 해당함)
MvcService mvcService = new MvcServiceImpl();
// 서비스 실행 결과(어디로 어떻게 이동할 것인가에 관한 정보가 저장)
ActionForward af = null;
switch(urlMapping) {
case "/getDate.do":
af = mvcService.getDate(request);
break;
case "/getTime.do":
af = mvcService.getTime(request);
break;
case "/getDatetime.do":
mvcService.getDatetime(request, response);
break;
}
af가 null이 아닐때,
isRedirect()가 true라면 주어진 경로로 리다이렉트,
isRedirect()가 false라면 주어진 경로로 포워드.
if(af != null) {
if(af.isRedirect()) {
response.sendRedirect(af.getPath());
} else {
request.getRequestDispatcher(af.getPath()).forward(request, response);
}
}
[MvcService.java]
package service;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import common.ActionForward;
public interface MvcService {
public ActionForward getDate(HttpServletRequest request);
public ActionForward getTime(HttpServletRequest request);
public void getDatetime(HttpServletRequest request, HttpServletResponse response);
}
[MvcServiceImpl.java]
package service;
import java.io.PrintWriter;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import common.ActionForward;
public class MvcServiceImpl implements MvcService {
@Override
public ActionForward getDate(HttpServletRequest request) {
request.setAttribute("today", LocalDate.now().toString());
return new ActionForward("views/date.jsp", false);
}
@Override
public ActionForward getTime(HttpServletRequest request) {
request.setAttribute("now", LocalTime.now().toString());
return new ActionForward("views/time.jsp", false);
}
@Override
public void getDatetime(HttpServletRequest request, HttpServletResponse response) {
String datetime = LocalDateTime.now().toString();
PrintWriter out = null;
try {
out = response.getWriter();
out.println("<script>");
out.println("alert('" + datetime + "')"); // 요청 결과 보여주기
out.println("location.href = '" + request.getContextPath() + "'"); // 이동하기 (redirect와 같은 방식의 이동)
out.println("</script>");
out.flush();
out.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
[date.jsp]
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<c:set var="contextPath" value="${pageContext.request.contextPath}" />
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<script src="https://code.jquery.com/jquery-3.7.1.min.js" integrity="sha256-/JqT3SQfawRcv/BIHPThkBvs0OEvtFFmqPF/lYI/Cxo=" crossorigin="anonymous"></script>
</head>
<body>
${today}
</body>
</html>
[time.jsp]
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<c:set var="contextPath" value="${pageContext.request.contextPath}" />
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<script src="https://code.jquery.com/jquery-3.7.1.min.js" integrity="sha256-/JqT3SQfawRcv/BIHPThkBvs0OEvtFFmqPF/lYI/Cxo=" crossorigin="anonymous"></script>
</head>
<body>
${now}
</body>
</html>


