Bhavin Ragha
Bhavin Ragha

Reputation: 73

linking a servlet to jsp.

Is there a way of linking a servlet to a JSP without using physical url link. so I want the servlet to run and then the servlet to take me to a JSP. any ideas.

Upvotes: 1

Views: 14903

Answers (4)

BalusC
BalusC

Reputation: 1109532

Just invoke the URL of the servlet instead of the URL of the JSP and do the preprocessing job in the doGet() method of the servlet.

E.g. a servlet which loads a list of products before the JSP presents them:

@WebServlet("/products")
public class ProductServlet extends HttpServlet {

    @EJB
    private ProductService productService;

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // Preprocess request: load list of products for display in JSP.
        List<Product> products = productService.list();
        request.setAttribute("products", products);
        request.getRequestDispatcher("/WEB-INF/products.jsp").forward(request, response);
    }

}

Where the JSP look like this:

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/format" prefix="fmt" %>

<!DOCTYPE html>
<html lang="en">
    <head>
        <title>Our Products</title>
    </head>
    <body>
        <h1>Products</h1>
        <table>
            <tr>
                <th>ID</th>
                <th>Name</th>
                <th>Description</th>
                <th>Price</th>
            </tr>
            <c:forEach items="${products}" var="product">
                <tr>
                    <td>${product.id}</td>
                    <td><c:out value="${product.name}" /></td>
                    <td><c:out value="${product.description}" /></td>
                    <td><fmt:formatNumber value="${product.price}" type="currency" /></td>
                </tr>
            </c:forEach>
       </table>
    </body>
</html>

If you go to http://localhost:8080/contextname/products directly, then the servlet's doGet() will be invoked and the products will be loaded from the DB and stored in the request scope and the control will be forwarded to the JSP which in turn presents the results in some pretty HTML markup.

See also:

Upvotes: 2

thejosh
thejosh

Reputation: 137

I think what you want is a forward. The browser URL will maintain the URL of the servlet and attributes put on the request will be available to the jsp.

RequestDispatcher r = getServletContext().getRequestDispatcher("foo.jsp");

r.forward(request, response);

Upvotes: 0

shridatt
shridatt

Reputation: 926

Yes,in the servlet you can add the html code,and then send a redirect to the JSP page.

Upvotes: 0

dantuch
dantuch

Reputation: 9293

Yes, use a framework. Servlets and JPS alone are like water and stones - you can build road with them, but you can't request them to do it alone. You have to sweat, or get some framework to do it for you ;)

I suggest http://www.playframework.org/ (1.2.4 ... 2.0 is less Javish, more Scalish-like) if you are familiar with Java

Upvotes: 0

Related Questions