0

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>
  • bold is not needed ;) – jotadepicas Jul 10 '15 at 17:58
  • just for highlight place of question :) – jose pereira Jul 10 '15 at 18:52
  • I have edited your edited code with casting **request.getAttribute** and the loop – Ali Helmy Jul 10 '15 at 22:15
  • it work try bro, my wrong is dont acess by url the servlet, then i put servlet how homepage, now i have this doubt, if my bg is big and slow to load my page dont will load before servlet finish all process right? how should be for this cases, ty anyway – jose pereira Jul 10 '15 at 22:43
  • If you have big data, you can use paging and every page select some data and display them in jsp page. you can send the page number as a parameter to the servlet. see this [Paging with servlet and JSP](http://theopentutorials.com/examples/java-ee/jsp/pagination-in-servlet-and-jsp) – Ali Helmy Jul 10 '15 at 23:00

1 Answers1

0
  • First your servlet you handle the database and get the list of article
  • You can User in the servlet the request.setAttribute("Articles",articales);
  • In your JSP page use <% List<Article> articales = (List< Article> ) request.getAttribute("Articles") %>
  • Don't forget to import all required classes in JSP <%@page import="" %>
Ali Helmy
  • 784
  • 6
  • 18
  • But as the jsp will understand that request.getAttribute ( " Articles" ) this Coming from the servlet that I created to load data. without use get or post from jsp – jose pereira Jul 10 '15 at 17:45
  • Yes when you are in the servlet you just redirect to the jsp page, while redirecting the request is holding the object you attached with. – Ali Helmy Jul 10 '15 at 17:47