In: Computer Science
This is for my Advanced Java Programming class. The book we use is Murach's Java Servlet's and JSP 3rd Edition. I need help modifying some code. I will post the code I was told to open that needs to be modified below.
In this exercise, you'll enhance the Future Value application to store the amount and interest rate in the user's session. That way, the user can experiment with different numbers of years to see the value of his or her investment without having to enter the amount and interest rate each time.
Review the project.
Modify the code
http://localhost:8080/ch07_ex3_futureValue (Links to an external site.)Links to an external site.
Since the values are stored in the session, the application should remember them even though you left the Future Value application and went to a different site.
Here's the code I was told to open. I think only the FutureValueServlet class needs to be modified. I just need it to meet the requirements above. This is all in Java. Thank you very much.
index.jsp
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"
%>
<%@include file="header.jsp" %>
<section>
<h1>Future Value Calculator</h1>
<p><i>${message}</i></p>
<form action="calculate" method="post">
<label>Investment Amount:</label>
<c:if test="${investmentAmount != null}">
<input type="text" name="investment"
value="${investmentAmount}"/><br>
</c:if>
<c:if test="${investmentAmount == null}">
<input type="text" name="investment"
value="${calculation.monthlyInvestmentAmount}"/><br>
</c:if>
<label>Yearly Interest Rate:</label>
<c:if test="${interestRate != null}">
<input type="text" name="interest_rate"
value="${interestRate}"/><br>
</c:if>
<c:if test="${interestRate == null}">
<input type="text" name="interest_rate"
value="${calculation.yearlyInterestRate}"/><br>
</c:if>
<label>Number of Years:</label>
<input type="text" name="years"
value="${calculation.years}"/><br>
<label> </label>
<input type="submit" value="Calculate"/><br>
</form>
</section>
<%@include file="footer.jsp" %>
result.jsp
<%@include file="header.jsp" %>
<section>
<h1>Future Value Calculator</h1>
<label>Investment Amount:</label>
<span>${calculation.monthlyInvestmentAmountCurrencyFormat}</span><br
/>
<label>Yearly Interest Rate:</label>
<span>${calculation.yearlyInterestRate}</span><br
/>
<label>Number of Years:</label>
<span>${calculation.years}</span><br />
<label>Future Value:</label>
<span>${calculation.futureValueCurrencyFormat}</span><br
/>
<label> </label>
<br>
<span><a href="index.jsp">Return to
Calculator</a></span>
</section>
<%@include file="footer.jsp" %>
FutureValueServlet.java
package murach.fv;
import java.io.*;
import javax.servlet.*;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.*;
import murach.business.Calculation;
@WebServlet("/calculate")
public class FutureValueServlet extends HttpServlet {
@Override
protected void doPost(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
// get parameters from the request
String investmentString = request.getParameter("investment");
String interestRateString =
request.getParameter("interest_rate");
String yearsString = request.getParameter("years");
// validate the parameters
String url;
String message;
double investment = 0;
double interestRate = 0;
int years = 0;
try {
investment = Double.parseDouble(investmentString);
interestRate = Double.parseDouble(interestRateString);
years = Integer.parseInt(yearsString);
message = "";
url = "/result.jsp";
}
catch (NumberFormatException e) {
message = "Please enter a valid number in all three text
boxes.";
url = "/index.jsp";
}
// store data in Calculation object
Calculation calculation = new Calculation();
calculation.setMonthlyInvestmentAmount(investment);
calculation.setYearlyInterestRate(interestRate);
calculation.setYears(years);
request.setAttribute("calculation", calculation);
request.setAttribute("message", message);
getServletContext()
.getRequestDispatcher(url)
.forward(request, response);
}
@Override
protected void doGet(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
doPost(request, response);
}
}
There are two options you can try:
1. Clear up the memory of the current session
// store data in Calculation object
Calculation calculation = new Calculation();
calculation.setMonthlyInvestmentAmount(investment);
calculation.setYearlyInterestRate(interestRate);
calculation.setYears(years);
request.setAttribute("calculation", calculation);
request.setAttribute("message", message);
getServletContext()
.getRequestDispatcher(url)
.forward(request, response);
HttpSession session = request.getSession(false);
if(session != null) {
//This will clear up the memory of the current session
session.invalidate();
}
2. Delete each attribute of the request
// store data in Calculation object
Calculation calculation = new Calculation();
calculation.setMonthlyInvestmentAmount(investment);
calculation.setYearlyInterestRate(interestRate);
calculation.setYears(years);
request.setAttribute("calculation", calculation);
request.setAttribute("message", message);
getServletContext()
.getRequestDispatcher(url)
.forward(request, response);
HttpSession session = request.getSession(false);
if(session != null) {
//This will clear up the memory of the current session
session.removeAttribute("calculation");
session.removeAttribute("message");
}