views:

166

answers:

2

I am designing a small J2EE web application (for example, the service name would be like http://www.mysite.com). It uses Glassfish. Spec: When a user sign up into the web application, he will get a custom URL like

http://mysite.com/username

instead of

http://mysite.com?username=username&userId=xxxx

things i know is,this a part of is a part of directory-level configuration,and in Appache this can be done by configuring .htaccess.

How can i accomplish this in my web application. I am still developing the app. i have not hosted it yet.

thanks

+1  A: 

Typically, when you develop a web application when you install it in a servlet container (in your case glassfish) you define the application root that maps URL's to your web-app.

If you use /myapp as application root, the container will map requests to http://mysite.com/myapp/* to your web-app. Servlets in the web-app are mapped in the web.xml in which you map servlet class.

If you say, map the servlet com.mysite.UserServlet to user the container will map all URL's of the format http://mysite.com/myapp/user* to that servlet. You can use the pathinfo to retrieve the part after /myapp/user and parse it to extract the username in case you chose to use URL's like http://mysite.com/myapp/user/Sam instead of http://mysite.com/myapp/user?name=Sam

Edit

The method HttpServletRequest.getPathInfo() (quote) returns any extra path information associated with the URL the client sent when it made this request. The extra path information follows the servlet path but precedes the query string. This method returns null if there was no extra path information.

So for http://mysite.com/myapp/user/Sam and a servlet mapped to /user/ getPathInfo() would return Sam, which you can then use just like you would if you got the value as attribute.

For this, your web.xml would contain a mapping like:

<web-app>
    <servlet>
        <servlet-name>userservlet</servlet-name>
        <servlet-class>com.mysite.UserServlet</servlet-class>
    </servlet>

    <servlet-mapping>
        <servlet-name>userservlet</servlet-name>
        <url-pattern>/user/</url-pattern>
    </servlet-mapping>
</web-app>
rsp
can u elaborate last para once more please.
Sam Rudolph
A: 

Sounds like your login page should use POST instead of GET in the submit action.

http://www.w3.org/TR/html401/interact/forms.html#submit-format

Thorbjørn Ravn Andersen
sorry but unable to get any relevant info of the link..
Sam Rudolph
I also don't see how this relates to the question.
Stephen C
@Stephen, do you agree with my analysis? (aside from the link)
Thorbjørn Ravn Andersen
@Thorbjørn Ravn Andersen :ya i agree with u
Sam Rudolph
It is true that a login action should use a POST rather than a GET, but the OP is not talking about a login action. He is talking about how to implement a fixed URL that denotes the user's personalized page. If login is required, that would be (for example) triggered by visiting that URL.
Stephen C