• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Sending the results from a Servlet back to a JSP

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It is good to know that there is a friendly place for a Java greenhorn like me. I have an assignment where I need to create a Java screen to interface a file on an AS/400 / iSeries system.

I have created a JSP called test.

Now This JSP calls a servlet called ProcessSKUSearch.java The code for that looks like this:
package com.mscdirect.test;





What I have been doing for testing purposes, is creating a table on the servlet to display the results of an SQL statement. What I need to do is forward these results to another JSP. I have been working with a Java developer who is very limited in his time for me. He suggested that I modify the above servlet code to use RequestDispatcher.forward and the setAttibute method to accomplish this. I have studied some examples, but I don't understand what I am actually sending this new JSP (Lets call is results.jsp), or what it is suppose to do with what I am sending it.

Thanks to anyone that can help me,


BTW, is it possible to add/paste a screen from another browser page to this post? I tried doing it, but it wouldn't paste the screen.






 
Ranch Hand
Posts: 40
Hibernate jQuery
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You're sending results of your Business Logic, written in servlet, performed on the input values(request parameters). Now you're preparing your result page in the same servlet you have written. Results can be rendered on an another JSP page(result.jsp) only when you forward request in servlet to that JSP page(result.jsp).
 
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Your colleague is right. It's considered a poor practice to create HTML in a servlet. Rather, it should place data in request scope and forward to the JSP where JSTL and EL notation is used to access the data and to render the display. Modern JSPs do not contain any Java code.

If you are a newcomer to JSP, you might find these articles helpful:
  • The Secret Life of JSPs
  • The Front Man

  •  
    Jim Gmeiner
    Greenhorn
    Posts: 5
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Thanks Bear. I think that my confusion now is with the 2 JSP files. Does the 2nd JSP file(results.jsp in my example), the one that receives the data forwarded from the servlet now become the interface for my user?

    You see I still have procedural mindset while approaching this problem. I think of the first JSP as my interface to the user, it calls the servlet which does my processing and then sends the data back to the calling JSP which would create my table and allow the user to select another field to do another search on if desired.

    By sending (forwarding) the data to a different JSP, is my user now looking at a different page now, and how does the user go about performing a new search?

    Thanks again for your advice.

     
    Bear Bibeault
    Sheriff
    Posts: 67746
    173
    Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
    • Likes 1
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Each request results in a new response back to the browser, so yes, if you forward to another JSP, the results of that JSP is what will appear to the user. If you want the same interface to be displayed, you'd forward to the same JSP page.

    But it's the same mechanism either way. Each request to the server results in a new response being returned and displayed in the browser. Whether it's a new JSP or the same JSP depends on what you want the user to see.
     
    Jim Gmeiner
    Greenhorn
    Posts: 5
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator




    My issue now is that I am trying to pass a collection list object to the JSP, but I'm not sure how to reference the list object after it has been 'forwarded' back to the JSP.

    This is my code on the JSP:



    Eclipse is yelling at me saying "Syntax error on tokens, delete these tokens"

    I am using the JSP 2.0 method, I found the information below from a PDF document.

    • Servlet
    SomeBean value = LookupService.findResult(...);
    request.setAttribute("key", value);
    RequestDispatcher dispatcher =
    request.getRequestDispatcher
    ("/WEB-INF/SomePage.jsp");
    dispatcher.forward(request, response);
    • JSP 2.0
    ${key.someProperty}
    • JSP 1.2 (Old!)
    <jsp:useBean id="key" type="somePackage.SomeBean"
    scope="request" />
    <jsp:getProperty name="key" property="someProperty" />



    Bear, You mentioned using JSTL and EL notation tags. How would these tags look when looping through list object on the JSP?

    Thanks,


     
    Bear Bibeault
    Sheriff
    Posts: 67746
    173
    Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    You would iterate through the list with the JSTL <c:forEach> tag, and use the EL to access each item.

    For example:


    If the properties of SKUItem need to be accessed: ${item.propertyName}

    P.S. "skusearch" doesn't seem to be an appropriate name for a list of items.
     
    Jim Gmeiner
    Greenhorn
    Posts: 5
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    You are correct. "skusearch" probably isn't the best name, but I'm flying by the seat of my pants right now.
     
    Bear Bibeault
    Sheriff
    Posts: 67746
    173
    Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Understood. :cool:

    But using good names actually improves the readability and understandability of code to a much greater degree than one would think. It's one of those psychological things,

    If it's a list of SkueItem instances any of: skueItems, skueItemList, locatedSkues, and so on would make it clear that the variable holds a reference to a list of skue items.
     
    Jim Gmeiner
    Greenhorn
    Posts: 5
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Ok, making progress albeit slowy. My problem now is I want the results showing up in original jsp (index.jsp) as opposed to the jsp that I currently 'forwarding' it to (searchResults.jsp)

    Here is the code that I added to 'searchResults.jsp':



    Here is the code for index.jsp:



    Now lines 1 & 2 from searchResults.jsp were inserted after Line 1 in index.jsp, and Lines 14 - 25 from searchResults.jsp replaced lines 53 - 60 in index.jsp.

    Now what is happening is this:
    1. I run my jsp form
    2. I enter text into one of my 6 text boxes and hit the "Submit" button
    3. The program crashes with a "Null Pointer Exception"

    Now I know, I know, I know from reading other posts that Java code in the jsp is frowned up, but I have been told that is how I have to do it (reasons unknown).

    With that said, I'm thinking that the reason for the "Null Pointer Exception" is because when I do the getAttribute it comes back null because I haven't done the setAttribute because the servlet hasn't run because I haven't hit the "Submit" button yet. (enough "because's" for one sentence?)

    Should I be checking the "items" object for a null value first and then skipping the "for" statement if it is null?

    Thanks to anyone who has time to respond to this post.
     
    Bear Bibeault
    Sheriff
    Posts: 67746
    173
    Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Jim Gmeiner wrote:My problem now is I want the results showing up in original jsp (index.jsp) as opposed to the jsp that I currently 'forwarding' it to (searchResults.jsp)


    You can easily forward to any JSP you want including the same JSP. Be aware that this will be a completely new request and a new invocation of the JSP. From the point of view of everything involved, it's no different from invoking any other JSP.

    This code is completely egregious for a number of reasons:
    <td> <% out.print(items.get(i).getItemNumber()); %> </td>

    1. Java code in a JSP is ridiculous and irresponsible in 2012.
    2. Even when scriptlets were in vogue, using out.println() i this manner is completely inside out. But as scriptlets shouldn't be used at all, that's moot.

    I know from reading other posts that Java code in the jsp is frowned up, but I have been told that is how I have to do it (reasons unknown).


    You have been badly misinformed. And if this other party told you to do it as above then this is someone whose advice you should avoid like the plague.

    Step 1: Refactor the JSP to use the JSTL and EL instead of scriptlets. There is no valid excuse, none at all, for using scriptlets in a JSP that was not written prior to 2002.
    Step 2: Be sure that your page controller is setting up everything needed for the page. Keep the complexity in the controller, not in the JSP.

    After that, we'll see how much closer you are to your goal and take it from there. You current path is taking you in the wrong direction.
     
    Don't get me started about those stupid light bulbs.
    reply
      Bookmark Topic Watch Topic
    • New Topic