Separating frontend and Backend Tomcat apps? - java

I currently have a Spring application that can be run or debugged locally via embedded Tomcat (through spring-boot), or packaged into a war file and deployed as an app. It has some RESTful services, which are consumed by a single-page-application.
Right now, all of the static assets, including the front-end javascript, are contained in the /src/main/webapp directory. This allows rapid development of both the back-end (embedded tomcat can restart quickly for back-end changes), AND front-end code (any changes made to html/javascript/css can be picked up with just a refresh while the app is running in embedded mode).
However, for various reasons outside my control, I am going to have to separate the front-end and the back-end into separate projects which will be developed and deployed separately. This presents a problem - how do I get a local development environment setup with the same kind of rapid development of both the back-end and the front-end on the same development machine? The front-end servlet and the back-end servlet can't run on the same port, and if I set them up to use different ports, then the browser won't let the front-end javascript make requests to the back-end servlet without setting up CORS, which I'd like to avoid.
Eventually, the two apps will either be deployed to the same Tomcat instance, or else a router will route traffic to them such that they're still on the same domain, so that they can talk to each other without CORS. But that doesn't help me for local development.

I ended up using nginx: http://nginx.org/en/docs/windows.html
I added the following to my conf/nginx.conf:
location /site1/ {
proxy_pass http://localhost:8081/;
}
location /site2/ {
proxy_pass http://localhost:8080/;
}
And configured my two apps (site1 and site2) to run on ports 8081 and 8080 respectively. This effectively simulates the shared tomcat deployment (one domain/port) while using two running instances of tomcat embedded (on two ports).

"The front-end servlet and the back-end servlet can't run on the same port"
Yes they can, tomcat can run several servlets, indeed it can run several applications on the same instance. Just make sure the apps can be differentiated at the URL level
i.e.
server.com/backend_app/
server.com/frontend_app/
Maybe I'm not understanding your question fully......

Related

Running two web applications on same port?

We have two different Java web apps named foo.war and bar.war and we want to deploy these on Jetty server
Jetty is running on 8080 port on the machine
Is it possible simultaneously run two apps on same port ? If no what are the alternatives to run multiple web apps on same jetty server
Thanks
You can run as many web applications as you want in a servlet container. They just need different context roots.
Let's say you have abc.war and def.war. If you deploy both, by default you can access them through http://localhost:8080/abc and http://localhost:8080/def.
Since this usually looks ugly, it's a good idea to put a reverse proxy in front of your Servlet container. This will allow you to have for example 2 different domains foobar.com and xipbaz.com that will then be routed to different applications, even if they're all on the same server.

What are some techniques for deploying a single page application that depends on a rest api?

I'm building a Single Page Application (using AngularJS) that renders data from a REST API call it makes to a legacy system. This legacy system is very large, written in Java and takes minutes to deploy so we decided it would be more productive to develop the Single Page Application completely separate from the legacy system.
The problems occurred once we tried to communicate with the legacy system's REST API. Although both apps were deployed locally to the same host, they were deployed on different app servers so I needed to use different ports when communicating. But because the SPA was communicating to a REST API on a different port, the browsers prevented my requests to protect against Cross Site Scripting attacks.
We found this build tool name lineman (that leverages grunt) that made it easy to proxy http requests. This got us around the cross site scripting limitation but this was only suitable in development mode.
Now that we've got a proof of concept working, we want to know how we're supposed to deploy these apps together without the proxying. It's hard for me to find advice on how to do this because Angular doesn't assume you have a backend in the first place and most people that use Angular on the front end aren't using Java in the backend (if that even matters).
We're running into issues like, the context paths of the apps change depending on if they're deployed in prod mode vs dev mode so we've gotta think of clever ways to avoid broken links that work for both modes. I wonder if/where I took a wrong step here along the way. Should I avoid developing the SPA on a separate server from the backend?
We had the same issues. The URL situation is that you will have different URL paths to your java REST API because you are in different environments.
In order to make these paths cascade down to the angular application, we had to first externalize the base paths in the web app (the app that spawns Angular) to use values that are set during deployment depending on where it is deployed to. We have values in our app servers that link to XML values in config files that we then reference in the application.
Then we create a call from the Angular app to the webapp that spawns it (not the java REST API) that will return the URL that is correct for the environment.
In the angular application, we can then setup the resuorce with the correct base path (the rest of the URL should stay the same from environment to environment).
If you can get the first part working correctly with externalizing the environmental settings, the rest is not difficult.
I would put apache in front and use mod_proxy as a reverse proxy to the apps.
Say your REST API is at http ://localhost:9000. If the angular app is only static assets you can deploy it directly under apache. If not you reverse proxy it as well.
For the REST api yoivsetup a reverse proxy for say /api to localhost:9000. So any request hitting the apache at http://some.host.name/api will now be forwarded to the legacy system. Now fix the angular app and you are done.
For local development you can use node-http-proxy which is ease to setup in a similar fashion

Web Server for existing Java application

I have an existing Java application running on a linux box that monitors the state of relays and network information. This is a stand-alone device currently.
What I would like to further develop is a web server for the device to remotely monitor, configure, and control the device via an Ethernet connection. To achieve this, I need some way to interface between the web server, which could be configured as enabled/disabled, and the master device, which is always running.
Is there a good way to do this? I have looked at apache w/ tomcat and similar web servers but not sure if this is what I need. The key here is that the web server needs to be able to access the existing java application without interfering with its always running services.
You either develop a webapp, use your Java application's API inside the webapp, and deploy this webapp inside a web container. Or you can do the reverse and embed a web server inside your application (see here for documentation to embed Jetty).
If you want to keep the webapp and the original application in two separate JVMs, you'll need some wey to communicate between both, like sockets, RMI, or even files, but it will be more complex.
You might want to take a look at JMX http://docs.oracle.com/javase/tutorial/jmx/overview/index.html

Running a java application on a remote server

I want to run a standalone java application on a remote server. It would not be accessible to clients, but would do background calculations and interact with a database and Secure Socket connection to a third party site. It would also interact with a php site.
Do I have to deploy this with JSP, or can I write a standalone application? If so, how would I deploy a standalone java application (jar file) on a remote server? I understand that I must have them install a jvm on the server (not a problem) but then how would I deploy it (if possible). Would I start it with a command line?
I know I have much to learn, but I am not sure how I would access the command line on a remote server. Through the cPanel?
Thanks.
First of all, you'll want to set up some firewall rules to allow access to that server. I'm hoping that you don't expose that server naked to the Internet.
If all you need is database access exposed on the Internet, I don't see why it can't be a secured web app deployed on a servlet/JSP engine and accessed via a web server. You can leverage basic auth for security, JDBC access to the database from the server, and servlets as controllers to accept requests in a nice REST API.
It'll save you the complications of sockets and inventing your own protocol (use HTTP), starting and stopping the application (now it's just a web server/servlet engine), and deployment (send a WAR file).
Does it really must be a 'standalone' application? I think that in your case the best match would be to use Spring container to load your application within some server (tomcat?) and expose services via standard controllers - with Spring you only have to add some annotations on services methods actually.
Then, your php site can interact with these controllers using for example ajax requests.
If your application is written already, you can easily transform it to run within Spring container. It's very non-invasive and promotes usage of POJOs.

Which application server should i choose for my project?

I am currently developing an application for some researchers in my university.It's a small java program that you can use by command line. The next step is to package that program and deploy it to an application server. Some clients program will submit requests to the server who will call the tool that I wrote. Lately, we will add more tools to the server and he has to dispatch the requests to the right tool.
Which application server fits my needs ? I have looked for Tomcat, Jetty and Glassfish but it seems that they are only used for web application.
Is it possible to use those servers in some context different from web context? Which package archive should i use (jar, war) ?
Any advice?
Some clients program will submit requests to the server who will call the tool that I wrote.
The big question is what server-side technology and what communication protocol can you use between the clients and the server. You basically have two major options: HTTP and web services (in that case, consider using either JAX-WS or JAX-RS) or RMI-IIOP and EJBs (in that case, you'll have to use a Java EE compliant server like GlassFish).
I have looked for Tomcat, Jetty and Glassfish but it seems that they are only used for web application.
Not really. As I said, they can also be used for web services oriented applications. And GlassFish can be used for EJBs applications.
Which package archive should i use (jar, war)
The packaging will depend on the type of application you'll write, it's not something that you choose upfront, it's just a consequence. EJBs are packaged in an EJB JAR and typically deployed inside an EAR; Servlet based web services are deployed inside a WAR.
You really need to think about what technology to use first (with the current level of detail, I can't provide more guidance).
Do you even need an application server? There's nothing stopping you from adding the necessary network bindings and deploying it on its own.
Of the servers you mention, you've got 2 different categories: servlet containers and full-stack Java EE servers
Tomcat and Jetty are servlet containers. That doesn't mean that you can only do web stuff with them and you could manually add the required libraries to get a full Java EE server.
Glassfish is a full-stack Java EE server and can be compared with JBoss (both are open source) or the commercial rivals Weblogic and Websphere.
Sometimes this question is simple as the environment you are working in mandates a particular flavour of app server. You should check this first.
If you're not forced to use an app server, I'd ask why you think you need to use an app server?
I don't see why you would want to use tomcat, glassfish or jetty for a command line program.
If it's command-line based, and you want it to run server-side, you could write a little program that allows users to, for instance, telnet to your server, which in turn starts the CLI-application in question, and relays input / output to the client.
You may also want to look into Java Webstart, which makes deployment of new versions a breeze.
Actually we can't answer with so few elements.
- What are you planning to do
- With what technologies
- Where are you planning to host your application (have you got budget?)
- In which language are written the clients (even the future ones)?
- Could clients be on mobile phones (add some technlogy constraints...)
....
It would also be great to know what kind of request the clients will do, and what kind of response the server will provide...
Actually with what you tell us, all those application servers can do what you want...
I have looked for Tomcat, Jetty and
Glassfish but it seems that they are
only used for web application
You could even make a webapplication (servlet) and on the clientside use a httpclient to call that servlet... there are so many options :)
vive Paris!

Categories