Jim
Jim

Reputation: 723

Forwarding JSTL SQL Result to Servlet

First of all, I need to make it clear - this is for an academic project. I also feel I should say that I know this is the wrong way of doing what I need to do, however, it's the way I have to do it, so please stick to answering the question if possible rather than alternatives.

I have to display the results of an SQL query either in JSTL or through a servlet, depending on whether the browser is a phone or not. I've managed the JSTL part, and determining whether it's a phone, and I can forward to a servlet fine.

The problem is, I need to be able to access the result variable from the servlet. It's easy enough to access this from the next JSTL page, as I can set the scope to application, however I'm not sure how to go about getting access from the servlet.

I've tried:

request.getParameter("viewResult");

but this only returns a string, whereas I believe I need a

javax.servlet.jsp.jstl.sql.Result

object.

Does anyone know of a way to pass an object from a JSTL JSP page on to a servlet when forwarded?

Upvotes: 1

Views: 578

Answers (1)

BalusC
BalusC

Reputation: 1109132

The HttpServletRequest#getParameter() returns HTTP request parameters which are implicitly always Strings since that's just specified so in the HTTP specification.

You rather want to set it as request attribute. You can do that with var attribute of sql:query.

<sql:query dataSource="${dataSource}" var="result" scope="request">
    SELECT * FROM foo
</sql:query>
<jsp:include page="/servletURL" />

This way it'll be available in the servlet as follows:

Result result = (Result) request.getAttribute("result");

Needless to say that this is indeed not the best practice. Those tags are designed for quick prototyping, not for real world applications (Sun/Oracle's own words). You should rather be obtaining the DB result in doGet() or doPost() method of the servlet (for preprocessing resp postprocessing).

Upvotes: 2

Related Questions