tags:

views:

167

answers:

1

I want to apply MVC2 J2EE approach in my work with JSP pages. I want to separate the code in Servlet and design in JSP. The problem I am facing is that I want to display all users and their data from DB table to HTML table in JSP page Now How should I call the servlet from JSP page, since there is no form in display page and I don't know whether I could use dispatcher since the admin will click on <a href>display users and JSP pages should display all uesrs. How can i make that?

+3  A: 

Just use the doGet() method of the Servlet and call the URL of the servlet instead.

E.g.

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    List<User> users = userDAO.list();
    request.setAttribute("users", users);
    request.getRequestDispatcher("/WEB-INF/users.jsp").forward(request, response);
}

Map this servlet in web.xml as follows:

<servlet>
    <servlet-name>users</servlet-name>
    <servlet-class>com.example.UsersServlet</servlet-class>
</servlet>
<servlet-mapping>
    <servlet-name>users</servlet-name>
    <url-pattern>/users</url-pattern>
</servlet-mapping>

Now the servlet is accessible by http://example.com/context/users.

In the /WEB-INF/users.jsp file use JSTL c:forEach to display users in a table.

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
...
<table>
    <c:forEach items="${users}" var="user">
        <tr>
            <td>${user.name}</td>
            <td>${user.email}</td>
            <td>${user.age}</td>
        </tr>
    </c:forEach>
</table>

The JSP file is to be placed in /WEB-INF folder to avoid that it's accessible without calling the servlet first.

See also:


Update: to install JSTL, take the following steps:

  1. Download jstl-1.2.jar.
  2. Drop it in /WEB-INF/lib folder. Do NOT extract it! Do NOT change web.xml! Some poor online tutorials will suggest this. They're all wrong.
  3. Declare the taglib of interest in top of JSP page as per the JSTL TLD documentation. For example, JSTL core:

     <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
    
BalusC
when i use </c:forEach> it makes error so i download JSTL_imp jar file and JSTL_api jar file and add it to my project , but </c:forEach> didn't recognize yet
Alaa
See update in answer. I assume you're using a Servlet 2.5 container (e.g. Tomcat 6.x and up) and that your `web.xml` is declared as Servlet 2.5.
BalusC
can u tell me what is user in servlet mapping<servlet-mapping> <servlet-name>users</servlet-name> <url-pattern>/users</url-pattern></servlet-mapping>Is it MY JSP page?
Alaa
for each now is work very well thanx , plz see the other comment .
Alaa
No, it is the URL which the Servlet should listen on. If you open http://example.com/context/users, then it will execute any servlet which is listening on an `url-pattern` of `/users` (or `/users/*`). See also the "See also" links for more hints and details. By the way, don't forget to mark the answer accepted (and those on your [previous questions](http://stackoverflow.com/users/303467/alaa), your accept rate is pretty poor).
BalusC