Intellij JSP - EL (Expression Language)

1 minute read

EL (Expression Language)

  • Expression Language

    • <%= value %> = ${value}
      • 산술, 관계, 조건, 논리식 사용 가능
    • <jsp:getProperty name="user" property="name"/> = ${user.name}
    • 내장객체
      • pageScope
      • requestScope
      • sessionScope
      • applicationScope
      • param
      • paramValues
      • initParam
  • EL Exmaple

    • User.java
        public class User{
                String name;
                String id;
                String pw;
                String email;
      
                public User(String name, String id, String pw, String email) {
                        this.name = name;
                        this.id = id;
                        this.pw = pw;
                        this.email = email;
                }
      
                public User() {
                }
      
                // getter, setter 생략
        }
      
    • web.xml
        <?xml version="1.0" encoding="UTF-8"?>
        <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
                         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
                         version="4.0">
                ...
      
                <context-param>
                        <param-name>name</param-name>
                        <param-value>Kim</param-value>
                </context-param>
                <context-param>
                        <param-name>location</param-name>
                        <param-value>Seoul</param-value>
                </context-param>
      
                ...
        </web-app>		
      
    • login.jsp
        <%@ page contentType="text/html;charset=UTF-8" language="java" %>
        <html>
        <head>
                <title>Login</title>
        </head>
        <body>
        <form action="home.jsp" method="post">
                <label>ID : <input type="text" name="id"></label><br/>
                <label>Password : <input type="password" name="pw"></label><br/>
                <label>이름 : <input type="text" name="name"></label><br/>
        <input type="submit" value="Login">
        </form>
      
        <%
                session.setAttribute("session_name", "session_value");
                application.setAttribute("application_name", "application_value");
        %>
        </body>
        </html>		
      
    • home.jsp
        <%@ page contentType="text/html;charset=UTF-8" language="java" %>
        <jsp:useBean id="user" class="beans.User"/>
        <jsp:setProperty name="user" property="*"/>
      
        <html>
        <head>
                <title>Home</title>
        </head>
        <h2>Parameter value</h2>
        <hr>
        <body>
        <%
                //page와 request scope는 home.jsp 페이지에서 값을 넣어야 값이 유지 됨
                pageContext.setAttribute("page_name", "page_value");
                request.setAttribute("request_name", "request_value");
        %>
        아이디 ${param.id}<br/>
        비밀번호 ${param.pw}<br/>
        <hr>
        아이디 ${param["id"]}<br/>
        비밀번호 ${param["pw"]}<br/>
        <hr>
      
        <h2>JavaBean</h2>
        Set Email -> ${user.email="ABC@gmail.com"}<br/>
        아이디 : ${user.id}<br/>
        비밀번호 : ${user.pw}<br/>
        이름 : ${user.name}<br/>
        Email : ${user.email}<br/>
        <hr>
      
        <h2>Scope Value</h2>
        <hr>
        pageScope : ${pageScope.page_name}<br/>
        requestScope : ${requestScope.request_name}<br/>
        sessionScope : ${sessionScope.session_name}<br/>
        applicationScope : ${applicationScope.application_name}<br/>
        <hr>
      
        <h2>Context Parameter Value</h2>
        <hr>
        name : ${initParam.name}<br/>
        location : ${initParam.location}
      
        </body>
        </html>	
      
    • 로그인 화면

    • 홈 화면

  • 신입SW인력을 위한 실전 JSP Servlet

Categories:

Updated:

Leave a comment