๐Ÿ’ป

JSP ํ”„๋กœ๊ทธ๋ž˜๋ฐ - EL, JSTL ๋ณธ๋ฌธ

KITRI/JSP

JSP ํ”„๋กœ๊ทธ๋ž˜๋ฐ - EL, JSTL

๋˜ํšจ๋‹ˆ 2020. 6. 10. 15:46

EL(Expression Language)

JSTL(Jsp Standard Tag Library)

 

 

 

el.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
	<div>JAVA๋ช…๋ น์–ด๋Š” JSTL, EL์‚ฌ์šฉํ•ด์•ผํ•œ๋‹ค.</div>
	<!-- HTML์ฃผ์„ -->
	<%-- JSP์ฃผ์„ --%>
	
	<%-- 1. ๋ฐ์ดํ„ฐ, ์—ฐ์‚ฐ์ž --%>
	<h3>EL - 1. ์ถœ๋ ฅ, ์—ฐ์‚ฐ์ž ๊ฐ€๋Šฅ (์ง์ ‘ ๋ฐ์ดํ„ฐ ์‚ฌ์šฉ๊ฐ€๋Šฅ, ๋ณ€์ˆ˜(JSTL))</h3>
	<ol>
		<li>์ˆ˜ ์ถœ๋ ฅ: ${10}, ${99.99}</li>
		<li>๋ฌธ์ž(๋ฌธ์ž์—ด) ์ถœ๋ ฅ: ${"apple"}, ${'apple'}</li>
		<li>์—ฐ์‚ฐ: ${10+20}, ${10-20}, ${4/5}, ${5%7}</li>
		<li>์ž‘๋‹ค: ${2<3}, ${2 lt 3}</li>
		<li>ํฌ๋‹ค: ${2>3}, ${2 gt 3}</li>
		<li>์ž‘๊ฑฐ๋‚˜ ๊ฐ™๋‹ค: ${2.5<=3.7}, ${2 le 3}</li>
		<li>ํฌ๊ฑฐ๋‚˜ ๊ฐ™๋‹ค: ${2.8>3.3}, ${2 ge 3}</li>	
		<li>๋…ผ๋ฆฌ์—ฐ์‚ฐ์ž: ${"&&, AND, ||, OR, !, NOT"}</li>
		<li>๋นˆ๋ฌธ์ž์—ด: ${null}</li>
	</ol>
</body>
</html>

 

 

 

MemberInfo.java

package com.java.el;

public class MemberInfo {
    private String name;
    private String id;
    private String pwd;
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getId() {
        return id;
    }
    public void setId(String id) {
        this.id = id;
    }
    public String getPwd() {
        return pwd;
    }
    public void setPwd(String pwd) {
        this.pwd = pwd;
    }
    @Override
    public String toString() {
	return "MemberInfo [name=" + name + ", id=" + id + ", pwd=" + pwd + "]";
    }
    
    
}

 

 

 

memberInfo.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
	<h3>EL - 2. setMethod, getMethod</h3>
	
	<%-- ๊ฐ์ฒด ์„ ์–ธ MemberInfo member = new MemberInfo() --%>
	<jsp:useBean id="member" class="com.java.el.MemberInfo"/>
	
	<%-- setMethod --%>
	${member.setName("์ด์˜์ž")}
	${member.setId("lee1234")}
	${member.setPwd("1234")}
	
	<%-- getMethod / ๊ฑฐ์˜ ์‚ฌ์šฉ ์•ˆํ•จ --%>
	<h3>${member.getName()}</h3>
	<h3>${member.getId()}</h3>
	<h3>${member.getPwd()}</h3>
	<br/><br/>
	
	<h3>${member.name}</h3> 	<%-- get ๋ฉ”์†Œ๋“œ ํ˜ธ์ถœ --%>
	<h3>${member.id}</h3>
	<h3>${member.pwd}</h3>
</body>
</html>

 

 

 

objFormView.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
	<form action="objFormView.jsp" method="post">
		<label>์ด๋ฆ„</label>
		<input type="text" name="name"/>

		<label>์•„์ด๋””</label>
		<input type="text" name="id"/>

		<label>๋น„๋ฐ€๋ฒˆํ˜ธ</label>
		<input type="text" name="pwd"/>
		
		<input type="submit" value="์ „์†ก"/>
		<input type="reset" value="์ทจ์†Œ"/>
	</form>
	
	<% // ์Šคํ”Œ๋ฆฟ : ์ž๋ฐ”์ฝ”๋“œ ์ž‘์„ฑ๊ฐ€๋Šฅ
		java.util.Date date = new java.util.Date();
		
		// ๋‚ด์žฅ๊ฐ์ฒด๋กœ ์ง€์›
		// pageScope : ํ˜„์žฌ ํŽ˜์ด์ง€์—์„œ๋งŒ ๋ฐ์ดํ„ฐ ๊ณต์œ 
		pageContext.setAttribute("pName", "apple");
		
		// requestScope : ์š”์ฒญ์‹œ ๋ฐ์ดํ„ฐ ๊ณต์œ (form, include, forward)
		request.setAttribute("rName", "banana");
	
		// sessionScope : ํ•˜๋‚˜์˜ ์›น์–ดํ”Œ๋ฆฌ์ผ€์ด์…˜์—์„œ ๋ฐ์ดํ„ฐ ๊ณต์œ (cookies, session), ์›น ๋ธŒ๋ผ์šฐ์ € ํ•œ๊ฐœ๋‹น
		session.setAttribute("sName", "melon");
	
		// applicationScope : ํ•˜๋‚˜์˜ ์›น์–ดํ”Œ๋ฆฌ์ผ€์ด์…˜์—์„œ ๋ฐ์ดํ„ฐ ๊ณต์œ , ์›น ์„œ๋ฒ„๊ฐ€ ๊บผ์งˆ ๋•Œ๊นŒ์ง€
		application.setAttribute("aName", "strawberry");
	%>
</body>
</html>

 

 

 

objForm.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
	<!-- EL ๋‚ด์žฅ๊ฐ์ฒด - HTTP ์š”์ฒญ ํŒŒ๋ผ๋ฏธํ„ฐ paramValues -->
	<h3>์ด๋ฆ„: ${param.name}</h3>
	<h3>์•„์ด๋””: ${param.id}</h3>
	<h3>ํŒจ์Šค์›Œ๋“œ: ${param.pwd}</h3>
	<br /><br />
	
	<!-- EL ๋‚ด์žฅ๊ฐ์ฒด  - JSP ์ €์žฅ๊ฐ์ฒด(Scope) ์ฝ๊ธฐ -->
	<ul>
		<li>${pageScope.pName}</li> <%-- ๊ฒฐ๊ณผ์•ˆ๋‚˜์˜ด --%>
		<li>${requestScope.rName}</li>   <%-- ๊ฒฐ๊ณผ์•ˆ๋‚˜์˜ด --%>
		<li>${sessionScope.sName}</li>
		<li>${applicationScope.aName}</li>
	</ul>
</body>
</html>

ํŽ˜์ด์ง€๋ฅผ ๋‹ค ๋„๊ณ  ๋‹ค์‹œ ์‹คํ–‰์‹œํ‚ค๋ฉด strawberry๋งŒ ์‚ด์•„ ์žˆ๋Š”๋‹ค. 

๋ฐ˜์‘ํ˜•
Comments