프로그래밍/JSP Servlet

JSP/Servlet - 자바의 정적 메소드를 EL 함수로 등록 및 사용

가카리 2016. 1. 13. 22:35
반응형

 

JSP/Servlet - 자바의 정적 메소드를 EL함수로 등록 및 사용

 

익스프레션 언어를 사용하여 자바의 정석 메소드를 호출하려면 웹컨테이너에 함수로 등록해야 합니다.

 

익스프레션 언어 함수를 등록하려면 TLD(태그 라이브러리 디스크립터) 파일에 등록을 해야합니다.

 

 

WEB-INF 폴더 아래에 tlds라는 폴더를 만들고 math-functions.tld라는 파일을 만들어봅시다.

 

그다음 다음과 같이 입력합니다.

 

math-functions.tld

 

<taglib xmlns="http://java.sun.com/xml/ns/javaee" version="2.1">

    <tlib-version>1.0</tlib-version><!-- 태그라이브러리 버전 -->

    <short-name>math</short-name><!-- 태그라이브러리 이름 -->

    <function><!-- EL함수 등록시작 -->

        <name>random</name><!--EL함수 이름-->

        <function-class>java.lang.Math</function-class><!-- EL함수를 가져올 클래스 -->

        <function-signature>double random()</function-signature><!-- 정적 메소드의 시그네쳐 -->

    </function>

</taglib>

 

 

그 다음 web.xml 파일에서 <jsp-config> 라는 엘리먼트를 만든 후 그 안에 <taglib>태그를 만듭니다.

 

그리고 또 그 아래에 <taglib-uri>와 <taglib-location>을 만듭니다.

 

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" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" 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>http://gakari.tistory/math-functions.tld</taglib-uri><!-- URI 마음대로 정해도됩니다. -->

<taglib-location>/WEB-INF/tlds/math-functions.tld</taglib-location><!-- tld파일의 경로를 의미합니다. -->

</taglib>

</jsp-config>

</web-app>

 

마지막으로 EL함수를 호출하는 JSP파일을 만들어 봅시다.

가장 중요한 부분은 taglib 지시자를 쓰는 부분인데 uri에는 web.xml에 기술했던 uri를 지정해야하고 prefix는 JSP페이지 안에서 사용할 짧은 이름을 지정하면 됩니다.

 

myrandom.jsp

 

<%@ page language="java" contentType="text/html; charset=EUC-KR"

pageEncoding="EUC-KR"%>

<%@taglib prefix="m" uri="http://gakari.tistory/math-functions.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>

 

    랜덤값은? ${m:random()}

 

</body>

</html>

 

 

실행 화면

실행하면 다음과 같이 잘 나옵니다.

 

 

이번에는 자기가 작성한 정적메소드를 EL 에 등록해봅시다.

 

처음에 WEB-INF폴더 아래에 classes폴더를 만들고 그 아래에 gakari라는 폴더를 만들면 위와같이 gakari라는 패키지로 변합니다.

 

그 다음에 MySum.java를 추가합니다.

 

MySum.java

 

package gakari;

 

public class MySum {

    public static int sum(int start, int end){//반드시 정적함수로 해야됨..

        int sum = 0;

        for(int cnt = start; cnt <= end; cnt++)

            sum += cnt;

        return sum;

    }

    

}

 

 

다음은 tld파일을 수정해봅시다.

math-functions.tld

 

<taglib xmlns="http://java.sun.com/xml/ns/javaee" version="2.1">

    <tlib-version>1.0</tlib-version><!-- 태그라이브러리 버전 -->

    <short-name>math</short-name><!-- 태그라이브러리 이름 -->

    <function><!-- EL함수 등록시작 -->

        <name>random</name><!--EL함수 이름-->

        <function-class>java.lang.Math</function-class><!-- EL함수를 가져올 클래스 -->

        <function-signature>double random()</function-signature><!-- 정적 메소드의 시그네쳐 -->

    </function>

    <function>

        <name>mysum</name>

        <function-class>gakari.MySum</function-class><!-- 패키지명.클래스명 -->

        <function-signature>int sum(int, int )</function-signature>

    </function>

</taglib>

 

마지막으로 JSP파일을 작성합니다.

 

Sigma.jsp

 

<%@ page language="java" contentType="text/html; charset=EUC-KR"

pageEncoding="EUC-KR"%>

<%@taglib prefix="m" uri="http://gakari.tistory/math-functions.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>

    ${param.NUM1 }부터 ${param.NUM2 }까지의 합은?<BR><BR>

     : ${m : mysum(param.NUM1, param.NUM2) }

</body>

</html>

 

web.xml (맨위쪽에 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" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" 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>http://gakari.tistory/math-functions.tld</taglib-uri><!-- URI 마음대로 정해도됩니다. -->

<taglib-location>/WEB-INF/tlds/math-functions.tld</taglib-location><!-- tld파일의 경로를 의미합니다. -->

</taglib>

</jsp-config>

</web-app>

 

 

실행 화면

실행 후 주소창에 다음과 같이 파라미터를 입력하면 값이 제대로 나옵니다.

반응형