This week's book giveaway is in the XML and Related Technologies forum.
We're giving away four copies of Java XML & JSON and have Jeff Friesen on-line!
See this thread for details.
Win a copy of Java XML & JSON this week in the XML and Related Technologies forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Devaka Cooray
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Junilu Lacar
  • Paul Clapham
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • salvin francis
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Frits Walraven
  • Ganesh Patekar

Set a parameter in Servlet  RSS feed

 
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
   Hello!
 
   I try to set a parameter in request of Servlet 1. For the purpose of sending this parameter to Servlet 2.
   I know, that, instead of this, it is better to use "setAttribute", but by reasons of code-logic I cant use this opportunity.

   In order to create parameter, I use the HttpServletRequestWrapper

   

 Then, in Servlet 1


 Filter




    But without success.
    I suppose, that issue in this fragment
   


   
 
Andrey Dmitriev
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
   I tried to use and more simple approach - without any filters and wrappers:

   In Servlet 1



  In Servlet 2



  But without success
 
Bartender
Posts: 20307
110
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
HttpServletRequest parameters are read-only. You cannot modify, add or delete them.
 
Andrey Dmitriev
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
   
  Hm... To use this construction?



    and



  But it does not work....
 
Sheriff
Posts: 21601
101
Chrome Eclipse IDE Java Spring Ubuntu VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's (partly?) because you shouldn't use == for comparing Strings but equals. Since the attribute may be absent, and getAttribute therefore may return null, perform the check on the constant:
 
Rob Spoor
Sheriff
Posts: 21601
101
Chrome Eclipse IDE Java Spring Ubuntu VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Scratch that. Although it is definitely something you should fix, this is not the cause of the issue. The problem lies with the sendRedirect. Request attributes only exists for the duration of the request. When you send a redirect, the current response will include the redirect content (302 status code, location header, etc). This will trigger the browser to send a new request. This is completely separate request which does not know about any of the previous request attributes. You should either set a session attribute, or use a query parameter in your redirect.

And that's where the comparison comes into place. You did use a query parameter earlier, but there you used == instead of equals. Go back to using a query parameter, but use equals to check the parameter instead of ==.
 
Master Rancher
Posts: 1042
17
Firefox Browser Hibernate IntelliJ IDE Java MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Going by rob's response, my aproach in the duplicate tread wasn't that bad at all?
 
Rob Spoor
Sheriff
Posts: 21601
101
Chrome Eclipse IDE Java Spring Ubuntu VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That wouldn't work, because the query parameters were added to the current request - not the new request. Your simplified version without wrappers looked more like it should work.

Tim Holloway wrote:HttpServletRequest parameters are read-only. You cannot modify, add or delete them.


Not in regular requests. If you create a wrapper like Andrey did in the opening post you can change your entire request. However, you lose that request once the current one ends.
 
Daniel Demesmaecker
Master Rancher
Posts: 1042
17
Firefox Browser Hibernate IntelliJ IDE Java MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What do you mean that wouldn't work, I use it in a project of mine:
 
Andrey Dmitriev
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rob Spoor, I tried to do the following:




  But with the same result. Namely, in log:
ServiceException: Unable to create requested service [org.hibernate.engine.jdbc.env.spi.JdbcEnvironment]
at ru.spb.FactoryClass.getSessionFactory(FactoryClass.java:22)

   The root of issue in that fragment:


  of Factory-class


 
Andrey Dmitriev
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Daniel Demesmaecker,
just a webapp.

  Servlets are mapped in hibernate-configuration file.

  And I'll try to use your approach.
 
Daniel Demesmaecker
Master Rancher
Posts: 1042
17
Firefox Browser Hibernate IntelliJ IDE Java MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thoose are spring annotations,for servlets it's @RequestMapping
 
Rob Spoor
Sheriff
Posts: 21601
101
Chrome Eclipse IDE Java Spring Ubuntu VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Daniel Demesmaecker wrote:What do you mean that wouldn't work, I use it in a project of mine:


I misread your post, I thought you were Andrey... You're approach with adding the query parameter should definitely have worked.
 
The first person to drink cow's milk. That started off as a dare from this tiny ad:
Programmatically Create PDF Using Free Spire.PDF with Java
https://coderanch.com/wiki/703735/Programmatically-Create-PDF-Free-Spire
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!