There is a Problem in my Project. I have a JSP page and a Java servlet. The servlet have to save a list with names in the session with session.setAttribute(). On the JSP page have to read from the session with session.getAttribute() and print out all names from the list with a for-loop. So the current problem is I don't know how to cast the Obect/ArrayList to a regular String array. I know there are much better ways to do this, but I have to do it this way.
Here is my code
Servlet:
private ArrayList<String> userlist = new ArrayList<String>(Arrays.asList("bla1","bla2","bla3","bla4")); // unsynchronisiert
public void addName(String name){
userlist.add(name);
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
addName(request.getParameter("username"));
System.out.println(userlist);
HttpSession session = request.getSession();
session.setAttribute("userlist", userlist);
}
JSP:
String[] names = (String[]) session.getAttribute("userlist");
System.out.println(names);