As it should be to load data from the database on my main page without the User submit a post or get request
suppose this is my jsp home page, I want him popular articles from the database
<body>
<div class="content">
<div class="article"></div>
<div class="article"></div>
<div class="article"></div>
<div class="article"></div>
</div>
</body>
and my servlet that will connet in my db and return a object for populate my jsp
public class ServLetLoad extends HttpServlet {
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
}
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
}
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
}
}
suppose this method connects to the database and returns a list article
List<Article> articles = new ArticleContent ('article').findAll();
where in the servlet I'll put that statement, and how I will do my servlet to be invoked without a post or get request and return the object to my jsp?
Edit
i do this
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
try {
this.factory = Persistence.createEntityManagerFactory("article");
EntityManager em = factory.createEntityManager();
TypedQuery<Article> article= em.createNamedQuery("Article.findAll", Article.class);
List<Article> result = article.getResultList();
request.setAttribute("Article",result);
RequestDispatcher view = request.getRequestDispatcher("index.jsp");
view.forward(request, response);
} finally {
out.close();
}
}
In jsp i dont know recovery the datas i try something like this
<%@page import="java.util.ArrayList"%>
<%@page import="java.util.List"%>
<%@page import="dao.Article"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Home Page</title>
</head>
<body>
<% List<Article> list;
list = request.getAttribute("Article");
%>
</body>
</html>