Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8667929
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T18:05:13+00:00 2026-06-12T18:05:13+00:00

I want to use HttpSession in Spring 3 MVC. I have searched all the

  • 0

I want to use HttpSession in Spring 3 MVC. I have searched all the web and got this solution at http://forum.springsource.org/showthread.php?98850-Adding-to-stuff-to-the-session-while-using-ResponseBody

Basically, my application auto authenticates user by getting winId and authorizes through LDAP (it’s an intranet site).

Here is the flow of the application:

  1. User enters Application URL (http://localhost:8082/eIA_Mock_5) it has a welcome page (index.jsp)
  2. index.jsp gets winId through jQuery and hits login.html (through AJAX) and passes windowsId
  3. login.html (Controller) authenticates through LDAP and gives back ‘Valid’ String as a response
  4. JavaScript, upon getting the correct response, redirects/loads welcome page i.e. goes to localhost:8082/eIA_Mock_5/welcome.html

Now, I have filter associated with it, which checks if the session is valid for each incoming request. Now the problem is even though I set data on to HttpSession, yet the filter or any other controller fails to get the data through session as a result it doesn’t proceeds further.

Here is the code. Could you suggest what is wrong actually?

Home_Controller.java:

@Controller
public class Home_Controller {

    public static Log logger = LogFactory.getLog(Home_Controller.class);

    @RequestMapping(value = {"/welcome"})
    public ModelAndView loadWelcomePage(HttpServletRequest request, HttpServletResponse response)
    {
        ModelAndView mdv = new ModelAndView();
        try {
            /*HttpSession session = request.getSession();
            UserMasterBean userBean = (UserMasterBean)session.getAttribute("userBean");
            String userName = userBean.getWindowsId();
            if(userName == null || userName.equalsIgnoreCase(""))
            {   
                mdv.setViewName("homePage");

                System.out.println("Unable to authenticate user ");
                logger.debug("Unable to authenticate user ");
            }
            else
            {
                System.out.println("Welcome User "+userName);
                logger.debug("Welcome User "+userName);
                */
                mdv.setViewName("homePage");
            /*}*/

        }
        catch (Exception e){
            logger.debug("inside authenticateUser ",e);
            e.printStackTrace();
        }
        return mdv;
    }

    @RequestMapping(value = "/login", method = RequestMethod.GET)
    public @ResponseBody String authenticateUser(@RequestParam String userName, HttpSession session)
    {
        logger.debug("inside authenticateUser");

        String returnResponse = new String();
        try {
            logger.debug("userName for Authentication " + userName);
            System.out.println("userName for Authentication " + userName);

            //HttpSession session  =  request.getSession();

            if (userName == null || userName.trim().equalsIgnoreCase(""))
                returnResponse = "Invalid";
            else
            {
                System.out.println("uname " + userName);

                String ldapResponse = LDAPConnectUtil.isValidActiveDirectoryUser(userName, "");

                if (ldapResponse.equalsIgnoreCase("true"))
                {   
                    returnResponse="Valid";

                    System.out.println(userName + " Authenticated");
                    logger.debug(userName + " Authenticated");

                    UserMasterBean userBean = new UserMasterBean();
                    userBean.setWindowsId(userName);

                    //if(session.getAttribute("userBean")==null)
                    session.setAttribute("userBean", userBean);
                }
                else
                {   
                    returnResponse = "Invalid";

                    //session.setAttribute("userBean", null);

                    System.out.println("Unable to Authenticate the user through Ldap");
                    logger.debug("Unable to Authenticate the user through Ldap");
                }

                System.out.println("ldapResponse " + ldapResponse);
                logger.debug("ldapResponse " + ldapResponse);

                System.out.println("returnResponse " + returnResponse);
            }

            UserMasterBean u = (UserMasterBean)session.getAttribute("userBean");
            System.out.println("winId " + u.getWindowsId());
        }
        catch(Exception e){
            e.printStackTrace();
            logger.debug("Exception in authenticateUser ", e);
        }
        return returnResponse;
    }
}

Filter:

public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
{
    System.out.println("in PageFilter");
    boolean flag = false;
    HttpServletRequest objHttpServletRequest = (HttpServletRequest)request;
    HttpServletResponse objHttpServletResponse = (HttpServletResponse)response;
    HttpSession session = objHttpServletRequest.getSession();
    String contextPath = objHttpServletRequest.getContextPath();

    String servletPath = objHttpServletRequest.getSession().getServletContext().getRealPath(objHttpServletRequest.getServletPath());

    logger.debug("contextPath :" + contextPath);
    logger.debug("servletPath :" + servletPath);
    System.out.println("in PageFilter, contextPath :" + contextPath);
    System.out.println("in PageFilter, servletPath :" + servletPath);

    if (servletPath.endsWith("\\") || servletPath.endsWith("/") || 
        servletPath.indexOf("css") > 0 || servletPath.indexOf("jsp") > 0 || 
        servletPath.indexOf("images") > 0 || servletPath.indexOf("js") > 0 || 
        servletPath.endsWith("index.jsp") || servletPath.indexOf("xls") > 0 || 
        servletPath.indexOf("ini") > 0 || servletPath.indexOf("login.html") > 0 || 
        /*servletPath.endsWith("welcome.html") ||*/ servletPath.endsWith("logout.do") )
    {
        System.out.println("User is trying to access allowed pages like Login.jsp, errorPage.jsp, js, images, css");
        logger.debug("User is trying to access allowed pages like Login.jsp, errorPage.jsp, js, images, css");
        flag = true;
    }

    if (flag == false)
    {
        System.out.println("flag = false");

        if (session.getAttribute("userBean") == null)
            System.out.println("yes session.userbean is null");

        if ((session != null) && (session.getAttribute("userBean") != null))
        {
            System.out.println("session!=null && session.getAttribute(userId)!=null");

            logger.debug("IF Part");

            UserMasterBean userBean = (UserMasterBean)session.getAttribute("userBean");
            String windowsId = userBean.getWindowsId();

            logger.debug("User Id " + windowsId + " allowed access");
            System.out.println("User Id " + windowsId + " allowed access");
            flag = true;
        }
        else
        {
            System.out.println("else .....session!=null && session.getAttribute(userId)!=null");
            logger.debug("Else Part");
            flag = false;
        }
    }

    if (flag == true) {
        try {
            System.out.println("before chain.doFilter(request, response)");
            chain.doFilter(request, response);
        } catch (Exception e) {
            e.printStackTrace();
            try {
                objHttpServletResponse.sendRedirect(contextPath + "/logout.do");
            } catch (Exception ex) {
                ex.printStackTrace();
            }
        }
    }
    else
    {
        try {
            System.out.println("before sendRedirect");
            objHttpServletResponse.sendRedirect(contextPath + "/jsp/errorPage.jsp");
        } catch (Exception ex) {
            ex.printStackTrace();
        }

    }

    System.out.println("end of PageFilter");
}

index.jsp:

<script type="text/javascript">
    //alert("inside s13");
    var WinNetwork = new ActiveXObject("WScript.Network");
    var userName = WinNetwork.UserName;
    alert(userName);

    $.ajax({  
        url: "login.html",  
        data: "userName="+userName,
        success: function(result) {
            alert("result == " + result);
            if (result == "Valid")
                window.location = "http://10.160.118.200:8082/eIA_Mock_5/welcome.html";
        }
    });
</script>

web.xml has a filter entry with URL pattern as *
I am using Spring 3 MVC.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T18:05:14+00:00Added an answer on June 12, 2026 at 6:05 pm

    I think problem in ajax call and setting windows.location after that.

    Make sure you set cookie enabled. If you don’t do this, your ajax request will lead to new session every time.

    When you do window.location = url and this url differ than your current url, it also lead to new session, because cookie is domain related, and you changed domain, for example from localhost to 10.160.118.200.

    For each request output sessionid and compare it with previous request. It helps find when session was recreated.

    Also this answer can help.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i want use some data from a website with web service. i have a
We have a powerbuilder application and we want use a scanner through this application
I want use this 1 for using Bar code or QR code scanner. I
I want use a single php file to handle all of my voting requests.
I want use page that has this form: <form method=post action=modules.php?name=search> <input onkeypress=FKeyPress(this); onkeydown=FKeyDown(this);
I have a Spring MVC application. It uses its own custom Login page .
I currently have this running in my app. The use of this is for
I have a web application written in JSF2 that uses Spring. I need to
I want use Twisted in Python, but when I installing ,in comes this error,
I want use AES Algorithm. I use this function on it void SubBytes(char *SArr[4][4]){

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.