Hi there guys,
I got this assignment where I need to set a session attribute. Although I'm having problems since I cant set attribute that is of type double? I got these hints but since I've worked with doubleValue() or downcasting, the hints didn't help me much..
Hints:
-Recall that session attributes are stored as objects and must be downcast.
-Because a Double object cannot be modified, use the Double method, doubleValue(),to return an intrinsic double value.
My code:
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.text.DecimalFormat;
public class SessionBank extends HttpServlet
{
// Create Class Variable
DecimalFormat myFormat = new DecimalFormat("$#,000.00");
public void doGet(HttpServletRequest req,HttpServletResponse res)
throws ServletException, IOException
{
doPost(req,res);
}
public void doPost(HttpServletRequest req,HttpServletResponse res)
throws ServletException, IOException
{
//Makes instance variable a local variable
double accBalance = 0.00;
//Set MIME type of content returned to browser
res.setContentType("text/html");
PrintWriter out = res.getWriter();
//Starts outputting the HTML Form
out.println("<html><head>" +
"<title>Online Bank Simulator</title>" +
"</head><body>" +
"<hr color=\"#808000\">" +
"<center><h1>Banking Simulation</h1></center>" +
"<form method=\"POST\" action=\"../servlet/HTMLBank\">" +
"<center>Amount: <input type=\"text\" name=\"Amount\" size=\"20\"></center>");
//Decides which action to take
String action = req.getParameter("act");
if(action != null)
{
if(action.equals("Deposit"))
{
double amount;
String strAmount = req.getParameter("Amount");
amount = Double.parseDouble(strAmount);
if(amount <= 0.00)
{
out.println("<h2>Error: The amount is either null or a minus</h2>");
}
else
{
accBalance = accBalance + amount;
out.println("<br><center>Balance:"+myFormat.format(accBalance)+" </center> <br>");
}
}
else if(action.equals("Withdraw"))
{
double amount;
String strAmount = req.getParameter("Amount");
amount = Double.parseDouble(strAmount);
if(amount <= 0.00)
{
out.println("<h2>Error: The amount is either null or a minus</h2>");
}
else
{
accBalance = accBalance - amount;
out.println("<br><center>Balance:"+myFormat.format(accBalance)+" </center> <br>");
}
}
else if(action.equals("Balance"))
{
out.println("<br><center>Balance:"+myFormat.format(accBalance)+" </center> <br>");
}
else
{
out.println("<br><center>Balance:"+myFormat.format(accBalance)+" </center> <br>");
}
}
else
{
out.println("<br><center>Balance:"+myFormat.format(accBalance)+" </center> <br>");
}
//Get the current session and set Attribute
HttpSession session = req.getSession(true);
session.setAttribute("accBalance",accBalance);
//Outputs rest of HTML
out.println("<table width=\"35%\" align=\"center\">" +
"<tr><td width=\"33%\" align=\"center\">" +
"<input type=\"submit\" name=\"act\" value=\"Deposit\">" +
"</td>" +
"<td width=\"33%\" align=\"center\">" +
"<input type=\"submit\" name=\"act\" value=\"Withdraw\">" +
"</td>" +
"<td width=\"33%\" align=\"center\">" +
"<input type=\"submit\" name=\"act\" value=\"Balance\">" +
"</td></tr>" +
"</table><br>" +
"</form>" +
"<hr color=\"#80800\">" +
"</body></html>");
}
}
Error at compile time:
setAttribute(java.lang.String,java.lang.Object) in javax.servlet.http.HttpSession cannot be applied to (java.lang.String,double) session.setAttribute("accBalance",accBalance);
Thanks for having a look at this!