ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • JSP로 만드는 Hello 서블릿, 모델1과 모델2
    JSP, Servlet 2021. 12. 21. 01:19

    1. JSP로 만드는 Hello 서블릿
        nana.jsp

    <%@ page language="java" contentType="text/html; charset=UTF-8"
        pageEncoding="UTF-8"%>
    <%
    	String cnt_ = request.getParameter("cnt");
    	
    	int cnt = 100;
    	if(cnt_ != null && !cnt_.equals(""))
    		cnt = Integer.parseInt(cnt_);
    %>
    <!DOCTYPE html>
    <html>
    <head>
    	<meta charset="UTF-8">
    	<meta name="viewport" content="width=device-width, initial-scale=1.0">
    	<title>Insert title here</title>
    </head>
    <body>
    	<%for(int i=0; i<cnt; i++) { %>
    	안녕 Servlet!!<br>
    	<%} %>
    	<!-- url에 nana.jsp 뒤에 ?cnt=3 을 입력하면 3번만 반복함 -->
    	
    </body>
    </html>


    2. 스파게티 코드를 만드는 JSP
        실타래 코드. 한번 꼬이면 대책이 없는 코드.
        이런 코드를 지양하기 위해서는 코드가 한 곳에 모여있는 방법이 필요함

    <%@ page language="java" contentType="text/html; charset=UTF-8"
        pageEncoding="UTF-8"%>
    <%
    	int num = 0;
    	String num_ = request.getParameter("n");
    	if(num_ != null && num_.equals(""))
    		num = Integer.parseInt(num_);
    %>
    <!DOCTYPE html>
    <html>
    <head>
    	<meta charset="UTF-8">
    	<meta name="viewport" content="width=device-width, initial-scale=1.0">
    	<title>Insert title here</title>
    </head>
    <body>
    	<%if(num%2 != 0) {%>
    	홀수입니다.
    	<%} else { %>
    	짝수입니다.
    	<%} %>
    	<!-- ?n=3 이라고 전달하면 홀수입니다. 출력 -->
    </body>
    </html>

    3. JSP MVC model1
        코드 블록들을 한 곳에 모을 방법을 찾아야 함
        입력 코드에서 출력할 데이터를 미리 만들고 출력 코드에 출력 데이터만 출력

        출력 데이터: Model
        출력 담당: View [HTML 코드]
        입력과 제어를 담당: Controller [자바 코드]

        <%@ page language="java" contentType="text/html; charset=UTF-8"
        pageEncoding="UTF-8"%>
        <%
            int num = 0;
            String num_ = request.getParameter("n");
            if(num_ != null && num_.equals(""))
                num = Integer.parseInt(num_);
            
            String result;
            
            if(num%2 != 0)
                result = "홀수";
            else 
                result = "짝수";
            
        %>
        <!DOCTYPE html>
        <html>
        <head>
            <meta charset="UTF-8">
            <meta name="viewport" content="width=device-width, initial-scale=1.0">
            <title>Insert title here</title>
        </head>
        <body>
            <%=result %>입니다.
            <!-- ?n=3 이라고 전달하면 홀수입니다. 출력 -->
        </body>
        </html>

    4. JSP MVC model1을 model2 방식으로
        model2: 컨트롤러와 뷰가 물리적으로 분리된 방식. 코드가 물리적으로 분리.

    spag.jsp
    
    <%@ page language="java" contentType="text/html; charset=UTF-8"
        pageEncoding="UTF-8"%>
    
    <!DOCTYPE html>
    <html>
    <head>
    	<meta charset="UTF-8">
    	<meta name="viewport" content="width=device-width, initial-scale=1.0">
    	<title>Insert title here</title>
    </head>
    <body>
    	<%=request.getAttribute("result") %>입니다.
    </body>
    </html>
    Spag.java
    
    package servlet.problem;
    
    import java.io.IOException;
    
    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;
    
    @WebServlet("/spag")
    public class Spag extends HttpServlet{
    	@Override
    	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    		int num = 0;
    		String num_ = request.getParameter("n");
    		if(num_ != null && !num_.equals(""))
    			num = Integer.parseInt(num_);
    		
    		String result;
    		
    		if(num%2 != 0)
    			result = "홀수";
    		else
    			result = "짝수";
    		
    		request.setAttribute("result", result);
    		
    		//redirect : 새로운 요청을 하게 만듦
    		//forward : 현재 작업한 내용을 이어갈 수 있도록 공유
    		RequestDispatcher dispatcher = request.getRequestDispatcher("spag.jsp");
    		dispatcher.forward(request, response);
    		
    	}
    }

     

    댓글