티스토리 뷰

Servlet과 JSP연동

  • Servlet프로그램 로직이 수행되기에 유리하다. IDE 등에서 지원을 좀 더 잘해준다.

ex) 자체가 자바 파일, out.println()를 써가며 html 태그 사용

  • JSP는 결과를 출력하기에 Servlet보다 유리하다. 필요한 html문을 그냥 입력하면 됨.

ex) html을 그냥 작성가능, <%%>를 써가며 자바 코드 작성

  • 프로그램 로직 수행은 Servlet에서, 결과 출력은 JSP에서 하는 것이 유리하다.
  • Servlet과 JSP의 장단점을 해결하기 위해서 Servlet에서 프로그램 로직이 수행되고, 그 결과를 JSP에게 포워딩하는 방법이 사용되게 되었다. 즉 로직은 Servlet이 맡고, 출력은 JSP가 맡는다. 이를 Servlet과 JSP연동이라고 한다.

 

 

 

실습

- LogicServlet 에서 1부터 100사이의 random한 값 2개와, 그 값의 합을 구한 후 그 결과를 result.jsp 에게 포워딩 하는 방법으로 전송하여 결과를 출력한다. 

 

 

*LogicServlet.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
package example;
 
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("/logic")
public class LogicServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;
 
    public LogicServlet() {
        super();
    }
 
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        int v1 = (int) (Math.random() * 100+ 1;
        int v2 = (int) (Math.random() * 100+ 1;
        int result = v1 + v2;
 
        request.setAttribute("v1", v1);
        request.setAttribute("v2", v2);
        request.setAttribute("result", result);
 
        RequestDispatcher rd = request.getRequestDispatcher("/result.jsp"); // WebContent 안의 경로
        rd.forward(request, response);
    }
 
}
 
cs

 

*result.jsp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
<%@ 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>
<%
    int v1 = (int)request.getAttribute("v1");
    int v2 = (int)request.getAttribute("v2");
    int result = (int)request.getAttribute("result");
%>
 
 
<%=v1%> + <%=v2 %> = <%=result %>
</body>
</html>
cs

 

 

- jsp는 출력에 유용한 파일이다. 이 jsp 파일에 자바코드가 너무 많이 있으면 코드를 볼 때 불편할 수 있다.

- 그래서 자바코드를 줄일 수 있는 표기법들이 등장한다. el, jstl 등..

- 나중 수업에서 배우겠지만 간단한 예제로 한번 살펴보자.

 

*el 표기법을 사용한 result.jsp()

1
2
3
4
5
6
7
8
9
10
11
12
<%@ 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>
${v1 } + ${v2 } = ${result }
</body>
</html>
cs

 

 

참고 사이트 : http://www.edwith.org/



본 게시물은 개인적인 용도로 작성된 게시물입니다. 이후 포트폴리오로 사용될 정리 자료이니 불펌과 무단도용은 하지 말아주시고 개인 공부 목적으로만 이용해주시기 바랍니다.

' > 부스트코스' 카테고리의 다른 글

[WEB]EL(Expression Language)  (0) 2018.06.28
[WEB]scope  (0) 2018.06.27
[WEB]redirect, foward  (0) 2018.06.27
[WEB] JSP 내장 객체  (0) 2018.06.04
[WEB]JSP 문법  (0) 2018.06.04
댓글