1

I've been scratching my head for hours now. I'm working expanding a web app and I've come across this problem:

I have a class called SelectionHandler which uses cases in a doPost() method to forward to the right app:

private void userEditor(HttpServletRequest request, HttpServletResponse response) {
response.setHeader("destination", "edit_user");
    HttpRedirectHandler.forwardToURL(request, response, "EditUser");
}

Now this servlet is designed to be re-used, so it will decide where to forward the user next:

String header = response.getHeader("destination");
if (header.equals("price_manager")) {
    destination = "PriceManager";
} else if (header.equals("new_user")){
    destination = "NewUser";
};

and then

HttpRedirectHandler.forwardToURL(request, response, destination);

But before that happens I've been redirecting the user to a JSP page. Problem is, when I do that the information where I wanted to go gets lost.
Can someone advise me as to what is the best way to retain this information? I tried reading the header and writing it again to a header in the JSP page, but I'm not sure that is actually possible.

informatik01
  • 16,038
  • 10
  • 74
  • 104
kacpr
  • 440
  • 1
  • 8
  • 28
  • Related: [Passing variables from JSP to servlet](http://stackoverflow.com/a/15797418/814702) – informatik01 Nov 12 '13 at 17:11
  • Thank you. As you probably noticed I am not accustomed to jsp's. – kacpr Nov 13 '13 at 08:15
  • Right now I am trying to pass the data via a hidden form field and use POST. I don't see anyone giving that example (or maybe I'm stupid), so would that be an ok solution to my problem? – kacpr Nov 13 '13 at 08:22
  • btw Are you sure you need to retrieve an HTTP header from the response? Maybe you need to retrieve them from the request using [HttpServletRequest.html#getHeader(java.lang.String)](http://docs.oracle.com/javaee/7/api/javax/servlet/http/HttpServletRequest.html#getHeader(java.lang.String))? Maybe you're doing this intentionally, then sorry. Also *HttpRedirectHandler* is not a class from Java EE API, so it's hard to say what exactly happens when you call `HttpRedirectHandler.forwardToURL()`. – informatik01 Nov 13 '13 at 09:13
  • Related to your reply to my comment: it doesn't matter whether you pass some data to a servlet from a JSP or from an ordinary (X)HTML page. Take a look at this popular tutorials, maybe you'll find them useful for your case: [Beginning & Intermediate Servlet & JSP Tutorials](http://courses.coreservlets.com/Course-Materials/csajsp2.html) – informatik01 Nov 13 '13 at 09:21
  • 1
    Yeah, it's pretty good, I've been using it. There's another one on website called tutorialspoint, if you look for jsp tutorials. I've been pretty confused which function to retrieve what and from where - for example that
    data can be taken from request by GetParameter. But I think I'm getting it now. So far using the hidden form fields works for me, so thank you all for your replies.
    – kacpr Nov 13 '13 at 11:21

2 Answers2

0

example:

In your servlet:

request.setAttribute("HelloKey","Hello World!");

In your jsp:

String myHelloString = (String)request.getAttribute("HelloKey");

or you put it in the session:

request.getSession(true).setAttribute("HelloKey","Hello World!");

and in the jsp:

String myHelloString = (String)request.getSession().getAttribute("HelloKey");
nano_nano
  • 12,351
  • 8
  • 55
  • 83
0

I agree with the previous post, you can store information in request, session, or application scope.

I would like to expand on the answer as follows: From your statement about "I tried reading the header and writing it again to a header in the JSP page", it appears you may not have enough of a grasp on JSP. I suggest you go to amazon.com and look for a JSP book that has good reviews. Then, read it cover to cover. It will save you countless hours of experimenting with the technology and not getting anywhere.

user2810910
  • 279
  • 1
  • 2