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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Multi Language - Application.properties 1

Status
Not open for further replies.

cezarn

Programmer
Aug 13, 2003
10
CA
Hello all,
I'm new to Struts and I have like tons of questions. Part of them I found the answer (thanks Google) but not for some of them, mostly "best practices" questions.
So, here it comes the first one:
We use application.properties file for displaying a certain language depending on the user's browser.
However, if I want to do a multilanguage application and I want to let the user decide the language (even if its browser says someting different), how can I do this? Can I create a link where the user can click to change the language?
I mean, it has to exist a possibilty to use those multilanguage .propoerties files, other than just let the browser decide, rigth?

Thanks,
Cezar

 
Supply the buttons for the user. Include the language in the link for each button like this:

<html:link src=&quot;thispage.jsp?lang=ko&quot;>

Try using this method in you Action:

setLocale(request, lang);

The request is your HttpServletRequest and lang is the lang in your URI. You might have to write some code to handle this for each request. You could have a single controller action that all pages must go through. Then, if the user changes the language, use URL rewriting to change all the links in the page to include ?lang=jp or whatever their language is. Then everytime they click on a link, it is rewritten.

Or you could use a cookie with their language specified in it. Either way you will have to route all requests through some Action or Servlet that gets the lang and overrides the default browser setting.
 
Wow, thanks Kindon for your quick reply.

So it does exists a solutions also it seems to me a bit cumbersome. I'll try it this week.

Another question:
- I have an app where user log in, do some stuff (I forward him to jsp pages I customize for him) and then logs out.
If someone tries to access directly the jsp pages of the app, you have to check on EVERY jsp page if the user is logged in (with a session variable) ?
--
<%
if(session.getValue(&quot;loggedIn&quot;) == null)
response.sendRedirect(&quot;logon.jsp&quot;);
%>
--

Is this a good way to do it? Does it exists another one?
Thanks again,
Cezar
 
If possible use container authentication. Then they will not be able to access the jsp unless they have authenticated.

Otherwise, yes, you have to validate something in each page. This construct is not all that great either, but it will work:

<%
if(session.getValue(&quot;loggedIn&quot;) == null)
response.sendRedirect(&quot;logon.jsp&quot;);
%>

If you can't use container authentication (apache, tomcat, etc), route all *.jsp requests through a controller servlet that does the validation for you. Then if somebody tries to go directly to a jsp they are rerouted to the controller. The controller checks for a valid login and then forwards the use accordingly.

Yet another slick way is to check the login status with a taglib. Use the taglib to set the status to a bean of the user valid. You could redirect them to your login.jsp page if it is invalid. Then you can get rid of the scriptlet code.
 
Hi Kindon,
Well, very nice to have the doubts cleared in such a short time :) Your answers are very appreciated, thanks.

When you talk about container authentication you talk about the server.xml and web.xml settings that allow access only on certain conditions? Do you know where I can find some examples?

I didn't play to much with the taglib but I'll give it a try.

As usual, another question :)
- it is normal practice for Struts to have an Action redirect the user to a jsp in which content is displayed using a JavaBean or I'm bypassing something?

Many thanks,
Cezar
 
Yes. Container controlled authentication is set up in the config files of your container. web.xml, server.xml, tomcat-user.xml or whatever is specified by your container.

Whether it is &quot;normal&quot; or not is another question. But it is a good practice. One way to enforce this is to put all your JSPs in something like a /WEB-INF/jsp dir. Then they are accessable to be forewarded to, but they are not accessable directly to the users. If someone tried to access the JSP directly they would either get an error or be redirected to an Action. Don't use any direct links to JSPs and your users will never know the name of the JSP. They will see a lot of addresses like this:

method=post

or

method=get

or even this

method=get

This is a very good practice.
 
Hello Kindon,

It's working very well the method with the jsp under WEB-INF/jsp/, I had just to change the relative links to the actions in the action form field, from SomeAction.do to ../../SomeAction.do. I see <html:base/> tag has a big influence on the links.

>>If someone tried to access the JSP directly they would >>either get an error or be redirected to an Action.

Do you know how can I redirect the one who tries to access directly to login/error page instead of default error page he gets?

Again, thanks

Cezar
 

What was wrong?
I could not access the forum for 2 days :-((

Cezar
 
After reading the way I'm checking now to see the user is still logged in, in every Action I check the session variable I set on login, if it's not there I redirect the user to login page. I no longer need the:
<%
if(session.getValue(&quot;loggedIn&quot;) == null)
response.sendRedirect(&quot;logon.jsp&quot;);
%>
in every jsp page.
The only problem, besides finding the solution a bit long to do (you have to check the session's variable in very Action) is that for the page right after login (a menu with links to possible actions) I can't do the verification. If I do a refresh the page still displays. The links, if you try them, are not valid which means the logoff is correct.

The structure of the application:
- logon.jsp
- menu.jsp by LogonAction (if logon successful)
- jsp1 by Action1 (if UserLoggedIn using a session var)
- jsp2 by Action2 (if UserLoggedIn using a session var)
- etc
- logoff

I want to try the taglib version but I'm not sure (yet) how to use it. It is the <bean:parameter /> I should use to check the login?

Thanks,
Cezar
 
Wow,

I just noticed, after I logoff, if I go Back to menu and I do a refresh a new session gets created and you can use the links to navigate.

What am I doing wrong ?? How can one do not to create another session when users hits refresh on the page right after login?

Cezar
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top