I just got a servlet class working in Eclipse. I was testing it by deploying it using App Engine, and it would prompt me to run as a Web Application. Now I want to run this code from another class. So I made another class and put "TestServlet ts = new TestServlet();" in the main function. When I run it nothing happens. Do I have to make a call to the TestServlet's "doGet" method, or is it something to do with not running the main class as a web application?
Umm... you should not be doing this. Extract common logic into a separate POJO (plain old java object) class and invoke it from both your servlet and your other class.
Manually instantiating / invoking servlets is NOT a good idea.
You need a servlet container if you want to run it up for real. I would suggest using Winstone http://winstone.sourceforge.net. It's a small, fast, zero-config container for getting your servlet up and running.
Calling doGet (or doPost or any other visible method) in the instance of the servlet object will execute the containing code. However be aware that behaviour may differ from running the servlet in an web container if the code makes use of any of the "wired-in" context variables.
At face value I would suggest you refactor to have the code you require in a common method and call from both your servlet and your main class.
Related
Here's situation. I have one container with php code and php-fpm - this is my application container. Sometimes main application calls java application - jar file. So I decided to split those technologies and make seperate java container with this java application. Now I need a way to call jar file launched inside another container. One way is rebuilding java applicaton to support REST api, but it takes time so is there any other possibility to solve this problem?
You could take a look at Java Remote Method Invocation (Java RMI), but to be honest, I have no idea if it's possible to to this invocation not using java.
https://docs.oracle.com/javase/8/docs/technotes/guides/rmi/hello/hello-world.html
I Have two applications.One is Standalone application.Another one is web Application (Servlet App).
Here I want to Call Java Application From Servlet Application.So can You Guys Suggest Me or Can i have one app for this one.So that i will implement in my application.
In this Application I have created Jar file of Stand alone Application After that What to do ?
First, you should make your command line application classes available in your web applcaction environment. To achieve this you should either:
package all in one jar
put classes of stand alone application under WEB-INF/classes in you war
put jar that contains your stand alone application under WEB-INF/lib in you war
Stand alone application starts from main() method. You can just call this method from your servlet and pass parameters, for example: MyApp.main("hello", "stand alone application").
Since you are the author of both applications and are familiar with the internal design of the stand alone application you could (and probably should) call internal layer directly without using main(). For example if you main method starts with new MyApp().start() perform the same call from your servlet.
Just Switch from GlassFish to Tomcat , And then your problem has been solved.
Is there anyway I can use spring to get rid of the main function in my java project.
It is going to be a standalone program, and I want spring to get rid of the main function, and take care of entry point by itself.
I understand that main function are a must, but I am talking something on the line of a dynamic web project, wherein I do not define the main classes, but just the services etc.
If You want a standalone application, that is impossible. You should rather load context in Your main method, and just run a specific bean's method (treat main method only as an entry point to Your app).
I have a legacy Java servlet that is currently running in a Tomcat container. I want to run it outside the container, as a standalone Java application. The primary intention in doing so is because the new role in which this application will be deployed, involves only computation, and no servicing of requests.
How should I go about modifying the servlet code? Will pulling out the servlet's init() code into the main() method of a new class help?
Extract the functionality you want from the servlet to other classes, so that the servlet is only the web interface to the functionality.
Those other classes should ideally not use anything in javax.servlet.*.
Then create a separate class with a main method that uses those other classes, in an appropriate way.
I would transform the servlet class into a main class, as you mentioned. The main method of the new class will create an instance of main class, execute init method to initialise if (potentially using the arguments received from the command line). Then invoke the service method inside a try/catch/finally block and invoke the destroy method of the servlet inside the finally block. Of course, your new service method will be invoked without parameters and will not contain any references to the servlet api.
yes.
The servlet implements the interface to interact with the Tomcat servlet container. If there are no settings read from the servlet context (path names, configuration paramters) you can extract the logic into a main class.
You will need to make sure you do not rely on the request/response scheme in your services. Usually if only one process runs code is much easier to write than in concurrent scenarios. But one cannot be sure there will be no such effect (for example caches that are now request based will not be emptied in standalone).
also remove any servlet api dependencies in your code. It will not work and is no longer required.
good luck!
You have to do that carefully. It may happen that some filters are doing necessary work not seen within the servlet
Assuming your case is rather simple and a GET or POST just triggers the code, it should be easy to convert into a classical java application.
Just call the former init part and the code from doGet or doPost respectly.
You need to figure out a couple of things first, the functionality in the servlet will probably react to some of the url's parameters.
first figure out what the computational part is and what parameters it expects,
once you understand that, figuring out what to put in your standalone application will be trivial.
another approach is to embed Jetty in your main application and let that run your servlet,
this will leave your servlet code untouched reducing the risk of introducing bugs
*Hello Guys..! *
Normally a java program needs main() function to execute the code otherwise it'll returns an error[java.lang.NoSuchMethodError: main Exception in thread "main"]..! In struts2 we are using action classes & bean classes(java codes) but they don't needs main() function in it. What is the concept behind this..? By the way I'm a newbie to the Struts framework.
This is because the Struts framework provides the infrastructure to get the application up and running. As a user of the framework, you implement specific classes that are called by the framework code.
Struts itself runs inside an application server such as Tomcat, and usually it is the server that contains the main() method which is ultimately responsible for starting up your application.
This is an example of inversion of control -- many application frameworks work this way.
You need a main() method in applications started from the command line as that's the API that the JVM expects to execute in that case. Code called from within other frameworks must implement whatever API that framework expects. In many cases that framework implements main() and calls your code via some other API. This is usually more robust than public void main(String[] args) allows. In other instances the framework is itself called from another abstraction (e.g. a Web Application within a Web Framework within a App Container).