質問

I'm having problems sending data from a servlet to a JSP.

I data comes in from a user form, I modify it and attempt to send it to the output page but that values sent are the same as those obtained on the input page.

When debugging I see the variables changed and updated in the map (created under the hood of the service method) so I'm sure the problem is with the way I'm attempting to send the data to the output page.

My servlet:

public class OpportunityCost extends HttpServlet {
    private static final long serialVersionUID = 1L;

    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    if (request.getParameter("purchasePrice") == null) {
        getServletContext().getRequestDispatcher("/index.jsp").forward(request, response);
        return;
    }

    String iPurchasePrice = request.getParameter("purchasePrice");
    double purchasePrice = Double.parseDouble(iPurchasePrice);
    purchasePrice = purchasePrice * 100;

    request.setAttribute("purchasePrice",  purchasePrice);

    getServletContext().getRequestDispatcher("/output.jsp").forward(request, response);
}
 }

input code:

 <form action="OpportunityCost" method="get"> 
    <table border="0">
        <tr> 
            <td> Home Purchase Price*: <input type="text" value="1" name="purchasePrice" /></td>
        </tr>

        <tr> 
            <td colspan=2"> <input type="submit" value="submit" /> </td>
        </tr>
        </table>

output code:

    String finalPrice = request.getParameter("purchasePrice"); %>
    <%= finalPrice %> 

Any idea what I'm doing wrong?

役に立ちましたか?

解決

Attribute and parameter are two different concepts. With request.setAttribute request.getAttribute you can pass on values internally e.g. between servlets. request.getParameter() is coming from the client. So change your output code to reqeust.getAttribute("purchasePrice").

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top