开发者

How to create custom URL http://mysite.com/username in Glassfish?

开发者 https://www.devze.com 2022-12-19 21:22 出处:网络
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 cu

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


You can use PrettyFaces for URL rewriting in GlassFish: http://ocpsoft.com/prettyfaces/


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>


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

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号