Results 1 to 14 of 14
  1. #1
    sandeepsai39 is offline Member
    Join Date
    Feb 2009
    Posts
    96
    Rep Power
    0

    Default catching request scoped attributes with redirect

    hi,

    I have some problem,

    Is there is any way to catch request scoped attributes in a jsp that is redirected from a servlet ,I explain this with following example.

    Initially i made a request to some servlet say ex1.java

    In ex1 servlet i wrote some code like this.

    Java Code:
    request.setAttribute("test" ,"some message");
    response.sendRedirect("test1.jsp");
    
    ////
    
    in test1.jsp
    
    result is ${requestScope.test}
    In test1.jsp the output print like below

    "result is "

    I know why it so , because when i redirected to a new url actually response
    send back to client with adding a new status code and new redirected url in response header ,when the client[in this situation browser] ,saw the response status code and new url ,it send back a new request to redirected url,so old request scope is not available for new url ,in my example test1.jsp.so i am unable to catch the attribute that is set to before redirect.

    Is there any way to catch the attributes of request in a redirected url .

    thanks in advance.
    Last edited by sandeepsai39; 11-07-2010 at 07:23 AM.

  2. #2
    FON
    FON is offline Senior Member
    Join Date
    Dec 2009
    Location
    Belgrade, Serbia
    Posts
    368
    Rep Power
    5

    Default

    request scope?

    can u use sessions?

    cookies?

  3. #3
    sandeepsai39 is offline Member
    Join Date
    Feb 2009
    Posts
    96
    Rep Power
    0

    Default

    sessionscope:
    of course session attributes resolve this problem,but i need to change the requestscope to sessionscope in many areas of my module and i need to synchronise all session attributes.

    cookies:
    In worst case user may disable cookies.cookies also may not correct as per my knowledge.

  4. #4
    FON
    FON is offline Senior Member
    Join Date
    Dec 2009
    Location
    Belgrade, Serbia
    Posts
    368
    Rep Power
    5

    Default

    Can you place your attributes in ServletContext and read them after?

    Java Code:
    getServletContext().setAttribute...
    and
    ...getServletContext().getAttributeNames();

  5. #5
    sandeepsai39 is offline Member
    Join Date
    Feb 2009
    Posts
    96
    Rep Power
    0

    Default

    What ever may the scope either session or application scope I have to replace in many areas and also application scope also not thread safe.

  6. #6
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,931
    Rep Power
    19

    Default

    If you need the request data then why are you redirecting?
    Why not simply forward?

    That seems to be the root of your problem. The session shouldn't be used as a dumping ground.

  7. #7
    FON
    FON is offline Senior Member
    Join Date
    Dec 2009
    Location
    Belgrade, Serbia
    Posts
    368
    Rep Power
    5

    Default

    If this is some separate case you can do it outside servlet API, but if you have this in a lot of places...

    Can you use Filters to wrap requests and maybe keep data
    in some util classes for this. You have to find out mechanism to map each request with previous one and recognize data you need?

  8. #8
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,931
    Rep Power
    19

    Default

    Since this:
    Java Code:
    response.sendRedirect("test1.jsp");
    is not going to another server then that should be a forward.

    It really is that simple.

  9. #9
    sandeepsai39 is offline Member
    Join Date
    Feb 2009
    Posts
    96
    Rep Power
    0

    Default

    actually , i give very simple example ,but actually it is not like as simple as test1.jsp . I give for simple example thats it ,i know i always use forward . Only in one case i use redirect.Let us forgot about my example.

    So there is no way to handle request attribute in redirect except as Fon said one solutions ,actually i never used such criteria.

    If the request attribute is a String then i catch this value in redirected url ,using something like this.

    Java Code:
       String attr_value  = req.getAttrbute("name").toString();
       String redirected_url = test1.jsp?attrubuteValue=attr_value [please note test1.jsp is not actual redirected url]
       res.sendRedirect(redirected_url);
    
       In test1.jsp,I can catch value using something below expression.
    
       Attribute value ${params.attrubuteValue}
    Is it work for simple string requsetscope attribute
    Last edited by sandeepsai39; 11-08-2010 at 03:13 PM.

  10. #10
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,931
    Rep Power
    19

    Default

    You might need to encode the values.

    The other option is to get the attribute string as is directly from the request, if you want everything (getQueryString()), but that won't work if the request is a POST.

  11. #11
    sandeepsai39 is offline Member
    Join Date
    Feb 2009
    Posts
    96
    Rep Power
    0

    Default

    thanks Tolls I forgot the post request didn't work well for above example.

    I don't understand encoding the values.You mean to say request.encodeUrl(url);

  12. #12
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,931
    Rep Power
    19

    Default

    If your attrValue string has "special" characters then they need to be encoded into the URL otherwise it won't work. The request coming might have something in (say) German, and the German-specific characters would need to be encoded (usually UTF8 codes).

  13. #13
    RamyaSivakanth's Avatar
    RamyaSivakanth is offline Senior Member
    Join Date
    Apr 2009
    Location
    Chennai
    Posts
    826
    Rep Power
    6

    Default

    Try to use RequestDispatcher and see the sample code below.

    RequestDispatcher reqObj =
    request.getRequestDispatcher("/template.jsp");
    if (reqObj != null) reqObj.forward(request, response);
    Ramya:cool:

  14. #14
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,931
    Rep Power
    19

Similar Threads

  1. Help with Catching Exceptions
    By javaman1 in forum New To Java
    Replies: 4
    Last Post: 09-11-2010, 02:06 AM
  2. After catching the exception thrown
    By scoobyrox in forum New To Java
    Replies: 2
    Last Post: 09-05-2010, 02:29 PM
  3. can we send request attributes from one jsp to jsp
    By java_$life in forum JavaServer Pages (JSP) and JSTL
    Replies: 3
    Last Post: 06-23-2010, 11:08 AM
  4. Catching exception from another library (class)
    By ezilka in forum New To Java
    Replies: 15
    Last Post: 05-26-2010, 06:59 AM
  5. How redirect a request to https
    By vns955 in forum Web Frameworks
    Replies: 1
    Last Post: 11-12-2009, 03:26 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •