What Is Servlet? An Introduction To Java Servlets
What Is Servlet? An Introduction To Java Servlets
javax.servlet.Servlet is one of pre-defined top most interface, which is containing life cycle methods[init(), service(), destroy() i will describe about these methods later] for servlet. javax.servlet.GenericServlet is one of the predefined abstract class which is implementing javax.servlet.Servlet interface, this GenericServlet class is used for developing protocol independent applications [ But in real time we will use protocol dependent applications only, so GenericServlet wont be appeared in real time ] Javax.servlet.http.HttpServlet is sub class of GenericServlet used for developing protocol dependent application [ HTTP protocol ]
Consider Myserlet.java is our own class always recommended to extends HttpServlet servlet, as our current internet world is supporting HTTP protocol right. In real time, we will use HttpServlet only not GenericServlet remember.
Create one root directory with your own name, and create another directory within that directory with name src and now write one servlet program and copy into that src folder Now create another folder web-inf in the root directory, this web-inf folder contains web.xml file will see later Create classes folder with in web-inf folder
Compile our servlet program which is in src folder, so we will get one .class file right, just copy this .class file into classes folder in web-inf folder By the way, we are going to run our servlet by using Tomcat server. So we must set the class path for Tomcat related jar file, servlet-api.jar You can find this jar file in your Tomcat directory\common\lib\, Similarly if we are using Weblogic server, we need to set the class path for weblogic related jar files, i.e weblogic.jar which is in c [your drive]:\bea\weblogic\server\lib If our servlet is dealing with any database like MySql or Oracle, it is mandatory for us to create a folder lib with in web-inf folder Once everything is done, just copy our root director into Tomcat/webapps folder, and start the server and check the output, i will show you how in the first application
Directory Structure:
Set servlet-api.jar ( Will be available in your tomcat/lib folder ) in your class path Open notepad or any text editor EditPlus or some thing Import javax.servlet.*; Import javax.servlet.http.*; Create one user defined class whose modifier must be public, which must extends javax.servlet.GenericServlet[Not recommended] or javax.servlet.http.HttpServlet[recommended] We know GenericServlet implementing Servlet interface, and HttpServlet is the sub class of GenericServlet. So by default all life cycle methods will be overridden in GenericServlet class, so just override the required [No need all] life cycle methods of servlet in HttpServlet.
Save your servlet program by giving an extension .java You must place your .java files in root folder or in src folder, and .class files in classes files Compile and check the result
Yeah these are the basic and default steps for writing any servlet application, i will tell you the more(web.xml, deployments and all) while we are working with the examples Once everything is done, just copy our root director into Tomcat/webapps folder, and start the server and check the output, i will show you in the first example.
We must call our servlet by typing its <url-pattern> only For example http://localhost:8080/OurServletApplicationFolderName/UserFriendlyName Once we call our servlet just like above, then the server will first loads web.xml and verifies whether the url pattern we are calling is same as what in <url-pattern> tag in web.xml [Line number 8] or not If matched, it will checks the <servlet-name> [Line number 7] and will jump to <servlet-name> [Line number 3] in <servlet> tag and, will check whether the value of <servlet-name> in line number 3 & line number 7 are same or not if same it will load our class in <servlet-class> [line number 4] Finally our .class file will be executed, so we can check the output
Setting Classpath:
Open command prompt Type this command, set classpath=C:\Program Files (x86)\apache-tomcat-7.0.33\lib; Thats it
But friends, dont worry. We are going to run the servlet application in Eclipse so things are very very easy, i will show you in the first program.
We should write .java files in src folder and .class files will be automatically copied into build folder including the package, so i am going to create .java file OnGeneric.java [ First application i am going to tell you using GenericServlet, from the next application i will take HttpServlet only ]
So we need to create the classpath for servlet-api.jar > right click on ROOT folder [ Servlet-Hello-World-Java4s ] > Properties > now a window will open in front of you > left side click on Java Build Path > and click on Add External Jars and add the servlet-api.jar from the tomcat folder > and click on ok