'output repeats when I reload the page in JSP in Intellij idea
In the following code, I have a JSP file and a TodoList class. In the last line of JSP, the showList (out) method is called, which prints the Todo list. My problem is that every time I reload the page, the showList (out) method Is called, and the list is reprinted, but the interesting thing is that TODO LIST: <br> which was in the body of the method is not reprinted! Even previous out.prints() are not reprinted. Why is that?
servlet container: Tomcat 10.0
update: I changed the TODO LIST: <br> style because the <br> was considered as line breaker by the text editor.
jsp file:
<%@ page import="java.time.LocalDate" %>
<%@ page import="java.util.HashMap" %>
<%@ page import="java.util.Map" %>
<%@ page import="java.util.Iterator" %>
<%@ page import="com.isoft.TodoList" %>
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport"
content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Hello Java Page</title>
</head>
<body>
<h3>Welcome Back! : <%=LocalDate.now()%>
</h3>
<p id="par"></p>
<%
HashMap<Integer, String> students = new HashMap<>();
students.put(391, "Ali");
students.put(923, "Mohammad");
students.put(123, "Ahmad");
students.put(988, "Hossein");
students.put(906, "Hamed");
Iterator<Map.Entry<Integer, String>> iterator = students.entrySet().iterator();
Map.Entry<Integer, String> student;
out.print("Students with id > 500 :<br>");
while (iterator.hasNext())
if ((student = iterator.next()).getKey() > 500)
out.print(student.getValue() + "<br>");
out.print("<hr>");
TodoList todoList = TodoList.getInstance();
todoList.addItem("dentist", LocalDate.of(2022, 12, 23), "I should go to the dentist");
todoList.addItem("park", LocalDate.of(2022, 10, 1), "I should go to the Park");
todoList.showList(out);
</body>
</html>
TodoList class:
package com.isoft;
import jakarta.servlet.jsp.JspWriter;
import java.io.IOException;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.Optional;
public class TodoList {
private static ArrayList<Item> itemsList = new ArrayList<>();
private static final TodoList instance=new TodoList();
private TodoList() {
}
public static TodoList getInstance() {
return instance;
}
public boolean addItem(String title, LocalDate date, String content) {
return itemsList.add(new Item(title, date, content));
}
public boolean removeItem(String title) {
Optional<Item> removedItem = itemsList.stream()
.filter(item -> item.title.equalsIgnoreCase(title))
.findAny();
return removedItem.map(item -> itemsList.remove(item)).orElse(false);
}
public void showList(JspWriter out) throws IOException { //*******we have work with this**********
out.print("TODO LIST:<br>");
itemsList.forEach(i-> {
try {
out.print(i+"<br>");
} catch (IOException e) {
throw new RuntimeException(e);
}
});
}
private static class Item {
private String title;
private LocalDate date;
private String content;
public Item(String title, LocalDate date, String content) {
this.title = title;
this.date = date;
this.content = content;
}
public String getTitle() {
return title;
}
public LocalDate getDate() {
return date;
}
public String getContent() {
return content;
}
@Override
public String toString() {
return "title='" + title +
", date=" + date +
", content=" + content+"\n";
}
}
}
first output:
Welcome Back! : 2022-05-04
Students with id > 500 :
Hamed
Mohammad
Hossein
TODO LIST:
title='dentist, date=2022-12-23, content=I should go to the dentist
title='park, date=2022-10-01, content=I should go to the Park
second reload:
Welcome Back! : 2022-05-04
Students with id > 500 :
Hamed
Mohammad
Hossein
TODO LIST:
title='dentist, date=2022-12-23, content=I should go to the dentist
title='park, date=2022-10-01, content=I should go to the Park
title='dentist, date=2022-12-23, content=I should go to the dentist
title='park, date=2022-10-01, content=I should go to the Park
third reload:
Welcome Back! : 2022-05-04
Students with id > 500 :
Hamed
Mohammad
Hossein
TODO LIST:
title='dentist, date=2022-12-23, content=I should go to the dentist
title='park, date=2022-10-01, content=I should go to the Park
title='dentist, date=2022-12-23, content=I should go to the dentist
title='park, date=2022-10-01, content=I should go to the Park
title='dentist, date=2022-12-23, content=I should go to the dentist
title='park, date=2022-10-01, content=I should go to the Park
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
