开发者

How to pass session value to another web application/project in java

How can I pass the session attribute value to another application which is in same web server. The reason why I should know this is because we have a project which is divide by a module. Each module will redirect to another that pass value using session. That session will be used to identify which user is accessing that module.

Suppose I have a LogIn Module that separate from my other module.

Here my sample code:

Sample Url http://localhost:8080/Login

authorization.jsp : This page will call after user input a userId and then submit

page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"

HttpSession sessionid = request.getSession();
String userId = request.getParameter("userId");

sessionid.setAttribute("userId", userId); 
System.out.printl开发者_开发知识库n("SESSION IS :" + sessionid.getAttribute("userId"));

response.sendRedirect("http://localhost:8080/OtherModule"); 

Sample Url http://localhost:8080/OtherModule

In my Home servlet I will check if the session have a userId

protected void doGet(HttpServletRequest request, HttpServletResponse response){
    HttpSession session = request.getSession();
    if(session.getAttribute("userId") != null){
        System.out.println("SESSION ID @ GET: " + session.getAttribute("userId"));
    } else {
        System.out.println("No Session for userId");
    }
}

//I also tried this with post but still I can't get the session

I hope this information may give you idea what's wrong with my code. Please help me with this. Thanks


You will have to configure your web-server accordingly. Tomcat for example provides a valve for that. See here: http://tomcat.apache.org/tomcat-6.0-doc/config/host.html#Single_Sign_On

Note: The localhost-URL you posted, only works on your computer (hence the name "local").

It would be much easier though to just add all of your modules into one Web-Application or to use one of the countless Java Web Application Frameworks.


If your shared data is related to authentication/login, then SSO (single sign on) is the way to go as @Ridcully says - it's managed for you by the application server, and your apps shouldn't need to worry about it.

If the problem is more general - how to share data between webapps - then a very clean approach is to use JNDI. Tomcat (and any other servlet/J2EE server) provide a lookup space that can be common to all webapps, this mechanism is most often used to define database (or other resource) configuration outside apps in a way that can be shared.

So, you could write a class to contain the data, have it in JNDI, and have each application look it up (using explicit JNDI calls or resource injection). If you need more info on this let me know.


Single sign on indeed solves the shared-logged-in-user issue.

It does however not allow for sharing the same session among all deployed webapplications. If that is after all your actual intent, then you need to set emptySessionPath attribute of the <Connector> element in /conf/server.xml to true.

<Connector ... emptySessionPath="true">

See also Tomcat 6.0 HTTP Connector documentation.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜