Now This JSP calls a servlet called ProcessSKUSearch.java The code for that looks like this:
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.
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).
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:
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?
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.
Joined: May 02, 2012
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.
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.
Joined: May 02, 2012
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.
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.