Anda di halaman 1dari 3

Servlets are Java classes which service HTTP requests and implement the javax.servlet.Servlet interface.

Web application developers typically write servlets that extend javax.servlet.http.HttpServlet an abstract class that implements the Servlet interface and is specially desi!ned to handle HTTP requests.

Sample Code for Hello World:


"ollowin! is the sample source code structure of a servlet example to write Hello World#
// Import required java libraries import java.io.*; import javax.servlet.*; import javax.servlet.http.*; // Extend HttpServlet class public class HelloWorld extends HttpServlet { private Strin messa e;

public void init!" thro#s ServletException { // $o required initiali%ation messa e & 'Hello World'; ( public void do)et!HttpServlet*equest request+ HttpServlet*esponse response" thro#s ServletException+ I,Exception { // Set response content t-pe response.set.ontent/-pe!'text/html'"; // 0ctual lo ic oes here. 1rintWriter out & response. etWriter!"; out.println!'2h34' 5 messa e 5 '2/h34'"; ( public void destro-!" { // do nothin . ( (

Compiling a Servlet:
$et us put above code if HelloWorld.java file and put this file in %#&Servlet'evel (Windows) or *usr*Servlet'evel (+nix) then you would need to add these directories as well in %$,SSP,TH. ,ssumin! your environment is setup properly !o in ServletDevel directory and compile HelloWorld.java as follows#

6 javac HelloWorld.java

-f the servlet depends on any other libraries you have to include those J,. files on your %$,SSP,TH as well. - have included only servlet/api.jar J,. file because -0m not usin! any other library in Hello World pro!ram. This command line uses the built/in javac compiler that comes with the Sun 1icrosystems Java Software 'evelopment 2it (J'2). "or this command to wor3 properly you have to include the location of the Java S'2 that you are usin! in the P,TH environment variable. -f everythin! !oes fine above compilation would produce HelloWorld.class file in the same directory. 4ext section would explain how a compiled servlet would be deployed in production.

Servlet Deployment:
5y default a servlet application is located at the path 6Tomcat/installation/ directory7*webapps*.88T and the class file would reside in 6Tomcat/installation/ directory7*webapps*.88T*W95/-4"*classes. -f you have a fully qualified class name of com.myorg.MyServlet then this servlet class must be located in W95/-4"*classes*com*myor!*1yServlet.class. "or now let us copy HelloWorld.class into 6Tomcat/installation/ directory7*webapps*.88T*W95/-4"*classes and create followin! entries in web.xml file located in 6Tomcat/installation/directory7*webapps*.88T*W95/-4"*
2servlet4 2servlet7name4HelloWorld2/servlet7name4 2servlet7class4HelloWorld2/servlet7class4 2/servlet4 2servlet7mappin 4 2servlet7name4HelloWorld2/servlet7name4 2url7pattern4/HelloWorld2/url7pattern4 2/servlet7mappin 4

,bove entries to be created inside 6web/app7...6*web/app7 ta!s available in web.xml file. There could be various entries in this table already available but never mind. :ou are almost done now let us start tomcat server usin! 6Tomcat/installation/ directory7&bin&startup.bat (on windows) or 6Tomcat/installation/directory7*bin*startup.sh (on $inux*Solaris etc.) and finally type http: localhost:!"!" HelloWorld in browser0s address box. -f everythin! !oes fine you would !et followin! result#

Anda mungkin juga menyukai