: 함수를 정의한 TLD 파일을 작성한다.
: web.xml 파일에 TLD 내용을 추가한다.
: EL(Expression Language)에서 함수를 사용한다.
- web.xml
: 웹서비스를 할 때 필요한 파일이 어디있는지 설정하는 것. (웹프로그래밍에 대한 설정을 하는 것)
<jsp-config> <taglib> <taglib-uri> /WEB-INF/tlds/el-fun.tld </taglib-uri> <taglib-location> /WEB-INF/tlds/el-fun.tld </taglib-location> </taglib> </jsp-config>
: web.xml에서 taglib를 가져다 쓰겠다는 것.
: 이것에 대한 url은 el-fun.tid안에 있다.
: 또한 el-fun.tld는 tlds 안에 있으므로 tlds 폴더를 만들어서 그 안에 el-fun.tld를 만들어준다.
- el-fun.tld
<description>EL에서 함수실행</description>
<tlib-version>1.0</tlib-version>
<short-name>ELfunctions</short-name>
<uri>/ELFunctions</uri>
<function>
<description>계산기</description>
<name>add</name>
<function-class>mm.OperatorTest</function-class>
<function-signature>
int add(java.lang.String ,java.lang.String,java.lang.String)
</function-signature>
</function>
: 이 함수의 이름은 ELfunction
: mm.OperatorTest은 패키지명.해당클래스명
: <name>add</name> 와 int add(...) 은 이름이 같아야함
- Cal.jsp 소스
<%@page import="mm.OperatorTest"%>
<%@ page language="java" contentType="text/html; charset=EUC-KR"
pageEncoding="EUC-KR"%>
<!-- el-fun.tld를 사용할 수 있게됨 -->
<%@taglib prefix="oper" uri="/WEB-INF/tlds/el-fun.tld"%>
<!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>
<center> e1을 이용한 폼데이터 처리 <br>
<form action = "Cal.jsp" method = "post">
<input type = "text" name = "x" value = "${param['x']}">
<select name = "op" value = "${param['op']}">
<option value="+">+</option>
<option value="-">-</option>
<option value="*">*</option>
<option value="/">/</option>
</select>
<input type = "text" name = "y" value="${param['y']}">
<input type = "submit" value = "전송"><br><br>
연산의 결과는 : ${oper:add(param['x'], param['op'], param['y'])} 입니다
</form>
</center>
</body>
</html>
- OperationTest.java 소스
package mm;
public class OperatorTest {
public static int add(String a, String b, String c){
int result = 0;
if(b.equals("+")){
result = Integer.parseInt(a)+Integer.parseInt(c);
}
else if(b.equals("-")){
result = Integer.parseInt(a)-Integer.parseInt(c);
}
else if(b.equals("*")){
result = Integer.parseInt(a)*Integer.parseInt(c);
}
else if(b.equals("/")){
result = Integer.parseInt(a)/Integer.parseInt(c);
}
return result;
}
}
- web.xml 소스
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
id="WebApp_ID"
version="3.0">
<jsp-config>
<taglib>
<taglib-uri>
/WEB-INF/tlds/el-fun.tld
</taglib-uri>
<taglib-location>
/WEB-INF/tlds/el-fun.tld
</taglib-location>
</taglib>
</jsp-config>
</web-app>
- el-fun.tld 소스
<?xml version="1.0" encoding="euc-kr"?>
<taglib xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-jsptaglibrary_2_1.xsd"
version="2.1">
<description>EL에서 함수실행</description>
<tlib-version>1.0</tlib-version>
<short-name>ELfunctions</short-name>
<uri>/ELFunctions</uri>
<function>
<description>계산기</description>
<name>add</name>
<function-class>mm.OperatorTest</function-class>
<function-signature>int add(java.lang.String ,java.lang.String,java.lang.String)</function-signature>
</function>
</taglib>
- 결과
- LoginEL.jsp
<%@ 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>
<center>
<form action = "LoginProc.jsp" method = "post">
아이디 : <input type = "texT" name = "id"><br>
비밀번호 : <input type = "password" name = "pass"><br>
<input type = "submit" value = "로그인"><br>
</form>
</center>
</body>
</html>
- LoginProc.jsp
<%@ 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>
아이디 : ${param.id}
</body>
</html>
- 결과