Java web application on apache tomcat - java

I am developing a web application in java using the Vaadin framework.
I am running that application on Apache Tomcat. What I want to ask is that, if I run that application on Apache Tomcat and access the same application using two different browsers on two different computers, then does the application then have two instances on Tomcat or does it have a single instance?? I have searched for it, but not been able to find a satisfactory answer.
Thanks !

If you run a web application in Tomcat, you'll have exactly one Tomcat instance, which will host your application. This single Tomcat instance (and therefore your web application) will be able to many different browser requests, from many different computers. This is exactly what web servers are designed to do: process requests from many different clients.

One instance of Tomcat, and many instances of your Vaadin app.
To access your Vaadin app, the user points their web browser to your Vaadin app's URL. Tomcat must already be running in order to accept the request from the web browser. When the already-running instance of Tomcat receives that request, Tomcat starts a new thread. In that thread a new instance of your subclass of the Vaadin "Application" class will be created and run.
So, if you have 5 simultaneous users running your Vaadin app, you will have one instance of Tomcat running. And that Tomcat instance will be running 5 sessions, each in its own thread running its own instance of your Vaadin "Application" subclass.
Anything marked "static" in your app applies to all 5 instances of your app, while anything not marked "static" applies only to a single instance of your app (a single user).

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.

Can I launch a jetty server for my gwt from my main method?

I have two applications.
One is a background application, which regularly checking some data in my database.
The other is a GWT application, which provide a simple UI to client to access a simple table.
My question is can I combine these two applications into one?
I am currently using gwt devmode to launch my GWT application. It is running fine.
But really, what I want are the followings:
I don't want any heavy TomCat or standalone web server.
I wish the two applications to be combined into one.
Either in my background application's main() method, I can launch gwt-dev-mode or jetty server
or I can easily deploy jetty server or just package the whole gwt application and run it with gwt-dev-mode, of course, launching my background application inside the jetty or gwt-dev-mode
can I do those?
We are using the solution 4.
Our GWT app inherits from the project with the back-end logic. When launching dev mode the back-end logic runs as usual.
Do you have to call a main() whenever you want to start the background application ? We are using Spring and the beans managing the background processes are created automatically on start-up.
Your local server could also just call that process on start-up.

In tomcat deploy mulitple apps with some context but make them think as they are root app

I am not sure what I am asking is correct approach or not. So, let me first clarify my requirement.
Till date I was using each app as a root app and was deploying them in separate tomcat instance, but my app number are now growing and I can not keep on deploying a separate instance of tomcat for each app.
There are two things which make me deploy every app as root:
For updating an app you have to shutdown tomcat instance which will also stops other app running in same instance. And I do not want this to happen (as some users are live on one web app or some job is running on some app, so I can not stop all apps at any given time).
For writing urls, if I deploy a web app as root, then in web-app I can simply write "/students/list" , but if I deploy them in same instance with some different context name, then I have to write context name before the urls, like "ctxt1/students/list"
And for developers sake I don't want them to write context name before every url in web-app and also I don't want context names to appear to a user.
Is there any approach with which I can solve these 2 problems. I am thinking of migrating to Glassfish for some apps. If glassfish offer solution to these problems then I can migrate all apps to glassfish as well
Secondly I am running tomcat behind IIS, using ISAPI. If IIS can be of any help here?
You should not use one tomcat per App. The amount of tomcats should increase only when needed (really needed).
It is not true that you have to kill the whole tomcat, for one App update, you should only restart the specific context (going to http://your.tomcat:8080/manager/html).
About the URLs you can use an apache in front with mod_proxy_ajp configured to talk with tomcat (see this: http://httpd.apache.org/docs/2.2/mod/mod_proxy_ajp.html)

Java Application running as web service communicating with Java EE app on Glassfish

I have a stand alone Java application that needs to get information (string data) from a Java EE application, running on a Glassfish 3.1 Application server. I have created a web service for my Java app, but I'm wondering how I could achieve communication with the Java EE glass fish app (using servlet?).
I hope to have a method on my app that can be called from, for example, a client running on glassfish (and vice-versa). This method would have something like a String array as parameter, so that I would be able to pass the data between the apps.
Note: I am unable to deploy my app on Glassfish, since we are trying to achieve separation till we are sure the application I am developing will not cause Glassfish to crash ( we currently have other critical apps running on Glassfish). Also note that this is all taking place on the same machine.
You should develop a web service and deploy it on Glassfish within your existing application. You can do this via a Servlet based web service, or a Session Bean web service, whichever is more appropriate for you.
You will then create a web service client against that web service for your Java app, and integrate it appropriately with calls to the servers via the web service.
Of course, this should all be done against development servers, not your production servers. Glassfish can be deployed pretty much anywhere: your machine, another machine, a VM, in "the cloud". Not having a development server available for, well, development is unacceptable. There is no way you can determine if your app will "crash Glassfish" unless you can test it.
To quote the esteemed Donald Knuth: "I have only proved it correct, not tried it."
Get a test server, develop against it. Move forward.
Have you looked at the URL class.
try this url Java URL example
This may help

Java application inside tomcat?

I have a standalone java application which uses java based TCP NIO to collect some information from various clients (not on web/HTTP but through some indigenously developed middleware). Now I have to develop a front-end for the users to perform a lot of querying through HTTP. So is there a way to put this application inside tomcat, so that servlets can invoke required functions on this application ???
The thing is it has to listen on that middleware outside tomcat too and at the same time service servlets inside tomcat. How to do this ??
Putting this application outside tomcat and using RMI is an option but I don’t want to do that.
Can embedding tomcat inside my application is an option ???
The brand-new Tomcat 7 has Embedded version for download.
Tomcat is big. You should try to embed Jetty, which is meant in order to make it possible.
http://jetty.codehaus.org/jetty/
I don't know about embedding Tomcat, but you can embed a servlet container inside your application using embedded Jetty. If what you want is to add an HTTP interface to an existing server, I think that's the way to go.
It also should be perfectly workable to launch your server's TCP listening components from a standard servlet in Tomcat. Then the servlet can call methods in your existing application directly, while it continues to listen to its usual TCP ports.
A third option is to write a servlet that just talks to your existing server in the same way other clients do.
The way you want to go, is to have an embedded web server to your existing application since you just need a little bit of web functionality.
Going the other way is possible. You can use servlet listeners to get started and stopped, and you need to take great care of any thread you start since Tomcat have no idea they exist.
As Traroth said, embedding Jetty is a much better option than doing the same with Tomcat. You have an example and a small tutorial in the Jetty website:
http://docs.codehaus.org/display/JETTY/Embedding+Jetty
You can always open the ports and listen manually if the requests aren't complicated.
Have you considered going the other way? That is embedding your application inside a web application?
Using spring you can instantiate your web application and then inject in into the appropriate web classes (e.g. struts2 actions).
This way you can deploy your web application into any web server that is preconfigured separate from your application.
If you do have to use Tomcat, you're best just making your application as a .war file which when Tomcat is ran will deploy your code. This can then talk to any RMI/middleware you like.
I'm not sure I understand your comment regarding memory spaces.
At a very basic level you have two choices:
Run your appication and the web
application in the same virtual
machine.
Run your application and
the web application in a different
virtual machine
If you take approach 1, you will be able to get instances of your applications classes from the servlets and call methods on them.
Embed tomcat / jetty inside your application. You will start your application normally through its main method. After starting your app you will need to create instances of the tomcat / jetty classes which will start up their own threads.
Embed your application inside a war file to be deployed on tomcat / jetty. Similar to the previous option instead you will need to start your application from a context listener.
If you prefer approach 2; start tomcat / jetty and deploy your web app and separately deploy your application. You'll now have two jvms running. To communicate between these two processes you'll have to use some form of socket communication: OutputStream/InputStream over sockets; RMI; JMX; JMS etc.

Categories