0

I am converting an application, written in Java, to be able to be run through a browser. This is an application to make bets and predict the outcome of sports matches.

The first part of the application allows all users of that round to enter their name.

Then in the doPost of the first servlet, the getParameter calls the "name" parameter from the jsp that requests to send its data to the servlet. I then instantiate a new object with a name, the name being that parameter. This instantiated object is then added to a list. The list is defined in the same servlet.

In a different servlet, I am trying to display the contents of the List via the toString method. But this list always ends up being empty. I expect I am making an error instancing error somewhere but I can't figure it out.

Any advice on how to proceed with this? I have never worked with servlets or jsp files before and have limited HTML knowledge so I am bound to make mistakes on this. I have tried looking for good tutorials but its either all Maven/Vaadin or other files and I am working from Eclipse by using a Dynamic Web Project and no external library files. Ergo, I can't figure it out.

My code:

My servlet that first redirects to the jsp file that has the html code to fill in an users name. It then tries to retreive the content in the doPost:

 @WebServlet("/voerDeelnemersOp")
 public class voerDeelnemersOp extends HttpServlet {
private static final long serialVersionUID = 1L;
private List<Deelnemer> deelnemers = new ArrayList<Deelnemer>();



public List<Deelnemer> getDeelnemers() {
    return deelnemers;
}

public void setDeelnemers(List<Deelnemer> deelnemers) {
    this.deelnemers = deelnemers;
}

public voerDeelnemersOp() {
    super();

}

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

    response.sendRedirect("deelnemerOpvoeren.jsp");

}

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

    String naamDeelnemer = request.getParameter("naam");
    Deelnemer deelnemer = new Deelnemer(naamDeelnemer);
    deelnemers.add(deelnemer);

    doGet(request, response);
}

}

And the servlet that tries to display the array:

    @WebServlet("/bekijkDeelnemers")
    public class bekijkDeelnemers extends HttpServlet {
private static final long serialVersionUID = 1L;
private voerDeelnemersOp voerDeelnemersOp;
private List<nl.WebApp.Deelnemer> deelnemers;

public void init() {
    voerDeelnemersOp = new voerDeelnemersOp();
    deelnemers = voerDeelnemersOp.getDeelnemers();
}

public bekijkDeelnemers() {
    super();

}


protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    PrintWriter out = response.getWriter();
    out.print("deelnemers: ");

    out.print(deelnemers.toString());





}


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

    doGet(request, response);
}

}

The class of the object

public class Deelnemer {

private String naam;
private Integer score;

public Deelnemer(String naam) {
    super();
    this.naam = naam;
    }

public String getNaam() {
    return naam;
}
public void setNaam(String naam) {
    this.naam = naam;
}
public Integer getScore() {
    return score;
}
public void setScore(Integer score) {
    this.score = score;
}   

@Override
public String toString() {
    return "Deelnemer [naam=" + naam + ", score=" + score + "]";
}

}

tushar_lokare
  • 461
  • 1
  • 8
  • 22
JB1989
  • 35
  • 6
  • 2
    You should probably try reading lifecycle of a servlet. – Farhan Qasim Feb 12 '19 at 15:35
  • Possible duplicate of [What is the difference between JSF, Servlet and JSP?](https://stackoverflow.com/questions/2095397/what-is-the-difference-between-jsf-servlet-and-jsp) – ferpel Feb 12 '19 at 15:50

0 Answers0