Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

authentification in Struts

Status
Not open for further replies.

TexTwil

Programmer
Mar 2, 2004
4
FR
Hi,
I'm quite a newbie in Struts and I have a question:
I have a login.jsp page with LoginForm and LoginAction classe associated with it. The user enters his login and password and if they are matching, the "user" bean is stocked to the session and the user is redirected to the main.jsp page.

My question is what is the best way to check in the main.jsp file that the visitor of this page logged in ?
I know I could write somethig like:

<%
if(session.getAttribute("user")==null){
response.sendRedirect("login.jsp");
}
%>

But I am looking for a "struts solution" : i want to have as little as possible code in my jsp pages.



Thanks for your help.

Tex.
 
Hi,

In the action mapping you can make use set-property. And in the Action page you can check for the attribute. if null forward to login else continue with the action. Its always good to use global-forwards

<global-forwards>
<forward name="login" path="/login.jsp"/>
</global-forwards>

<action path="/Test type="DummyTranAction">
<set-property property="loginRequired" value="true"/>
<forward name="success" path="/nextPage.jsp"/>
<forward name="failure" path="/previousPage.jsp"/>
</action>

In the DummyTranAction Action calss check for the property loginRequired if the value is true and check the session attribute("user") if it is null set the forward string to login else conitinue what the page has do.

Hope this will help you..

Cheers
Venu

 
i don't think it answers my question. I have this in my struts-config.xml:

<action-mappings>
<action name="loginForm" type="fr.gouv.sante.dhos.web.LoginAction" validate="true" input="/login.jsp" scope="session" path="/loginAction">
<forward name="user" path="/user_index.jsp" />
<forward name="error" path="/error.jsp" />
<forward name="admin" path="/admin_index.jsp" />
</action>
</action-mappings>


This means if the you log in as a "user" you are redirected to the user index page and if you log in as an admin you go to the admin index page. This works!
But i want to know how can i check in both user_index.jsp and admin_index.jsp pages that you are loged in.
In other words, if i tape the URL (or .../admin_index.jsp) directly in my browser (without logging in) i want to be redirected to the login page. What am I supposed to write in these pages to do this redirection ?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top