프로그래밍/JSP Servlet

JSP/Servlet – JSTL <c:url> 커스텀 액션 사용 하기

가카리 2016. 1. 30. 23:25
반응형

 

JSP/Servlet – JSTL <c:url> 커스텀 액션 사용하기

 


2016/01/30 - [프로그래밍/JSP/Servlet] - JSTL/Servlet – JSTL 커스텀 액션 사용하기



<c:url>은 URL를 저장하는 변수입니다. 기본적으로 다음과 같이 사용합니다.

 

    <c:url var="변수이름" value="url주소">

 

또한 param를 지정하고 싶다면 <c:param> 커스텀액션을 사용합니다.

    <c:url var="변수이름" value="url주소">

        <c:param name="데이터 이름" value="데이터 값"/>

        <c:param name="데이터 이름" value="데이터 값"/>

    </c:url>

 

 

Divide.jsp

 

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

pageEncoding="EUC-KR"%>

<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>

<%

    String str1 = request.getParameter("NUM1");

    String str2 = request.getParameter("NUM2");

    int num1 = Integer.parseInt(str1);

    int num2 = Integer.parseInt(str2);

%>

<!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>

    <c:catch var="e"><!-- 일부러 divide by zero 에러를 발생 시킴 -->

        <% int result = num1 / num2; %>

        결과는? <%=result %>

    </c:catch>

    <c:if test="${e != null }" ><!-- 실제 출력되는지 확인 -->

        에러 메시지 : ${e.message }

    </c:if>

</body>

</html>

 

urlRedirect.jsp

 

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

pageEncoding="EUC-KR"%>

<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>

<!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>

    <c:url var="next" value="Divide.jsp">

        <c:param name="NUM1" value="5"/>

        <c:param name="NUM2" value="0"/>        

    </c:url>

    <c:redirect url="${next }"/><!-- 이런식으로 url변수로 수도 있다. -->

</body>

</html>

 

실행 화면

<c:url> 커스텀 액션을 사용해도 이전 예제와 동일한 효과를 얻을 수 있다.

반응형