vigamage
vigamage

Reputation: 2113

Calling a Servlet when loading the index page

I need to load a list of some data from a database and show them on my index JSP page. My way of doing that is call a Servlet in the index page like this.

<body>
    <jsp:include page="listAll" />
    <fieldset>

        <legend>Search Here</legend>
        <input type="text" id="search" name="search"/>
        <input type="button" value="Search" id="searchBtn"/>

    </fieldset>

</body> 

I am going to do my coding in listAll servlet and add an attribute to the request. What I need to know is, Is this way correct? Can I do what I need like that? If not, How to do that?

Upvotes: 0

Views: 6433

Answers (3)

Will Hartung
Will Hartung

Reputation: 118641

In a Java Web App, there is an attribute of the web.xml file called the welcome-file-list.

The welcome-file-list tells the web app what URLs to pull up if nothing more specific is specified.

Typically, this value is configured for index.jsp, but it can be any mapping within the web application.

If you wanted to have a servlet respond, instead of index.jsp, then you would map the servlet properly, and then use that reference in the welcome-file-list.

Consider:

<welcome-file-list>
    <welcome-file>index</welcome-file>
</welcome-file-list>

<servlet>
    <servlet-name>IndexServlet</servlet-name>
    <servlet-class>pkg.IndexServlet</servlet-class>
</servlet>

<servlet-mapping>
    <servlet-name>IndexServlet</servlet-name>
    <url-pattern>/index</url-pattern>
</servlet-mapping>

Here we have the pkg.IndexServlet mapped to /index. We also tell the application that index is the welcome file for this application. So, internally, when the application sees http://host.com/webapp, it will automatically append index to it, and then route that appropriately, which will lead it to the servlet mapped to /index.

Once you have this mapped properly, you want to do a pattern much @Matthias did here, where the servlet gathers the data, and then forwards to the JSP.

Upvotes: 3

Matthias Steinbauer
Matthias Steinbauer

Reputation: 1776

I am not completely sure if I understood what you are trying to do. But I believe that you try show a list of data in a JSP page and you want to implement a Servlet that does the actual heavy-lifting of getting everything from the DB and things like this.

If I were you I would do it this way: Create a servlet, that performs the actual calls against the database and loads your data into a request attribute. Then you forward from the Servlet to a JSP that actually displays the data

protected void doGet(HttpServletRequest request, HttpServletResponse response)  throws ServletException {
    List<DataElement> list = new ArrayList<DataElement>(); // lets assume this is the list we got from the DB
    request.setAttribute("list", list); 
    String nextJSP = "/searchResults.jsp";
    RequestDispatcher dispatcher = getServletContext().getRequestDispatcher(nextJSP);
    dispatcher.forward(request,response);
}

In the JSP you can then access the request attributes and iterate them with JSTL

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<html>
<body>
<c:forEach var="dataElem" items="${list}">
   Item <c:out value="${dataElem.someValue}"/><p>
</c:forEach>
</body>
</html>

Upvotes: 1

Santhosh
Santhosh

Reputation: 8197

Your approach looks ok. you can wrap this things in the form. so that you get them in the servlet.

<form action="servletURL" method="post">
        <input type="text" id="search" name="search"/>
        <input type="submit" value="Search" id="searchBtn"/>
</form>

you can get those values using request#getParameter in your doPost() method.

Upvotes: 0

Related Questions