Hi all,
My code is as follows

<a href="/web/contacts/contactSearch.jsp">Search</a>

when i click this link, it will go to contactSearch.jsp. I want to send an action to the servlet when clicking this link, and display the data from the servlet on the contactSearch.jsp.

The data I mentioned is a dynamic data, which I am getting from backend through "collections".

Please help me on this....really stuck at this point

Dani AI

Generated

As originally linked straight to the JSP, the clean solution is to point the link at a servlet (mapped URI), have that servlet load the collection from the backend, place the collection in request scope, and then dispatch to the JSP for rendering. This follows the controller/view separation and avoids embedding business logic in the JSP. As suggested, forward the request from the servlet so request attributes remain available to the JSP.

Example pieces to apply immediately:

<!-- web.xml mapping (alternative: use @WebServlet("/contacts") with Servlet 3.0+) -->
<servlet>
  <servlet-name>ContactServlet</servlet-name>
  <servlet-class>com.example.ContactServlet</servlet-class>
</servlet>
<servlet-mapping>
  <servlet-name>ContactServlet</servlet-name>
  <url-pattern>/contacts</url-pattern>
</servlet-mapping>
<!-- link to invoke servlet and send an action -->
<a href="${pageContext.request.contextPath}/contacts?action=search">Search</a>
/* servlet (doGet) */
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
    throws ServletException, IOException {
  if ("search".equals(req.getParameter("action"))) {
    List<Contact> contacts = contactService.findContacts(...); // fetch collection
    req.setAttribute("contacts", contacts == null ? Collections.emptyList() : contacts);
    req.getRequestDispatcher("/contacts/contactSearch.jsp").forward(req, resp);
  } else {
    resp.sendError(HttpServletResponse.SC_BAD_REQUEST);
  }
}
<!-- contactSearch.jsp -->
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<c:forEach var="c" items="${contacts}">
  ${c.name} (${c.email})<br/>
</c:forEach>

Troubleshooting and cautions: forwarding preserves request attributes; a redirect causes a new request and will lose them unless data is reloaded or stored in session. Verify servlet mapping, attribute name spelling, JSTL availability on the classpath, and server logs for exceptions. Avoid putting large collections into session; use request scope for a single render and session only when data must persist across multiple requests.

Recommended Answers

All 2 Replies

instead of the URI for the result JSP, place the URI for the servlet (as mapped in the web.xml deployment descriptor) in the link tag.
Then in the servlet place the result of its operation in the request or session (if it should be persistent between requests) and forward the request to the result jsp with request.getRequestDispatcher().forward() (or something like that).

Could you please be more specific...

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.