显示会话历史记录

时间:2015-08-17 15:38:24

标签: java jsp

我创造了一个小额抵押贷款。计算。并且它正常工作,但我想让我们说出右侧显示的最后三次计算以及删除(清除)它们的能力(如小历史记录或类似记录)。无论我尝试以前的结果都会被删除。对servlet和会话来说是一个新手。

的.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>

<body>
 <div style="width:100%;"> 

<div style="float:left; width:20%;">

<form id="mortgage" action="mocalc" " method="post">
    <p>
    Principal:
        <input name="principal">
    Interest rate (%):
        <input name="interest_rate">
    Monthly payment:
        <input name="monthly_payment">
         <input name="calculate" type="submit" value="Calculate" />
    </form>

<form id="clear" action="index.jsp" method="get"/>
        <input name="clear" type="submit" value="CE"/></form>   

    </p>


</div>
<div style="float:right; width:80%; ">
<p>
<table>
<tr><td>Principal:</td>
    <td align="right">${pr}</td>
<tr><td>Interest Rate:</td>
    <td align="right">${ir}</td>
<tr><td>Monthly Payment:</td>
    <td align="right">${mp}</td>
</tr></table>
<p>

It will take ${y} years and ${m} months to payoff the loan
<p>

Total interest to be paid: $${i}
</div>

</div>
</body>
</html>

的.java

import java.io.IOException;
import java.io.PrintWriter;
import java.text.DecimalFormat;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * Servlet implementation class MoCalc
 */
@WebServlet("/mocalc")
public class MoCalc extends HttpServlet {
    private static final long serialVersionUID = 1L;

    /**
     * @see HttpServlet#HttpServlet()
     */
    public MoCalc() {
        super();
        // TODO Auto-generated constructor stub
    }

    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        response.getWriter().append("Served at: ").append(request.getContextPath());
    }

    /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        Float pr=Float.valueOf(request.getParameter("principal"));
        Float ir = Float.valueOf(request.getParameter("interest_rate"));
        Float mp = Float.valueOf(request.getParameter("monthly_payment"));
        String inter=null, pri=null, ira,mpa;
        DecimalFormat format = new DecimalFormat("#,##0.##");


        //Payoff time in months calculation
        double months=(Math.log10(mp)-Math.log10(mp-pr*(ir/100)/12))/(Math.log10(1+(ir/100)/12));
        int a = (int) (months + 0.5);
        Integer years=a/12;
        Integer month=a%12;

        //Total interest calculation
        double interest=mp*months-pr;

        //Format values
        inter=format.format(interest);
        pri="$"+format.format(pr);
        ira = format.format(ir)+"%";
        mpa="$"+format.format(mp);


        HttpSession session=request.getSession();
        session.setAttribute("y",years);
        session.setAttribute("m",month);
        session.setAttribute("i", inter);
        session.setAttribute("pr", pri);
        session.setAttribute("mp", mpa);
        session.setAttribute("ir", ira);

         RequestDispatcher dispatcher = request.getRequestDispatcher("index.jsp");
          dispatcher.forward(request, response);    }

}

0 个答案:

没有答案