Best Blogger Tips

Wednesday 3 October 2012

Running the first servlet program!



This brief tutorial will help you in running your first Java Servlet. As java Servlets are server side programs that are compiled by a java compiler and the their class files are executed by a web browser.
So it would be your first question how can we execute that class file(Servlet's Bytecode) on a web browser by submitting a URL of that servlet name?Definitely it's very easy and straight forward.

To run a Servlet you must have a web container installed and configured in your computer. In case of java Servlets you must have a Tomcat installed in your computer.It is a java based web server founded by the Apache Organization.

Apart from this, you should also have the standard JDK installed on your local machine.
So first of all i will teach you how to install standard JDK and it's configuration on local machine.

Following steps will help you in installing JDK and setting path on your local machine:

1- Download the latest JDK from the oracle java distribution . Follow the link below:

---> Download JDK
You will get the web page containing the given snapshot.
Choose your platform related distribution.

2- After downloading this software, double click the EXE file to start it's installation. By default it will be install in C://programfiles/java. You can change the path.

3- Now open up the JDK folder from newly created java folder and further open the bin folder. And after that, Copy the path from path bar.

4- Now right click My Computer go to properties->advanced tab->Environmental variables->new path and write "PATH" in variable name without coarse and paste the path in variable value and put a semicolon at the end.

--------------------------------------------------------

Variable Name : Path
Variable Value : C:\Program Files\Java\jdk1.6.0_26\bin;

--------------------------------------------------------
Apply Changes.


Now test it's execution:

Go to start->run->type "CMD"without coarse.
Type Command: javac
You will be able to see a bunch of information
And then
Type : java
Again you will see a bunch of information

All Done....

----------------------------------------------------

Now the real story begins...

Installing Tomcat:

1- First of all download Tomcat from free apache distribution centre from the following link.

--- > Download Tomcat.

2- Make sure you download the version of the Tomcat similar to the JDK i.e, JDK 7.0, Tomcat 7.0.

Note :- In this case, you should download the zip file of the binary distribution. (Make sure to check the platform relevancy.. In my case i chooses 32 bit windows zip for my 32 bit window 7 and 32 bit window xp).
(Reason for choosing zip file is that it wont required to be install, it will be configured easily)





3 - After downloading, right click My Computer->properties(in case of windows 7, open advanced system properties)->advanced tab->environmental properties->path-And then edit both fields with.

---------------

Variable name : java_home

Variable value : C:\Program Files\Java\jdk1.6.0_26 (my default location of jdk)

----------------

Press ok, AND THEN ONCE AGAIN OPEN PATH and edit both fields with:

---------------

Variable name : jre_home

Variable value : C:\Program Files\Java\jre6 (my default location of jre)

----------------

APPLY CHANGES...



Now start the services of the Tomcat through following guidelines:



1 - Go the the tomcat directory.

2- Open bin

3- Double Click startup.bat file.



Test Run:-

Open browser and write the following URL

=>Localhost:8080



=> ALL DONE if you receive the window of the Apache Software foundation.

--------------------------------------------------------------------

Now after when you have installed and configured the jdk and tomcat, you are ready to editing and compiling your first java servlets but this wont happen unless you paste the packages that are required by the servlet in the jdk because by default jdk don't provide the servlets packages (javax.http, javax.servler.http).
This would be done through following guid lines:

1 - Go to your tomcat directory.
2 - Open lib folder.
3 - Copy "Servlet-api" (jar file:java archive file).
4 - Now go to java folder and open it.
5 - Open jdk
6- Open jre(within jdk folder not with jdk)
7 - Open lib.
8 - Open ext.
9 - Paste the servlet-api here.

All done(Not Everything).... you have successfully built the servlet library in your standard JDK now you are ready to import both packages in your serlvet program and compile it.

GETTING READY TO RUN SERVLET :-

Follow these steps : -
1 - Write a java servlet through any plain text editor(I recommend you use Notepad++).
Following is an Example Program : -

import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
public class MyFirstWebApp extends HttpServlet{

public void doGet(HttpServletRequest req, HttpServletResponse res)throws IOException, ServletException {


PrintWriter out = res.getWriter();
out.write("Here is My First Web Application");//This line will be printing on //browser

}
}
2 - Open your tomcat directory -->webapps-->ROOT-->WEB-INF, and create a folder with the name "classes"(without coarse) in this directory and SAVE your server here with the name MyFirstWebApp.java

3- Now open the Command prompt and access the directory of classes containing servlet java file,described above.

4- Compile it with command "Javac MyFirstWebApp.java"(without coarse).
5-It will generate the bytecode.
6- Dont run the class file with java JIT (e.g java MyFirstWebApp) as this class would execute by the browser.
7- Now make some changes in xml file called WEB located in-->tomcat-->webapps-->root-->WEB-INF.
8 - Right click on it and open it with some text editor and Edit it with this:








Welcome to Tomcat

Welcome to Tomcat

MyFirstWebApp
MyFirstWebApp




MyFirstWebApp
/MyFirstWebApp





9 - Now start services of the tomcat by executed startup.bat file located in bin directory of the Tomcat.
10 - Open web browser
11- Write URL: Localhost:8080/MyFirstWebApp
12 - You will receive this output : Here is My First Web Application.

Now Everything is done.. This would be all.. Good Luck.!


Kindly Bookmark this Post using your favorite Bookmarking service:
Technorati Digg This Stumble Stumble Facebook Twitter
YOUR ADSENSE CODE GOES HERE

0 comments:

Blogger Tricks And TipsComment here

 

| Bloggers Broadcast © 2010. All Rights Reserved | Back To Top |

Your Text Link Here