• 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

Piecing together a Servlet and JSP interaction

 
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
stepper_97@yahoo.com
 
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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);
 
Ranch Hand
Posts: 3695
IntelliJ IDE Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?

reply
    Bookmark Topic Watch Topic
  • New Topic