Session handling in Struts 2.1.6

Posted on 2009-04-13
Last Modified: 2013-11-24
Hello folks,

I have a project with the following setup:

Tomcat 6.x
Struts 2.1.6
DisplayTag 1.2
Spring 2.x (1 or 5, don't remember now)

I want to know to to do session controlling in every action of my app, like if the users weren't logged in, they're redirect to certain page to login (in the case of my project, either the user come to a special crafted url like login/SPECIALHASHTOLOGIN or won't enter at all.

Need more details?

Thx in advance.
Question by:dgomesbr
  • 2

Accepted Solution

dgomesbr earned 0 total points
ID: 24129721
I'll need modify the default interceptor stack (or create a custom stack) and add a custom interceptor. This custom interceptor will need to implement SessionAware to access the user session, and must implement your custom logic (which action to redirect to, which URLs do not need protection, etc.).

Here is a good tutorial of a LoginInterceptor that behaves similar to what you are requesting: 

Author Comment

ID: 24130488
Some samples of struts.xml and applicationContext.xml

<?xml version="1.0" encoding="UTF-8" ?>


    "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"



    <constant name="struts.objectFactory" value="spring" />

    <constant name="struts.devMode" value="true" />

	<constant name="struts.enable.SlashesInActionNames" value="true" />


    <package name="renaconPackage" extends="struts-default">


	 		<interceptor name="login" class="loginInterceptor"/>


			<!-- stack seguro -->	  	    

			<interceptor-stack name="defaultLoginStack">

				<interceptor-ref name="params" />

				<interceptor-ref name="login" />

				<interceptor-ref name="prepare" />

				<interceptor-ref name="chain" />

				<interceptor-ref name="modelDriven" />

				<interceptor-ref name="fileUpload" />

				<interceptor-ref name="staticParams" />

				<interceptor-ref name="params" />

				<interceptor-ref name="conversionError" />

				<interceptor-ref name="validation" />

				<interceptor-ref name="workflow" />





	  	<!-- mudando o default stack para nosso stack seguro -->

	  	<default-interceptor-ref name="defaultLoginStack" />





			** Action handler generico									*


			*	eg: usuario/list										* 

			*			-> UsuarioAction.list();						*

			*			-> /pages/usuario/list.jsp						*



		<action name="*/*" class="{1}Action" method="{2}">

			<interceptor-ref name="defaultLoginStack" />		





ApplicationContext.xml (only the needed part):

	<!-- servicos -->

 	<bean id="usuarioService" class="" />


    <!-- actions que necessitam de servicos injetados -->

    <bean id="usuarioAction" scope="prototype" class="">

        <constructor-arg ref="usuarioService" />


    <bean id="loginInterceptor" scope="prototype" class="">

        <constructor-arg ref="usuarioService" />


Open in new window


Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
hasOne  challenge 59 126
groupSum6 challenge 6 77
firstswap challenge 20 68
eclipse package explorer vs project explorer view 2 84
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
The viewer will learn how to implement Singleton Design Pattern in Java.
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

895 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now