how to run the servlet in eclipse? - eclipse

I want to run a servlet in Eclipse. For this I have created a dynamic web project and I have deployed my servlet.java file under the WEB-INF folder. I have also added the servlet.jar file. How can I run the file as a java application?

Servlets run in a servletcontainer. Servlets are not "plain vanilla" java applications. See, they do not have a main() method! Servlets listens on HTTP requests and returns HTTP responses through the network. Running the sole servlet class as a plain vanilla Java application doesn't automagically make them to listen and react on HTTP requests.
Apache Tomcat is a popular servletcontainer. Just download and unzip it. Then in Eclipse (I assume that you already have downloaded the Eclipse Java EE version, else drop it all together and redownload the right version), go to Servers view and add the newly installed Tomcat instance. Then create a Dynamic Web Project wherein you pick the newly integrated server instance from the list. Eclipse will then automatically take the Servlet API libraries in the classpath/buildpath (thus, you do NOT need to download a random "servlet.jar file" separately yourself! this is only receipt for major trouble). Then create a Servlet class and register it in web.xml. Then deploy the project to the newly integrated server and start it. Then in your favourite webbrowser go to http://hostname:port/contextname which is usually http://localhost:8080/webprojectname.
To learn more about servlets (and Eclipse and Tomcat) I strongly recommend you to go through those tutorials. You can also search on youtube for video tutorials using the obvious keywords.

Servlets run in servlet/JSP engines, like Tomcat or Resin or Jetty. You normally don't run them outside a container.
You can certainly deploy your app to a servlet/JSP engine and start in from Eclipse. But it's the app server that you run, which then acts as the home for your servlet.

Servlets cannot be run directly as an Java app.
I recommend two approaches:
Refactor your servlet and put your "java" code (what ever generic code you want to call) in another jar that you call from both the servlet and the Java application.
Run the servlet in a container in Eclipse. See http://www.eclipse.org/jetty/
Communicate with the servlet through HttpRequest's and HttpResponse's

Related

make a standalone independent application out of a dynamic web project in eclipse

So, I've made a dynamic web project in Eclipse.It uses Jsp and servlets. Now I am told to make a standalone independent application for the same. I tried to google as to how it can be done. Most of them pointed me to use Export->Runnable jar file. But on doing so I can't find my dynamic web project in the Launch Configuration. Now how to make the standalone independent application ?
I've done a similar thing using embedded Jetty which means the application jar provides the web service without having to rely on a web server.

Tomcat REST services not deployed with Intellij IDEA

I have just switched to Intellij Idea (11) and I'm having possibly simple problem for which I cannot find a solution within Intellij IDE. I have a web application which contains some classes marked with annotations from javax.ws.rs like eg. #Path("/members") etc and my web.xml file configured for handling REST calls.
The application is build with Maven, when I package the application either from command line or through the Intellij IDE and copy the .war file into tomcat webapps directory manually, all works fine, eg. I can access GET based services from the browser.
If I deploy the application through configured tomcat in Intellij IDE the application works but without the REST portion, so I'm not able to execute any rest based calls (all end up with 404 error). I cannot see anything in logs about deploying rest service classes like I do during manual deployment:
INFO: Adding scanned resource: com.softberries.klerk.rest.MemberResourceRESTService
so its definitively an IDE configuration option I've missed which prevents tomcat from scanning classes for this annotations.
The question is how should I configure my Tomcat within the IDE to work the same as started manually.
with Intellij you need to expose the classes as web services from Tools > WebServices.
Also check that in Setting > Web Services, the prefix path for web service is correct (by default is /services).
Here you have a nice tutorial about how to create webservices in intellij.
http://www.academia.edu/4526516/Creating_Web_Services_Applications_with_IntelliJ_IDEA
By the way... when you do a deploy from intellij, the application goes to:
C:\Users\userName.IntelliJIdea10\system\tomcat\NameDeploy"
This info appears in the console as: "Using CATALINA_BASE": .../path...
Hope this helps,
Cheers

Eclipse doesn't import all Axis2 jars but project still runs on Tomcat?

When I develop an Axis2 web service on Eclipse, I noticed that Eclipse is automatically copying the classes from the lib folder of Axis2 to the lib folder of the new project. However, not all classes from the lib folder of Axis2 are being copied. Interestingly, the web service runs without any problem when deployed to Tomcat via Eclipse even if some the jars from Axis2 were not copied. Also, when I viewed the temp file of Tomcat, Tomcat seems to generate the jars for the listed modules on modules.list of the web service.
Can someone enlighten me regarding what is happening on this? Why Eclipse doesn't copy all the jars from Axis2? Why can the web service run on Tomcat even without the other jars from Axis2? What are those temp files for? When and why is it being generated?
I tried to run the same project on WebSphere and I am encountering a ClassDefNotFound exception because of the missing jars. My problem was solved when I copied all the Axis2 jars that was not copied by Eclipse to my project. But I'm not comfortable with my solution because Tomcat can run my project even without those jars. Is my solution really the right solution? Or am I missing a configuration setting?
This is just for clarification:
My web service is already running in Axis2. My class loading policy is set to PARENT_LAST. I know that since WebSphere has its own Axis2 configuration, the class loading policy must be set to PARENT_LAST so that WebSphere will use the Axis2 from the project itself. Aside from setting the class loading policy, I did something to make my web service run on WebSphere. I describe what I did above. My question is why such method must be taken?
WebSphere has it's own axis2 configuration as part of its Java EE server spec for JAX-WS. Change your class loading policy to PARENT_LAST and check if that solves your problem.
Edit:
As the original post already states: WebSphere is a Java EE server depending on version it supports its the standard Java JAX-WS web services. Actually web services became part of the standard jdk.
If you use JAX-WS like mentioned in Introduction to JAX-WS or building web services then you don't have to add any 3rd party library for getting your web services running. As soon as you use the non JDK implementation like axis2 you have to package it with your application.
IBM didn't just pack the axis2 into their WAS/JDK, they modified it. I'm not sure what Tomcat delivers, however as long as you use JAX-WS it shouldn't matter. With JAX-WS you don't have any direct import of the org.apache.axis packages. If you use these imports you have to supply the libraries and make sure that yours are loaded.

Can we check if the web project is working without deployment in Eclipse?

I just started web development, here is a simple example: i write a form submit jsp file and deployment descriptor web.xml to map the logical name used for request and servlet class , and controller servlet handle the http request and use printwriter to print the information in the request.(Don't have the model in this example.)
I know the simple way is to use ant to build a war and put into the web container(Like tomcat), but do i need to do it every time to see if the project is running? Or have a quick way to debug and see the result in eclipse?
A servlet container is the runtime environment for a Java web application. There are several open-source servlet containers available. Tomcat & Jetty are two popular ones. Both have plugins available that will allow you to do all your development and deployment from within Eclipse:
Tomcat & Eclipse
Jetty & Eclipse

Gwt and eclipse with jetty for production mode

Is there a standard easy way to launch Jetty from eclipse for the production mode?
I followed the instructions at http://code.google.com/webtoolkit/usingeclipse.html to setup a new project using gwt and eclipse.
The hosted (debug) mode seems to be already configured to work properly and involves one click in eclipse using "Debug as".
I would like to configure "Run as" to run the production mode in jetty. Is there a good tutorial on the standard way to set this up?
You have to compile your GWT-project before you can "Run As". It's the button with a red box and "G" on it.
Since GWT compiles to java script you don't need jetty if you don't have server-side logic... anyway, in a normal scenario you'll have some server interaction, BUT you're not sticked to jetty; tomcat or other server side technology could be used.
You can deploy the compiled JS to a web server and the server side logic to tomcat, jboss, jetty... even you could interact with php at the server.
Deployment to Google AppEngine is another option, a natural option if you use the eclipse plugin since it's tightly integrated with it.
In eclipse, Jetty is no more than a java process started like any normal java main.
Go to [Run]->[Run Configurations]->[Web Application]->[Your GWT Project] and you'll see a normal java main exec config.
Look at the [Main Class] option -> this is the embeded jetty ..or at
the [VM arguments] for the JVM
The ?gwt.codesvr= param in the URL only instruct the browser plugin to interact with the eclipse plugin embeded jetty in a way that only the modified code is re-compiled to JS and sent to the browser.
If you do not use the ?gwt.codesvr= probabilly you'll have to recompile all the project every time you change a single line of code.
I recommend you to clearly separate the compiled-to-js code and the server side code in different eclipse projects.
I also run the embeded jetty only for the client-side code; the server side code is run in tomcat.
That way I have a clear separation of layers at the time I can debug client-side GWT code and server-side java code.