Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

passing session values between two web application

 
preethi Ayyappan
Ranch Hand
Posts: 518
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I have index.jsp in webapp1 and when i login , I can get the parameters using request.getparameter in a servlet in the same webapp1 and set the attributes using session. From there i am redirecting to a jsp named indexdesign.jsp placed in webapp2. When i tried to get the parameters from the servlet in webapp1 using session or request.getparameter, i am getting null value. Is it possible to pass the session values from one webapplication to another? If yes, Kindly help me to solve this issue.

Thanks.
 
Hargobind Singh
Greenhorn
Posts: 7
Eclipse IDE Java Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
war to war collaboration is a bit tricky:

Servlet Context can call context of another war from one , but can't get inside it ..
 
Swastik Dey
Rancher
Posts: 1604
5
Android Java Java ME
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How about passing the values using querystring parameters.

Another approach could be is to use the crosscontext attribute in deployment descriptor. However I am not very sure whether all the servlet containers support this.

<Context path="/myapp" docBase="/home/web/myapp'" crossContext="true" debug="0" reloadable="false" trusted="false" />
 
preethi Ayyappan
Ranch Hand
Posts: 518
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Should i give the context path in web.xml of both the web application?
 
preethi Ayyappan
Ranch Hand
Posts: 518
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I have used the context path <Context path="/webapp2" docBase="/home/web/webapp2'" crossContext="true" debug="0" reloadable="false" trusted="false" /> in the context.xml file of tomcat.

In webapp1, i have got the context using getcontext and forward the request to the jsp in webapp2 using request dispatcher.



But i am getting null pointer exception when trying to get the context. Am i going right? Kindly assist me to solve this issue.

Thanks
 
Swastik Dey
Rancher
Posts: 1604
5
Android Java Java ME
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Worked for me without any issues

 
preethi Ayyappan
Ranch Hand
Posts: 518
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is the context.xml file under tomcat? or should we create a context.xml file in META-INF folder in the webapplication?
 
Swastik Dey
Rancher
Posts: 1604
5
Android Java Java ME
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It should be application specific. So you need to create the file under META-INF within your web app.
 
preethi Ayyappan
Ranch Hand
Posts: 518
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The context.xml file should be in both the webapplication?
 
Swastik Dey
Rancher
Posts: 1604
5
Android Java Java ME
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Probably no, it has worked for me by keeping it in app1.
 
preethi Ayyappan
Ranch Hand
Posts: 518
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks. I have included the following code in my servlet in webapp1


Then I have created a context.xml in META-INF in webapp1. The following is inside the context.xml file.


<Context path="/webapp2" docBase="/usr/local/apache-tomcat/webapps" crossContext="true" debug="0" reloadable="false" trusted="false" />


Then I have tried to get the value using servletcontext in my jsp in webapp2



But I am getting null pointer exception still in the line


ServletContext sc=getServletContext().getContext("/webapp2");


Please assist me
 
Swastik Dey
Rancher
Posts: 1604
5
Android Java Java ME
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
After adding context.xml under META-INF, have you restarted the server?
 
preethi Ayyappan
Ranch Hand
Posts: 518
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yes. I have restarted. Any other issue?
 
Swastik Dey
Rancher
Posts: 1604
5
Android Java Java ME
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I was able to reproduce the issue, only when I didn't have context.xml under META-INF, and after keeping it I didn't restart the server. Otherwise it's working fine here. What server are you using?
 
Swastik Dey
Rancher
Posts: 1604
5
Android Java Java ME
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Another query? Have you overriden the init method. If yes, make sure you are calling super.init.
 
preethi Ayyappan
Ranch Hand
Posts: 518
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
please find the code i have used below:


I have to write it in init method? If yes, how do i redirect to the jsp in init method?
 
Swastik Dey
Rancher
Posts: 1604
5
Android Java Java ME
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You don't have to write it in init method. What I was saying was in case you have overriden init method you must call super.init(). Another point forward or include works within the same application context. You have to use sendRedirect method. Any ways let me show you my code.



 
preethi Ayyappan
Ranch Hand
Posts: 518
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have tried the same. But i am still getting null. I have to change anything in web.xml?
 
Swastik Dey
Rancher
Posts: 1604
5
Android Java Java ME
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't think so, mine has just the servlet and servlet-mapping sections.
 
Swastik Dey
Rancher
Posts: 1604
5
Android Java Java ME
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What server are you using? Because as far as I know all don't support cross context applications. By the way following is my web.xml

 
Swastik Dey
Rancher
Posts: 1604
5
Android Java Java ME
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Other approach could be is to pass the values through query string.
 
preethi Ayyappan
Ranch Hand
Posts: 518
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for your advise swastik. I have tried to pass through url query string and that is working. But the as the url displays the username and password data, i am trying for cross context method. But it is not working for me. I am using Tomcat server.

Is there any possibility to avoid displaying the datas passing in query string in the url?
 
preethi Ayyappan
Ranch Hand
Posts: 518
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am using Tomcat 5.5. Is there any issue in using this version for cross context?
 
Swastik Dey
Rancher
Posts: 1604
5
Android Java Java ME
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am working on tomcat 7 and its working fine, I am not very sure about tomcat 5. As far as the query string is concerned, it will always display the data. So may be you can apply some encryption logic to encrypt the password.
 
preethi Ayyappan
Ranch Hand
Posts: 518
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have tried in tomcat7. but still facing the same issue. I am using spring annotation. It may be the reason?
 
Swastik Dey
Rancher
Posts: 1604
5
Android Java Java ME
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't have much of idea about spring. As per my suggestion just for a trial you can do one thing. You can create a different application with the codes I have posted, and see if works. If it works, then that might be a probable cause.
 
preethi Ayyappan
Ranch Hand
Posts: 518
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have created two servlet application and tried the same. It is working fine. I think the issue is due to spring and i have to follow any other method which spring supports. Thanks for your help.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic