2

I have the below code in the script-let. How can rewrite it using JSTL?

<%
    int Queryid = new Integer(request.getParameter(" QueryNo"));
    Queries query = QueriesUtil.findByQid(Queryid);
%>
Aneesh
  • 153
  • 5
  • 18
  • 3
    JSTL is not the right solution either. You're not doing any flow control of HTML output there. It's time to learn servlets to pre-process a request to a JSP page. See also http://stackoverflow.com/questions/3177733/how-to-avoid-java-code-in-jsp-files/3180202#3180202 and http://stackoverflow.com/tags/servlets/info – BalusC Aug 20 '13 at 12:25
  • 1
    +1 to BalusC. Side note: calling toString() on a String is useless. And you should respect the Java naming conventions. – JB Nizet Aug 20 '13 at 12:29
  • Do you mean to set my Queries object to an attribute to the request object in the servlet class and accepting from jsp page using EL? – Aneesh Aug 20 '13 at 12:48
  • sorry for using toString()... I updated it. – Aneesh Aug 20 '13 at 12:49

1 Answers1

4

Your request here should first hit a Servlet that invokes the QueriesUtil (Business/DAO classes in general) to retrieve the Query object as before

Queries query = QueriesUtil.findByQid(Qid);

Then the Servlet needs to make it available in the right scope (say, request for eaxmple)

request.setAttribute("query", Query);

before forwarding it to target JSP using a RequestDispatcher.

RequestDispatcher view = request.getRequestDispatcher("target.jsp");
view.forward(request, response);

Now, assuming Query has a value property it can be retrieved in target.jsp as

<c:out value="${query.value}" />
Ravi K Thapliyal
  • 51,095
  • 9
  • 76
  • 89
  • Thanks for this answer. But, what if If I am going from one jsp page to another, no servlets are executing in between. Consider, having a hyper-link with one parameter QueryNo Open. What should I do in viewQueries.jsp page for the same code above? I need to get the parameter (QueryNo) value from the URL and then need to create a Queries object corresponding to that QueryNo. – Aneesh Aug 20 '13 at 12:58
  • Just replace `href="viewQueries.jsp?QueryNo=4"` by `href="viewQueries?QueryNo=4"` whereby the `viewQueries` is definied as `@WebServlet("/viewQueries")` and put that Java code in servlets' `doGet()` method. That's all. Did you ever bother to take the little effort to click and read the links in my comment? It's all clearly explained over there. – BalusC Aug 20 '13 at 13:16
  • 1
    @Aneesh, all this falls under good practices and promotes the clean separation that MVC advocates. Why bother making half of your page use JSTL when the other half would still be breaking the standards? You may have other reasons to do this but you or whosoever asked you to do it is missing the big picture as outlined by BalusC [here](http://stackoverflow.com/a/3180202/1237040). – Ravi K Thapliyal Aug 20 '13 at 13:22
  • Thanx to both BalusC and Ravi Thapliyal. I read the links provided and understood the concepts as well. The last comment was only to know, is there any other ways to do it with out using servlet as I just started to learn JSTL today ;-) – Aneesh Aug 20 '13 at 13:41