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> 커스텀 액션을 사용해도 이전 예제와 동일한 효과를 얻을 수 있다.
'프로그래밍 > JSP Servlet' 카테고리의 다른 글
JSP/Servlet – JSTL <fmt:setLocale> 커스텀 액션 사용하기 (0) | 2016.02.01 |
---|---|
JSP/Servlet – JSTL <fmt:formatNumber> 커스텀 액션 사용하기 (0) | 2016.02.01 |
JSTL/Servlet – JSTL <fmt:formatDate> 커스텀 액션 사용하기 (0) | 2016.01.31 |
JSP/Servlet – JSTL <c:out> 커스텀 액션 사용하기 (0) | 2016.01.31 |
JSTL/Servlet – JSTL <c:redirect> 커스텀 액션 사용하기 (0) | 2016.01.30 |
JSTL/Servlet – JSTL <c:catch> 커스텀 액션 사용 하기 (0) | 2016.01.30 |
JSP/Servlet – JSTL <c:forTokens> 커스텀 액션 사용하기 (0) | 2016.01.30 |
JSP/Servlet – JSTL <c:forEach> 커스텀 액션 사용하기 (0) | 2016.01.25 |