This week's book giveaway is in the OO, Patterns, UML and Refactoring forum. We're giving away four copies of Refactoring for Software Design Smells: Managing Technical Debt and have Girish Suryanarayana, Ganesh Samarthyam & Tushar Sharma on-line! See this thread for details.
In my earlier questions, I asked about get the Referer IP address and redirecting output on a JSP. I am trying to insure that I am thinking in the correct terms and so my information is as follows; I have a servlet, that is invoked in it I would use request.getRemoteAddr() to return the IP address of the Access Controller. I do my processing and use response.setAttribute("fieldName", Object). As I am setting the attribute to be used by a JSP, what is the Object? After I set the attribute, I call response.getRequestDispatcher("webfiles/my.jsp"). How do I call my.jsp and pass the attribute? What would the code in the JSP look like for the getAttribute call in the JSP page? It has been a long time since I have done servlets and JSPs, so the more detail you can provide the better off I will be. If you wish to email the response, I have included one of my generic email accounts. Thanks in advance. Randall email@example.com
Hi Randall, What you need to do is: when you get the parameter - the IP address, do a request.setAttribute(parama-name, value), the param-name and value being for the IP address. Then do a getRequestDispatcher( on the JSp you want).forward(request.response) In your JSP, retrieve it using request.getParameter(param-name);
I do my processing and use response.setAttribute("fieldName", Object). As I am setting the attribute to be used by a JSP, what is the Object?
Ok, so you have a servlet that does processing, and there are any number of 'items' or 'data points' or even 'objects' that you want to be able to present to a user. The idea is that you would place all of those items into an appropriate scope (you have the choice of 'application', 'session' and 'request'). For this example, we'll stick with request.
So in answer to the question "what is the Object?" it is simply: "Whatever object you want". If you have a database call that returns a value, let's say: "SELECT COUNT(*) FROM INVOICES". This is the number of invoices. Keep in mind, that you can't stick primitives into the request, only "Objects".
After I set the attribute, I call response.getRequestDispatcher("webfiles/my.jsp"). How do I call my.jsp and pass the attribute?
You 'call' it by using either the .forward() or .include() method of the RequestDispatcher. As shown to you earlier by Sri:Perhaps it's clearer on two lines?
As for 'pass the attribute'... there is *no need*, because you've already previously set those attributes with your request.setAttribute() method. They already exist in the request object, and are available in the target JSP through getAttribute()
What would the code in the JSP look like for the getAttribute call in the JSP page?