简体   繁体   中英

How To Configure Tomcat That Java Jersey Runs On Different Port As Web Application

I am developing a Java Server Faces dynamic web application. I have some robots that communicate with this web application over Jersey's RETSful Web Service. So far so good, everything works fine.

But what I want is to give the web application and the web service different ports. I can't help myself to manage the configurations in the Tomcat's server.xml.

Does somebody know how to configure Connectors, Engine or Context in that context? Remark: The Web Services work properly. I just want the service to run on another port than the rest of the application.

Example:

http://localhost:8080/mywebapp/rest/helloworld should have the port 4444 . http://localhost:8080/mywebapp/faces/index.xhtml should keep port 8080 .

If you have question do not hesitate to comment, I will reply.


UPDATE

I have one single WAR-file. In this WAR-file I have two different servlets (JSF and Jersey). These two servlets should run on two different ports. I want a Connector only to Jersey's servlet and a Connector to the JSF Servlet.

Why do I want this configuration?

I do not want that anybody has access to my Jersey-Servlet. The transmitted XML does not contain super secret information, but nobody needs to see it. It is only used to communicate with a bot.

The user should consult the website on which the transmitted are illustrated. The bots do not need access to this part of the webapp (servlet).

This security issue has to be solved.

Add this to your server.xml in the service clause:

<Connector port="8080" protocol="HTTP/1.1"/>
<Connector port="4444" protocol="HTTP/1.1"/>

You probably have the 8080 port defined allready, so that should probably not be added.

In web.xml in WEB-INF of your application where you define your REST implementation you probably have something like:

<servlet-mapping>
    <servlet-name>ServletAdaptor</servlet-name>
    <url-pattern>/rest/*</url-pattern>
</servlet-mapping>

Which defines that url's containing "rest" should go to your webservice implementation, and all other url's should return your HTML file.

Hope this helps.

Too long for a comment, so I am adding another answer:

OK, so as I understand it, what you want to do is to block access to your rest services (mywebapp/rest) if the client is using port 8080 to access the application. And you want to do this using the configuration files available in a tomcat server. AFAIK this is not possible since the only configuration clause (connector in server.xml) that knows about the port has no knowledge of the part of the URL (/rest/) which identifies the part of the application that you want to block. And the application configuration files (web.xml and others) that knows about the URL does not know about the port! But there are a some other options for doing this. To describe a few: Add an apache webserver as a proxy for your tomcat. This is standard practice because it has security advantages, but I can understand if you think this solution is too much work. Another possibility is to split your JSF and REST code into two wars deployed separately. Next you define two services in your server.xml and put in a connector for the respective ports as described earlier. In the appBase parameter of the host clause you can then specify where your rest code and jsf code is placed.This solution of course is reminiscent of the solution described by xerx593, but there is that much work doing in this, I tried it took me about 10 mins. Of course it maybe difficult to split up the application, if the two modules has common code, but you can always deploy the common code twice. You could probably block the requests in your code too, if you check port or client address of the request. Spring would probably be helpful with this.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM