ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • EL
    JSP, Servlet 2022. 1. 5. 00:04

    1. EL
        EL(Expression Language)
        
        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") %>입니다.
            ${result }
        </body>
        </html>



        spag.java
     

     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);
                
            }
        }




        List list = new ArrayList(){"1", "test" ...};
        request.setAttribute("list", list);

        ((List)request.getAttribute("list")).get(0)
        ${list[0] } 이 코드로 편하게 꺼낼 수 있음

        Map n = new HashMpa("title", "제목");
        request.setAttribute("n", n);

        ((Map)request.getAttribute("n")).get("title")
        &{n.title } 이 코드로 편하게 꺼낼 수 있음

    2. EL의 데이터 저장소
        저장 객체에서 값을 추출하는 순서
            page -> request -> session -> application
            만약 4개 저장소에 같은 키워드를 가진 값이 있다면? 무조건 page 값을 가져옴
            그럴 때 사용할 수 있는 옵션이 있음
            pageScope, requestScope, sessionScope, applicationScope
            예) ${sessionScope.cnt}


    3. EL 연산자
        ${1 > (4/2)}        false
        ${4.0 >= 3}         true
        &{100.0 == 100}     true
        ${(10*10) ne 100}   false
        ${'a'<'b'}          true
        ${'hip' gt 'hit'}   false
        ${4 > 3}            true
        ${1.2E3 + 1.4}      12001.4
        ${3 div 4}          0.75
        ${10 mod 4}         2

        [].
        ()
        not! empty  
        * / div % mod
        + -
        < > <= >= lt gt le ge
        == != eq ne
        && and
        || or
        ? :

        사용하고 있는 html이 기본적으로 <>을 사용하기 때문에
        EL 안에서 <>를 사용하는 것은 바람직하지 않음 -> lt, gt, le, ge 사용

    댓글