Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

java

jsp

servlets

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.println("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

like image 279
ace Avatar asked Mar 02 '11 10:03

ace


1 Answers

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.

like image 142
Ridcully Avatar answered Sep 28 '22 15:09

Ridcully