How do I create a JSP page that cal开发者_Python百科culates tax? We have to use regular expressions to validate the user input. After submitting the form, the user will see the tax amount. The tax is calculated based on the following tax table. You also need to add currencySymbol="$"
to the <fmt:formatNumber>
tag.
Total Income Tax Rate
$1 - $5000 0%
$5001 - $20000 5%
$20001 and above 7%
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt"%>
<html>
<head><title>Assignment One - Queston 4</title>
<script type="text/javascript">
function verify() {
regExp = /\d+\b/;
if (!(regExp.test(thisForm.income.value))) {
alert("You must enter valid income amount!");
return false;}
return true;
}
</script>
</head>
<body>
<fmt:setLocale value="en-US" />
<c:set var="amount" value="${param.income}" />
<c:if test='${empty param.income}'>
<c:set var="amount" value="0"/>
</c:if>
<b>Enter the amount of total income for this year:</b>
<form name="thisForm" action="Q4.jsp" method="post"
onsubmit="return verify();">
Amount of sales:<input name=income size=10 value="?"> <br/><br/>
<input type=submit value="Calculate Tax">
</form>
<%-- Put Code to calculate the tax here --%>
</body>
</html>
The above is the current code I have now. Can someone help me?
I assume that you are new to JSP and you do not know how to get started. Here is a rough guide:
- To retrieve user input: use the
request.getParameter(yourFormField)
method of theHttpServletReques
t class. - To validate the input: use the
matches(pattern)
method of theString
class. - Calculate your output (e.g. taxRate) and display it in your JSP page.
精彩评论