We can also put filter chains in front of JSPs.
Create a calculator JSP:
Here's the JSP:
<%!
private double calcAmount(int age,
double income, int years) throws ServletException {
if (age < 0 || income < 0 ||
years < 0) {
throw new
ServletException("inputs must be > 0");
}
double result = 1500.0;
if (age < 65) {
result = 0;
} else if (age < 75) {
result *= (age- 65)/10.0;
}
if (50000 < income) {
result = 0;
} else if (20000 < income) {
result *= (50000 -
income)/30000;
}
if (years < 10) {
result = 0;
} else if (years < 20) {
result *= (years - 10)/10.0;
}
return result;
}
%>
<html>
<head>
<title>calculator</title>
</head>
<body>
<%
int age = new
Integer(request.getParameter("age"));
double income = new
Double(request.getParameter("income"));
int years = new
Integer(request.getParameter("years"));
double stipend = calcAmount(age,
income, years);
%>
You could qualify for
$<%=stipend%>/month!!!
</body>
</html>
We must make some changes. We must change the action of the form:
action = "servlet.jsp"
We must also add the following mappings to web.xml:
<filter-mapping>
<filter-name>AgeFilter</filter-name>
<url-pattern>calculator.jsp</url-pattern>
</filter-mapping>
<filter-mapping>
<filter-name>IncomeFilter</filter-name>
<url-pattern>calculator.jsp</url-pattern>
</filter-mapping>