19.05.21 model 2 방식 이해 (jsp model 2 1강)
Back-End/JSP 2019. 5. 21. 11:04-model 2 방식-
model 1 방식은 브라우저측에서 웹 서버 쪽으로 접근할때 jsp가 처리했지만,
(간단한 방식은 model1방식을 사용한다. 궂이 복잡한 서블릿 방식을 사용하지 않아도 처리가 가능하기 때문이다.)
model 2 방식은 브라우저측에서 웹 서버 쪽으로 접근할때 Servlet에서 처리하고, 그 결과만 jsp로 넘겨준다.
그 결과 jsp 페이지는 결과만 출력하게 된다. java코드는 jsp페이지에서 만들지 않고, Servlet에서 java코드를 사용해 처리
이런 jsp와 Servlet로 분리하는 개념을 MVC 라고 한다.
-서블릿 만들기-
-예제 및 출력결과-
서블릿에서 로그인을 처리하고 그 결과를 jsp로 다시보내서 id와 비밀번호를 출력하는 페이지.
LoginForm.jsp (로그인 페이지)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 | <%@ page language="java" contentType="text/html; charset=EUC-KR" pageEncoding="EUC-KR"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <body> <center> <!-- 로그인 페이지 작성 --> <h2>로그인</h2> <!-- 모델2방식은 서블릿에서 처리하기때문에 서블릿으로 보내야한다. --> <form action="LoginProc.do" method="post"> <table width="300" border="1"> <tr height="40"> <td width="120">아이디</td> <td width="180"><input type="text" name="id"></td> </tr> <tr height="40"> <td width="120">패스워드</td> <td width="180"><input type="password" name="password"> </td> </tr> <tr height="40"> <td align="center" colspan="2"><input type="submit" value="로그인"></td> </tr> </table> </form> </center> </body> </html> | cs |
LoginProc.jsp (로그인 처리 페이지)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | <%@ page language="java" contentType="text/html; charset=EUC-KR" pageEncoding="EUC-KR"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=EUC-KR"> <title>Insert title here</title> </head> <body> 넘어온 데이터는 ${id } 와 ${pass } </body> </html> | cs |
LoginProc.java (로그인 처리 서블릿 페이지)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 | package control; import java.io.IOException; import javax.security.auth.message.callback.PrivateKeyCallback.Request; import javax.servlet.RequestDispatcher; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; /** * Servlet implementation class LoginProc */ //jsp에서 자료를 받아 처리하는 서블릿 페이지 //웹 서블릿에 LoginProc기능이 들어간다. 이런것을 어노테이션 이라고 한다. //jsp 파일에서 form태그로 이동하는 파일명과 웹 서블릿 안에 적힌 파일의 이름이 같아야된다. @WebServlet("/LoginProc.do") public class LoginProc extends HttpServlet { // 자바는 get와 Post 방식을 HttpServlet을 상속받지 않고서는 사용할 수 없기 때문에 반드시 HttpServlet을 상속받아야 // 한다. protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { reqPro(request, response); // reqPro로 값을 받을수 있게 한다. } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { reqPro(request, response); // reqPro로 값을 받을수 있게 한다. // jsp에서 form 태그로 보낸 파일형식에 맞춰서 get와 post방식을 구분해서 사용한다. // get방식으로 보냈으면 get방식을 사용하고, post 방식으로 보냈으면 post 방식으로 사용한다. } // doGet와 doPost가 가지고있는 값을 reqPro로 보낸다. public void reqPro(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String id = request.getParameter("id"); String pass = request.getParameter("password"); request.setAttribute("id", id); // request 객체에 데이터를 저장 request.setAttribute("pass", pass); // jsp 쪽으로 처리한 자료를 넘겨준다. (LoginProc.jsp)파일로 넘겨준다. // 처리한 데이터를 RequestDispatcher클래스의 forward 메소드를 (request, response)를 매개변수로 받아서 // 자료를 넘겨준다. RequestDispatcher dis = request.getRequestDispatcher("LoginProc.jsp"); dis.forward(request, response); } } | cs |
'Back-End > JSP' 카테고리의 다른 글
19.05.21 jsp MVC model2 programming-JSTL (jsp model 2 4강~5강) (0) | 2019.05.21 |
---|---|
19.05.21 Expression Laguage 사용법 (jsp model 2 2강~3강) (0) | 2019.05.21 |
(JSP 인강) jsp web MVC model2 programming (0) | 2019.05.21 |
19.05.20 jsp 쇼핑몰 차량 예약 삭제 (동영상 71강) (1) | 2019.05.20 |
19.05.20 jsp 쇼핑몰 차량 구매하기 예약 및 결과 (동영상 69강~70강) (0) | 2019.05.20 |