views:

360

answers:

1

Hi,

The registered accounts in my web application (Created using struts2) gets a separate site like http://localhost/accountname

And each account has its own login page. After registering, the users will get a separate site http://localhost/accountname

I want to scan the request URL (in struts2) first and then forward that request to the corresponding login page.

how can i do this? please help

A: 

You can get the request object in your action method by:

   HttpServletRequest request = ServletActionContext.getRequest();

You can then find the request URL like this:

   String spath = request.getServletPath();

Then you can parse it and look for the pattern that you want and forward accordingly.


UPDATE:

You can use a package in the struts.xml configuration file. Lets say "userapps".

     <package name="userapps" extends="default" namespace="/userapps">
           <action name="*" class="path.to.your.ActionClass" method="processUrl">
               <result name="success" type="redirectAction">
                   <param name="actionName">userpage</param>
                   <param name="id">${user.id}</param>
               </result>
           </action>
     </package>

In the ActionClass's processURL method you can pull out the part of the URL that you are interested in and set a property lets say the user and his id. You then return success from your action.

You will have a second action called userpage say, that will take the user's id and forward to the correct page.

Now, any url of the form localhost/myapp/userapps/anything.action will call the processURL method.

Vincent Ramdhanie
but in struts2, if i say http://localhost/myapp/accountname accountname action does not exist at all. accountname is different for different users. How can I avoid 404 error for this thing if i follow your code
lakshmanan
can i set this to namespace / and have this functionality for all reqquests..?
lakshmanan
lets say i set my app as default context and http://localhost will get to my java web app. then user john registers with the service get a URL like http://localhost/john. The first time he comes to his site htt://localhost/john, he sees his login page. After he logins, lets say he has a tasks page in his account that has url http://localhost/john/tasks . Can i apply your functionality to tasks page request as well. How will the code change in that case ?
lakshmanan
@lakshman Yes. You can apply this to what you are describing. You can create action elements for tasks and others in the default package, then last create an action that maps to *. This will be the catch all which will handle all request such as localhost/john.
Vincent Ramdhanie