당신은 다시 JSTL에게 c:catch
태그를 사용하여 사로 잡았 할 수있는 URL이 잘못된 경우 FileNotFoundException
을 던질 것이다 외부 자원을 가져올 c:import
태그를 (단지 /WEB-INF/lib
에서 jstl-1.2.jar 드롭) JSTL를 사용할 수있다. 마지막으로 JSTL c:choose
을 사용하여 iframe 또는 최종 오류를 표시할지 여부를 확인할 수 있습니다.
여기 (JSTL가 설치된)를 copy'n'paste'n'run, SSCCE의 : http://google.com/foo
또는 유효하지 않은 뭔가
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<!doctype html>
<html lang="en">
<head>
<title>SO question 2291085</title>
</head>
<body>
<c:set var="url" value="http://google.com" />
<c:catch var="e">
<c:import url="${url}" varReader="ignore" />
</c:catch>
<c:choose>
<c:when test="${empty e}">
<iframe src="${url}"></iframe>
</c:when>
<c:otherwise>
<p>Error! ${e}</p>
</c:otherwise>
</c:choose>
</body>
</html>
변경 http://google.com
을, 당신은 오류가 대신으로 표시하는 것을 볼 수 있습니다.
버퍼가 있지만 읽지 않도록하기 위해 varReader="ignore"
을 사용 했으므로 동일한 URL을 두 번 요청했기 때문에 비용이 많이 들지는 않습니다.
업데이트는 : 양자 택일로, 당신은 java.net.URLConnection
의 도움으로 doGet()
방법 내부의 요청을 전처리하는 이것에 대한 Servlet
를 사용할 수 있습니다. 여기에 킥오프 예제가 있습니다.
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
URL url = new URL("http://google.com");
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
int status = connection.getResponseCode(); // 200 = OK, 404 = Not Found, etc.
if (status == 200) {
request.setAttribute("url", url);
} else {
// Do your thing to set custom message or request another page.
}
request.getRequestDispatcher("/WEB-INF/page.jsp").forward(request, response);
}
... 그리고
page.jsp
에는 단지
/foo
같은의
url-pattern
에 서블릿을지도하고 대신 JSP의
http:/example.com/contexty/foo
에 전화
<c:if test="${not empty url}">
<iframe src="${url}"></iframe>
</c:if>
같은 것을 가지고있다.