开发者

return vector from servlet

I have to do an HTML page with 2 text boxes, one for name and the other one for ammount, then theres a widget that let me choose which type of account im creating, savings or checking, then a send button, this information is going to be sent to the servlet. The servlet have to create an object depending on the type of account, then save it on a Vector, then the servlet need to respond an HTML textarea with the information thats on the Vector, and the same widgets that were in the first page so you can insert another account, when you insert another one you have to开发者_Go百科 send the information to the same servlet, and then do the same work. But in the HTML text area must appear the first account and the one that I just created, and so on and on.

The thing is that I can do all of this but what I can't do is the showing all the information of the vector for some reason I just get the account that I just created in the text area.

Here's the servlet code. Note, the toString() returns all of the info thats stored on the Vector, and addAccount() adds the account to the Vector.

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;

public class ServletPrincipal extends HttpServlet {
    public void doPost(HttpServletRequest request,
                       HttpServletResponse response)
    throws ServletException, IOException {

        response.setContentType("text/html");

        PrintWriter out = response.getWriter();

        String nombre = request.getParameter("nom");
        String monto = request.getParameter("mon");
        String tipo = request.getParameter("fgcghch");


        double montoi = Double.parseDouble(monto);
String a="ah";
String b="che";

AccountsLedger objeto = new AccountsLedger();

if(a.equals(tipo)){

    SavingsAccount cnt1= new SavingsAccount(nombre, montoi, 2);

    objeto.addAccount(cnt1);

    objeto.toString();
    out.println("<textarea rows='20' cols='20'>"+objeto.toString()+"</textarea>");
    out.print("<form action='ServletPrincipal' method='post'><input type='text' name='nom'><input type='text' name='mon'>");
    out.println("<select name='fgcghch'><option value='ah'>Ahorro</option><option value='che'>Cheque</option></select>");
    out.println("<input type='submit' name='boton'></form>");
    }
if(b.equals(tipo)){


CheckingAccount cnt= new CheckingAccount(nombre, montoi);



objeto.addAccount(cnt);
String y = objeto.toString();
out.println("<textarea rows='2' cols='20'>"+y+"</textarea>");
    out.print("<form action='ServletPrincipal' method='post'><input type='text' name='nom'><input type='text' name='mon'>");
    out.println("<select name='fgcghch'><option value='ah'>Ahorro</option><option value='che'>Cheque</option></select>");
    out.println("<input type='submit' name='boton'></form>");


}




    }
}


First, don't use Vector, use ArrayList. It works the same but does not used synchronized keywords on the methods which is actually a good thing for performance. It is very rare indeed that two threads will access an array and if they did the synchronized keyword is pretty useless anyway for a collection class.

Second, consider if you want to write a servlet here or whether a JSP would be better. A JSP is basically an inside out servlet so if there is more HTML than Java it makes sense.

Thirdly, to iterate an ArrayList with modern Java is trivial, so if the intent is to create a string to insert into HTML, then it can be done like this:

ArrayList<String> values = // ... built somewhere
StringBuilder sb = new StringBuilder();
for (String v : values) {
  sb.append(v);
}
out.println(sb.toString());


If I understand your problem right, you need to hold the objeto for more than one request. Now, you create a new one for every request. Do not use a member of the servlet for this (servlets need to be stateless), but you can use the session.

Instead of

AccountsLedger objeto = new AccountsLedger();

use

HttpSession session = request.getSession();
AccountsLedger objeto = (AccountsLedger)session.getAttribute("objeto");
if (objeto == null) {
  objeto = new AccountsLedger();
  session.setAttribute("objeto", objeto);
}


All you had to do is create the object accountledget before the doPost method

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜