기본적인 MVC가 설정되어있다면 넘어가도 좋다!
Java(Controller, Model)
Webapp (view)
main_p.MainService.java
package main_p;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public interface MainService {
void execute(HttpServletRequest request, HttpServletResponse response);
}
main_p.MainPage.java
package main_p;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class MainPage implements MainService{
@Override
public void execute(HttpServletRequest request, HttpServletResponse response) {
}
}
package main_p;
import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
@WebServlet("/main/*")
public class MainController extends HttpServlet {
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String service = request.getRequestURI().substring((request.getContextPath() + "/main/").length());
try {
MainService mainService = (MainService) Class.forName("main_p." + service).newInstance();
mainService.execute(request, response);
RequestDispatcher dispatcher = request.getRequestDispatcher("/view/template.jsp");
dispatcher.forward(request, response);
} catch (Exception e) {
e.printStackTrace();
}
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
}
view/template.jsp
<%@pagecontentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>login-practice</title>
</head>
<body>
<jsp:include page="${mainUrl}"/>
</body>
</html>
view/main/mainpage.jsp
<%@taglibprefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@pagecontentType="text/html;charset=UTF-8" language="java" %>
<div>
<h1>로그인</h1>
<button>카카오 로그인</button>
<button>네이버 로그인</button>
</div>
main_p.MainPage.java
public class MainPage implements MainService{
@Override
public void execute(HttpServletRequest request, HttpServletResponse response) {
request.setAttribute("mainUrl", "main/mainpage.jsp");
}
}
<%@ page contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>
<script>
location.href="main/MainPage"
</script>