September 15, 2009

How to create a Simple web-page and run it in Tomcat

Hello everyone, Hoping this information could be of some use to anyone at any point of time. Further down in this post, we are goin to make a simple web page and will learn how to run it in tomcat. Darn Simple process :) but just need to be clear with few things before. The strcture of Tomcat folder.

Now to make any simplest web-application there can be two ways:
1. One is directly make folders and configuration files and every related stuff manually. Which is quite tiresome for those who are used to IDE or tools.
2. Other way is definitely to use an IDE or any tool. :p. Entire project structure is automatically created without even the user's knowledge.

Well, I would always recommend to create your first web application by using first approach as it gives you the idea of which file do what? What is the role of being at particular location and almost every stuff about web thing.

Project structure of any web-application to run in Tomcat: 
Have a look at just once at the folder where your tomcat is installed. For example, in my machine is installed in "H:\>apache-tomcat-6.0.20". There will be a "bin" folder . Inside it , there are two .bat files of interest [Note: Don't touch rest of the files.] Just ote "startup.bat" for startin the server and "shutdown.bat" for stopping the server. Now come out of "bin" folder and check rest of the things. The folder of interest and most importance here is "webapps". Whatever web-application we will make, will be put into "webapps" folder so as to delploy it in "Tomcat". Tomcat is in fact not a server[ as many people call it as web server, application server and blah blah..]. It is actually a web container that allows web components like jsp and servlets to run.



One more thing, as I said above that whatever web application we make is put directly into "webapps" folder of "Tomcat" but wait wait I meant that actually every web-application made is zipped and that archive file is called as "WAR" [Web Archive] in web terms and we simply put that war file in "webapps" folder and double click "startup.bat" residing in "bin" folder" of  "Tomcat" and our war file is deployed and on startup of tomcat , our web pages would come in Browser. Now question that might be irritating you all would be -"Why cannot we directly delpoy web project as it is, why do we need an extra overhead of creating a 'war' file" right?" So answer is yes, you can very well copy paste your entire web project in "webapps" folder directly and can deploy it but "war" file concepts is introduced to transfer the entire project conveniently form one machine to another machine and can be deployed on any machine easily. You can avoid the overhead of transferring bulk project[Care need to be taken with full structure as well] from one machine to another machine. Okies, this was all basic which I think you all should be awared of first. If you all know it already that's really very good of you.

Okies, so now lets make our project in any other Drive(Lets' say I:\>) other than(H:\>) where our tomcat is installed. Web Project structure should always include a "WEB-INF" folder . It is case-sensitive. This folder is compulsory. All web files like htmls, images and jsps etc.. are kept in this folder. The story behind this "WEB-INF" folder is this , user cannot directly access any web-page saved in this folder. They need to navigate form any other page which might be saved out of "WEB-INF" folder.

STEP 1: Create a folder named "MyFirstWebApp" in "I:\>" drive.
STEP 2: Create a "WEB-INF" folder inside it. Remember "WEB-INF" is case-sensitive name.
STEP 3: Create a "index.html" file outside of "WEB-INF" folder. This file will be directly accessible from browser. "index.html" should have following lines of code.



STEP 4: Now create a folder named as "html" outside "WEB-INF" folder. Inside it create a "Hello.html" which has following code.


STEP 5: Now copy the entire "MyFirstWebApp" folder structure inside "webapps" folder of Tomcat" installed directory.

STEP 6: Go to "bin" folder of "Tomcat" and click on "startp.bat" . Server gets started. Stop when you see "Server start up.. " message in Command prompt screen like following



STEP 7: Now open browser and enter following url http://localhost:8080/MyFirstWebApp/index.html . You will see following page.


STEP 8: Now click on hyperlink "Hello" in above shown page and you will get following page.


STEP 9: Now click on "Go back to Index" link, you will get page in STEP 7 again.

STEP 10: Now let us include jsp pages also into it. Create an "index.jsp" page in "MyFirstWebApp folder". It should have following code.

STEP 11: Create a "jsp" folder in "MyFirstWebApp" folder and inside it create a "hello.jsp" with the code as below inside it.


 STEP 12: Now repeat STEP 7 to STEP 9 but with url as http://localhost:8080/MyFirstWebApp/index.jsp At all places you will just see "jsp" instead of "html" in those previous steps.

This was just a very simplest example of making a web-pages and deploying it in Tomcat. I will update with few more simplest compilcations with JSPs and few other things.

Till then enjoy..and feel free to fire questions. :)