code015.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>setAttribute和getAttribute</title>
</head>
<body>
<%
try
{
int money = 100;
int number = 0;
request.setAttribute("result", money / number);
}
catch(Exception e)
{
request.setAttribute("result", "除数不能为0,页面出现异常");
}
%>
<jsp:forward page="code016.jsp" />
</body>
</html>
本文档展示了一个简单的Java JSP代码片段,演示了如何在尝试进行除法运算时处理可能的异常。如果除数为0,程序会捕获异常并设置一个错误消息到request对象,然后通过jsp:forward标签将请求转发到code016.jsp页面。

5696

被折叠的 条评论
为什么被折叠?



