aspose file tools*
The moose likes JSF and the fly likes how to obtain the response object in jsf Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » JSF
Bookmark "how to obtain the response object in jsf" Watch "how to obtain the response object in jsf" New topic
Author

how to obtain the response object in jsf

Adam Zedan
Ranch Hand

Joined: Jun 10, 2011
Posts: 124

I ma trying to do the following in order to obtaine the response object

import javax.faces.bean.*;
import javax.faces.context.ExternalContext;
import javax.faces.context.FacesContext;

ExternalContext context =FacesContext.getCurrentInstance()
HttpServletResponse response =(HttpServletResponse)context getResponse();
(HttpServletResponse)context.getRresponse.sendRedirect(searchURL);

However which import statement would i need to to get HttpServletResponse.. ?? thanks

Don’t look where you fall, but where you slipped
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18154
    
    8

Adam Zedan wrote:However which import statement would i need to to get HttpServletResponse.. ?? thanks


Notice there's a link in your post? Follow it to the API docs for HttpServletResponse. (I just posted the same link.)
Adam Zedan
Ranch Hand

Joined: Jun 10, 2011
Posts: 124

Paul Clapham wrote:
Adam Zedan wrote:However which import statement would i need to to get HttpServletResponse.. ?? thanks


Notice there's a link in your post? Follow it to the API docs for HttpServletResponse. (I just posted the same link.)


I cant seem to add import javax.servlet.http maybe cause its a jsf project ... any hints ..

Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18154
    
    8

Adam Zedan wrote:I cant seem to add import javax.servlet.http maybe cause its a jsf project ... any hints ..


Hint: Explain the problem. Otherwise we don't know whether the "h" key on your keyboard is broken (so you can't type that) or what.
Adam Zedan
Ranch Hand

Joined: Jun 10, 2011
Posts: 124

Paul Clapham wrote:
Adam Zedan wrote:I cant seem to add import javax.servlet.http maybe cause its a jsf project ... any hints ..


Hint: Explain the problem. Otherwise we don't know whether the "h" key on your keyboard is broken (so you can't type that) or what.


I am trying to gain access to the response object in a jsf project Suppose The code i am using is something like this



in the above code the line



gets underlined since HttpServletResponse is not being found...
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18154
    
    8

But you already seemed to understand that you had to import the HttpServletResponse class; you didn't do that. You implied that there was some reason you couldn't do that; I asked why not. Evidently I didn't ask clearly enough. Why haven't you imported that class?
Adam Zedan
Ranch Hand

Joined: Jun 10, 2011
Posts: 124

Paul Clapham wrote:But you already seemed to understand that you had to import the HttpServletResponse class; you didn't do that. You implied that there was some reason you couldn't do that; I asked why not. Evidently I didn't ask clearly enough. Why haven't you imported that class?


because when i try to do this
import javax.servlet.http ;
I get an error saying
import javax.servlet.http cannot be resolved..
This http is not present in the intellisense which only displays
import javax.servlet.jsp.*;
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18154
    
    8

Come on, it's not that complicated. The name of the type is HttpServletResponse and it's in the javax.servlet.http package. So the fully qualified name of the type is javax.servlet.http.HttpServletResponse and that means the code to import the type is

Adam Zedan
Ranch Hand

Joined: Jun 10, 2011
Posts: 124

Paul Clapham wrote:Come on, it's not that complicated. The name of the type is HttpServletResponse and it's in the javax.servlet.http package. So the fully qualified name of the type is javax.servlet.http.HttpServletResponse and that means the code to import the type is



I wish it was that simple , but it seems as if you don't totally understand my problem... Let me explain again , I am fully aware that in order to use the response object I need to use the javax.servlet.http.* import. but unfortunately when i type the following

import javax.servlet.http.*

The compiler automatically considers this as an error saying this import statement cannot be resolved!!!

The only option i have is to add in the servlet section

import javax.servlet.jsp.*;

let me remind you this is a dynamic project using java server faces 2.0 and I just added a POJO not a servlet!!


Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18154
    
    8

Adam Zedan wrote:I wish it was that simple , but it seems as if you don't totally understand my problem...

That's true.
let me remind you this is a dynamic project using java server faces 2.0 and I just added a POJO not a servlet!!

Well, that isn't really a reminder because you never said that before.

However that does make sense. If you have a POJO then it shouldn't be messing about with the servlet response. Your IDE (I assume you're using one because you mentioned a "project") is acting in good taste there.

Adam Zedan
Ranch Hand

Joined: Jun 10, 2011
Posts: 124

Paul Clapham wrote:
Adam Zedan wrote:I wish it was that simple , but it seems as if you don't totally understand my problem...

That's true.
let me remind you this is a dynamic project using java server faces 2.0 and I just added a POJO not a servlet!!

Well, that isn't really a reminder because you never said that before.

However that does make sense. If you have a POJO then it shouldn't be messing about with the servlet response. Your IDE (I assume you're using one because you mentioned a "project") is acting in good taste there.



so any idea on how i could access the response object in a pojo , after all in a JSF project arent all the objects(beans) pojos ??
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 15662
    
  15

Adam Zedan wrote:
so any idea on how i could access the response object in a pojo , after all in a JSF project arent all the objects(beans) pojos ??


Well, actually, one of the defining characteristics of a Plain Old Java Object is that it really has no business knowing about the innards of external esoterica such as HttpServletResponse. A POJO is something you can inject data into and that you can invoke methods on, and it more or less doesn't care about the framework that contains it.

The point that we've been trying to make, however, is that in JSF, you mess with the ServletResponse at your peril. JSF owns the ServletResponse, not you. It has its own ideas on how to manage the headers and response stream, and you can't just jam stuff into it willy-nilly.

JSF isn't like JSP where the page is a linear translation into logic. Instead, the JSF View is compiled into a 2-dimensional (tree) data structure that is used by the JSF rendering engines as a reference when it creates the output stream. You don't code logic in a JSF view (despite the fact that so many people here keep trying to!), you provide a template.

If you have a Java object that needs direct access to the Response object, you should seriously consider placing it under the control of a Servlet or traditional JSP (not to be confused with a JSF "JSP" View definition). It's perfectly legal to do so. You won't have access to JSF functionality there, but JSF does play nicely with non-JSF request/response handlers in the overall application.


Customer surveys are for companies who didn't pay proper attention to begin with.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: how to obtain the response object in jsf
 
Similar Threads
Question about servlet filters
JNDI names for EJBs
Axis2 client - HTTP status Response
Name TestSession is not bound in this Context
EJB application deployment problem