I’m using Tomcat 7.0 with Eclipse. The tomcat server is synchronized with eclipse. After creating my first class, I put the .java file under src/(default package)/HelloWorld.java (not good practice I know but just for testing)
The content is just as follows, fairly simple:
import java.io.*;
import javax.servlet.*;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.*;
@WebServlet("/HelloWorld")
public class HelloWorld extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println ("Hello World");
}
}
Well many tutorials claim that I must use javac to compile the code. But I did nothing and it ran with no problem. Also everytime I change the code it updates immediately just like magic. Something must be working but I don’t know what it is.
Yeah it’s obviously a newbie question so any help is welcome. Also It’s better if you have any systematical and easy-to-follow tutorial links. I’m searching for them for several days but got lots of inconsistent answers.
To me, You mix 2 main technologies – tomcat as a web container and eclipse as your IDE. Their integration confuses you.
Lets leave JSP for now and talk only about servlets, because it confuses even more
Tomcat cannot work with source files (*.java). You Must compile your application with javac for example and create something called WAR – web archive – a zip file that will contain your compiled class and adheres some EE standards that tomcat understands (its also possible to use folder instead of zip, but lets put it aside as well, its not relevant for this explanation).
Among others this war (once compiled correctly) will contain your compiled servlet class HelloWorld.class).
Once tomcat is started and recognizes your war file in deployment folder it opens it and loads in runtime.
No compilation, only runtime loading.
Now people talk here about JSP.
In fact JSP is something that is technically equivalent to servlet but resembles HTML.
You put the file with extension .jsp and build your WAR. java compiler can’t read JSP files, so you should put them into your war file somehow (usually build tools/IDE do it for you). Bottom line is you have JSP files as you’ve created them in your war.
Now you put your war into Tomcat, it recognizes it as before and loads. At this point it still does nothing with your JSPs.
So, your war is deployed, tomcat is started and go to ‘http://localhost:8080/myfirstjsp.jsp‘ from your browser
At this point (first invocation of your JSP) a lot of thing happen:
Next time you’ll invoke the JSP, it will be already compiled.
The last question here is how Eclipse connected to all this story 🙂
In fact Eclipse has an integration with tomcat, so all the war-creating-and-deploying stuff is transparent to do. That’s why you push ‘play’ on eclipse and it compiles your project, creates a war, makes sure tomcat is aware of this war (configures deployment related stuff), starts tomcat and voila! – you have you application working.
Its important to understand what happens at what level
Hope this helps
Mark