user965369
user965369

Reputation: 5743

java - Calling specific servlet based on url request?

I'm pretty new to JSP, Servlet and Tomcat. If I point multiple domains to the IP adress of a server, is there a way I can call the relevant Servlet programmically, based on which domain has been requested?

Maybe there something I can do in web.xml?

Sorry for my lack of knowledge - I'm just getting started:(

Upvotes: 0

Views: 767

Answers (4)

PeakGen
PeakGen

Reputation: 23035

Redirect the request to the correct servlet using "RequestDispatcher"

Upvotes: 1

Rob Tanzola
Rob Tanzola

Reputation: 1795

If you are looking to have the same web application respond to multiple domains, you might look at having a dispatcher servlet or dispatcher filter. Frameworks like Struts 2 and Spring MVC use these concepts to route requests to the appropriate servlet. With a dispatcher servlet, you can use whatever conditions you want (in your case, hostname) to route to the approriate servlet.

If you are instead looking to have separate web applications respond to the different hostnames and/or IP addresses (commonly referred to as virtual hosting), then you might want to look at Tomcat virtual hosting. This is also commonly handled by putting a web server like Apache or IIS in front of Tomcat.

Upvotes: 1

Mark Thomas
Mark Thomas

Reputation: 16660

The HTTP host header will tell you which domain the client requested.

The way to obtain this via the Servlet API is:

javax.servlet.http.HttpServletRequest.getHeader("host");

Upvotes: 2

Zaz Gmy
Zaz Gmy

Reputation: 4356

use something like:

public void doGet(HttpServletRequest req, HttpServletResponse resp)
 throws IOException {

        // Get client's IP address
        String ipAddress = req.getRemoteAddr(); // ip 

        // Get client's hostname
        String hostname = req.getRemoteHost(); // hostname
    } 

Upvotes: -2

Related Questions