Programmatically create subdomains with JBOSS and java

拥有回忆 提交于 2019-12-07 15:32:44

问题


Right now I am developing an application on JBOSS 7.1 using JSF, SEAM and Primefaces. The application is providing a user registration. What I need is when the user registers an account for the nickname for example "andrew", his profile will be publicly accessed as andrew.mysite.com.

How can I implement this programmatically.

Thanks in advance,

Ilya Sidorovich


回答1:


This is simply a process of mapping your subdomain to URL's that can be accessed by the appserver and use something like REST to map the URL to request parameters.

In your example, you will probably need a webserver like Apache web server to handle the incoming requests that can do some URL rewriting. Something like this

user.mysite.com --> www.mysite.com/user

In Apache this can be achieved by creating a virtualhost and using RewriteCond and RewriteRule. Here is an example

RewriteCond %{HTTP_HOST} ^([^.]+)\.mysite\.com$
RewriteRule ^/(.*)$           http://www.mysite.com/%1/$1 [L,R]

You can then forward your requests from the webserver to your application server. If using Apache this can be done using mod_jk, mod_proxy or mod_cluster.

Once you have that, you can create a RESTFul service (jboss supports REST) that can map the URL to your application code. Here is an example

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.core.Response;

@Path("/")
public class UserService {

    @GET
    @Path("/{param}")
    public Response printMessage(@PathParam("param") String user) {

        String result = "User : " + user;
        return Response.status(200).entity(result).build();

    }

}


来源:https://stackoverflow.com/questions/9862584/programmatically-create-subdomains-with-jboss-and-java

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!