tags:

views:

202

answers:

5

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

http://username.mysite.com

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

thanks

+2  A: 

if you are on windows, edit the \windows\system32\drivers\etc\hosts file and add a line like this:

127.0.0.1  username.mysite.com

then when the server is running, you could open the browser and type:

http://username.mysite.com
http://username.mysite.com:8080

or whatever and see the website.

you will need admistrator privileges for this.

if you are on linux, you will need to edit /etc/hosts, and do the same thing.

however, when you are deploying, it is more about configuring the webserver and the dns server. so if you want to get the full experience, you should install a dns server locally and use that as your networks nameserver. this is a more complicated task, and the instructions depend on whether you are on linux or windows, and which webserver you are using.

then you have to write a bit of code in your application to check the server variables to find out which subdomain was used and do the processing based on that.

kinjal
i want this functionality at application run time for all users.. is this possible ? How can i do it ? The users goes to his domain and sign into his account via that domain ...
lakshmanan
if you can tell me whether you are on windows or linux, i can post specific instructions.
kinjal
@Kinjal:if you both the types, Please do tell us as we are keen to gain knowledge for both the scenario
Sam Rudolph
Im working in Ubuntu linux
lakshmanan
+1  A: 

It looks like you are looking for Virtual Hosts on Tomcat. Then the subdomains need to be configured in Tomcat:

http://tomcat.apache.org/tomcat-6.0-doc/virtual-hosting-howto.html

The web application only controls the URL part starting on the context root. So if there is a web app at "www.mysite.com/", it can handle requests to "www.mysite.com/alice" or "www.mysite.com/bob" but it will not handle requests to "alice.mysite.com/" if this has not been configured in the Tomcat server. This means, that for every new user, Tomcat needs to be configured with the new subdomain. Ii think this can not be done from within the web application.

mjustin
How else this type of work can be done ? Yes for every user, there is a subdomain. like alice.mysite.com and the user will login only via his own domain.
lakshmanan
+2  A: 

You’ll need to create a virtual host for each subdomain in Apache configuration like:

<VirtualHost *:80>
ServerAdmin [email protected]
DocumentRoot /www/subdomain1.mydomain.com
ServerName subdomain1.mydomain.com
ErrorLog logs/subdomain1.mydomain.com-host.example.com-error_log
CustomLog logs/subdomain1.mydomain.com-access_log common
</VirtualHost>

There is one problem though. If your solution requires folders creation, virtual host addition and apache restarts which does not look good to me. How would I deal with this problem is:

  1. Create a wildcard virtual host for “*.mydomain.com” so that all requests to subdomains go to this virtual host.
  2. Setup a rewrite rule for it to redirect all requests to one page, say index.jsp
  3. Write up a JSP page taking the hostname the user came to, searching the redirect URL into a storage by it, i.e. mysql db, and redirecting to it. This script will be the index.jsp script on the wildcard host.

If you use this way, you only need to setup the above once and then add the subdomains in to the storage which appears to be more flexible solution than creating subfolders and modifying Apache configuration.

Sam Rudolph
Is this Apache httpd configuration or Apache Tomcat configuration?
Thorbjørn Ravn Andersen
So I need to do this configuration for "every user" in my site ?
lakshmanan
@lakshmanan: you've originally tagged the question with "apache" instead of "tomcat". The word "apache" is synonymous to "Apache HTTPD Server", not to "Apache Tomcat". You've incorrectly got Apache HTTPD based answers from Sam, kinjal and Vincent. Those does **not** apply on Tomcat. You can however install Apache HTTP Server next to Apache Tomcat and bring it in front of Tomcat as a proxy with help of Tomcat Connector (mod_jk). For real Apache Tomcat answers, check the answers of Thorbjorn and mjustin.
BalusC
A: 

You could create a wildcard virtual host

<VirtualHost *:80>
    ServerName example.com
    ServerAlias *.example.com
</VirtualHost>

and then deal with subdomains inside your application code using something like HttpServletRequest#getRequestURL()

Vincent Robert
Why the downvote? If I understand Sam Rudolph, a wildcard virtual host would be necessary (as a part of the solution)
mjustin
Yes but my proposition is to deal with subdomains in the application itself, not in a rewrite rule.
Vincent Robert
+2  A: 

You have two issues.

First you will need to have the browser of the user recognize where "username.mysite.com" is located. This requires DNS configuration, and usually in the form of a "CNAME wildcard". Note that dyndns.org provides this in their Dynamic DNS Pro package, if you want to have control yourself in the try-out-phase.

The second one is that you will need to tell Tomcat to derive the user from the hostname, and provide a special site for each user. The easiest way to do this, is most likely by having a servlet filter that "tastes" on the hostname in the URL and sets the properties you need for your jsp-pages.

Thorbjørn Ravn Andersen