问题
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