Question

I am new to using JSP, I have used JSF much before but now need to build a portlet with JSP.

I put a string in my list in my java code every time the user presses the submit button. But each time the list is cleared and only have the size of 1 and i dont know why. I guess I get a new instance of Main each time the page rerender.

How do I fix this?

My JSP:

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<jsp:useBean id="Main" class="com.classes.Main" /> 

<div>

   La Till:  <%= request.getParameter("inputText")%>
   <form name="myForm" action="" method="post" >
      <input type="text" name="inputText" >
      <input type="submit" value="Lägg till!">
   </form>

<%
    String name = request.getParameter("inputText");

    if (name != null && !name.isEmpty()) {
        Main.addToList(name);
    }

%>

<c:forEach var="name" items="${Main.list}">
    <tr>
    <c:out value="${name}"></c:out>
    </tr>
</c:forEach>

My Java:

public class Main extends HttpServlet {

  private List <String> pplList = new ArrayList();

  public void addToList(String inName)
  {
      pplList.add(inName);
  }

  public List <String> getList()
  {
      return pplList;
  }


  public void setList(List <String> inList)
  {
      pplList = inList;
  }

}
Was it helpful?

Solution

Where to start... you seem to have a misunderstanding on how to combine servlets and JSPs, possibly because of your prior exposure to JSF. So you might want to read the Java EE tutorial on servlets and JSPs first.

Having said that, let's look at your code.

First of all, you have to understand that (conceptually) your JSP is executed in its entirety on every request. This also means that the <jsp:useBean> tag is evaluated on every request. This tag looks in your current request attributes for a bean called Main. If it does not exist, it will instantiate a new one.

So in this case, at every render, a new Main object is instantiated, which means that the field initializer private List <String> pplList = new ArrayList(); is executed on every request.

This problem can be solved by using the scope attribute on <jsp:useBean> like this:

<jsp:useBean id="Main" class="com.classes.Main" scope="session" /> 

Now the tag will look for a bean called Main in the session attributes; and will therefore return an existing Main instance on each subsequent request.

That answers your immediate problem.

However, your Main class is weird...

public class Main extends HttpServlet {...

You are using Main as a model (in MVC terminology), so there is no need to extend HttpServlet. The fact that you do, suggests that you wanted to implement a Model 2 servlet. Again, I refer you to the Java EE tutorial.

And finally, you mention that you need to build a portlet. Unfortunately, the Portlet API is somewhat different from the Servlet API -- getting the above code to work will not help you in effectively implementing a portlet.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top