*
The moose likes JSP and the fly likes passing session values between two web application Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » JSP
Bookmark "passing session values between two web application" Watch "passing session values between two web application" New topic
Author

passing session values between two web application

preethi Ayyappan
Ranch Hand

Joined: Oct 04, 2007
Posts: 518
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

Joined: Nov 30, 2011
Posts: 7

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

Joined: Jan 08, 2009
Posts: 1477
    
    6

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" />


Swastik
preethi Ayyappan
Ranch Hand

Joined: Oct 04, 2007
Posts: 518
Should i give the context path in web.xml of both the web application?
preethi Ayyappan
Ranch Hand

Joined: Oct 04, 2007
Posts: 518
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

Joined: Jan 08, 2009
Posts: 1477
    
    6

Worked for me without any issues

preethi Ayyappan
Ranch Hand

Joined: Oct 04, 2007
Posts: 518
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

Joined: Jan 08, 2009
Posts: 1477
    
    6

It should be application specific. So you need to create the file under META-INF within your web app.
preethi Ayyappan
Ranch Hand

Joined: Oct 04, 2007
Posts: 518
The context.xml file should be in both the webapplication?
Swastik Dey
Rancher

Joined: Jan 08, 2009
Posts: 1477
    
    6

Probably no, it has worked for me by keeping it in app1.
preethi Ayyappan
Ranch Hand

Joined: Oct 04, 2007
Posts: 518
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

Joined: Jan 08, 2009
Posts: 1477
    
    6

After adding context.xml under META-INF, have you restarted the server?
preethi Ayyappan
Ranch Hand

Joined: Oct 04, 2007
Posts: 518
yes. I have restarted. Any other issue?
Swastik Dey
Rancher

Joined: Jan 08, 2009
Posts: 1477
    
    6

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

Joined: Jan 08, 2009
Posts: 1477
    
    6

Another query? Have you overriden the init method. If yes, make sure you are calling super.init.
preethi Ayyappan
Ranch Hand

Joined: Oct 04, 2007
Posts: 518
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

Joined: Jan 08, 2009
Posts: 1477
    
    6

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

Joined: Oct 04, 2007
Posts: 518
I have tried the same. But i am still getting null. I have to change anything in web.xml?
Swastik Dey
Rancher

Joined: Jan 08, 2009
Posts: 1477
    
    6

I don't think so, mine has just the servlet and servlet-mapping sections.
Swastik Dey
Rancher

Joined: Jan 08, 2009
Posts: 1477
    
    6

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

Joined: Jan 08, 2009
Posts: 1477
    
    6

Other approach could be is to pass the values through query string.
preethi Ayyappan
Ranch Hand

Joined: Oct 04, 2007
Posts: 518
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

Joined: Oct 04, 2007
Posts: 518
I am using Tomcat 5.5. Is there any issue in using this version for cross context?
Swastik Dey
Rancher

Joined: Jan 08, 2009
Posts: 1477
    
    6

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

Joined: Oct 04, 2007
Posts: 518
I have tried in tomcat7. but still facing the same issue. I am using spring annotation. It may be the reason?
Swastik Dey
Rancher

Joined: Jan 08, 2009
Posts: 1477
    
    6

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

Joined: Oct 04, 2007
Posts: 518
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.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: passing session values between two web application