Servlets are basically developed for server side applications and designed to handle
http requests. The servlet-programming interface (Java Servlet API)
is a standard part of the J2EE platform and has the following advantages over other
common server extension mechanisms:
• They are faster than other server extensions, like, CGI scripts because they use a
different process model
• They use a standard API that is supported by many Web servers.
• Since Servlets are written in Java, Servlets are portable between servers and
operating systems. They have all of the advantages of the Java language,
including ease of development.
They can access the large set of APIs available for the Java platform.
Now, after installing the Tomcat Server, let us create our first Java Servlet. Create a
new text file and save it as ‘HelloWorld.java’ in the
C:\jakarta-tomcat-4.0-b5\webapps\saurabh\WEBINF\classes\com\stardeveloper\servlets’ folder. ‘C:\jakarta-tomcat-4.0-b5\webapps\’
is the folder where web applications that we create should be kept in order for Tomcat to
find them. '\saurabh' is the name of our sample web application and we have created it
earlier in Installing Tomcat. '\WEB-INF\classes' is the folder to keep Servlet classes.
'\com\stardeveloper\servlets' is the package path to the Servlet class that we will
create. Now type the following text into the 'HelloWorld.java' file we created earlier:
// Your First Servlet program
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class HelloWorld extends HttpServlet {
public void doGet(HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException {
res.setContentType("text/html");
PrintWriter out = res.getWriter();
out.println("<HTML>");
out.println("<HEAD><TITLE>Hello, Welcome to the World of Servlet
Programming</TITLE></HEAD>");
out.println("<BODY>");
out.println("<BIG>Hello World</BIG>");
out.println("</BODY></HTML>");
}
}
Our HelloWorld program is extremely simple and displays Hello World in the
Browser. We override method doGet() of HttpServlet class.In the doGet() method we
set the content type to ‘text/html’ ( telling the client browser that it is an HTML
document ). Then get hold of PrintWriter object and using it print our own HTML
content to the client. Once done we close() it.
Compiling and Running the Servlet
Now, we will compile this Servlet using the following command at the DOS prompt:
C:\jakarta-tomcat-4.0-b5\webapps\star\WEBINF\
classes\com\stardeveloper\servlets>javac HelloWorld.java
If all goes well a 'HelloWorld.class' file will be created in that folder. Now, open your
browser and point to the following address:
http://localhost:8080/star/servlet/com.stardeveloper.servlets.HelloWorld
You should see response from the Servlet showing “Helloworld”
I have assumed here that you are running Tomcat at port 8080 ( which is the default
for Tomcat).
Just like an applet, a servlet does not have a main() method. Instead, certain methods
of a servlet are invoked by the server in the process of handling requests. Each time
the server dispatches a request to a servlet, it invokes the servlet’s service() method.
A generic servlet should override its service() method to handle requests as
appropriate for the servlet. The service() method accepts two parameters: a request
object and a response object. The request object tells the servlet about the request,
while the response object is used to return a response. Figure 1 shows how a generic
servlet handles requests.

In contrast, an HTTP servlet usually does not override the service() method. Instead, it
overrides doGet() to handle GET requests and doPost() to handle POST requests. An
HTTP servlet can override either or both of these methods, depending on the type of
requests it needs to handle. The service() method of HttpServlet handles the setup and
dispatching to all the doXXX() methods, which is why it usually should not be
overridden. Figure 2 shows how an HTTP servlet handles GET and POST requests.

An HTTP servlet can override the doPut() and doDelete() methods to handle PUT and
DELETE requests, respectively. However, HTTP servlets generally don’t touch
doHead(), doTrace(), or doOptions(). For these, the default implementations are
almost always sufficient.
http requests. The servlet-programming interface (Java Servlet API)
is a standard part of the J2EE platform and has the following advantages over other
common server extension mechanisms:
• They are faster than other server extensions, like, CGI scripts because they use a
different process model
• They use a standard API that is supported by many Web servers.
• Since Servlets are written in Java, Servlets are portable between servers and
operating systems. They have all of the advantages of the Java language,
including ease of development.
They can access the large set of APIs available for the Java platform.
Now, after installing the Tomcat Server, let us create our first Java Servlet. Create a
new text file and save it as ‘HelloWorld.java’ in the
C:\jakarta-tomcat-4.0-b5\webapps\saurabh\WEBINF\classes\com\stardeveloper\servlets’ folder. ‘C:\jakarta-tomcat-4.0-b5\webapps\’
is the folder where web applications that we create should be kept in order for Tomcat to
find them. '\saurabh' is the name of our sample web application and we have created it
earlier in Installing Tomcat. '\WEB-INF\classes' is the folder to keep Servlet classes.
'\com\stardeveloper\servlets' is the package path to the Servlet class that we will
create. Now type the following text into the 'HelloWorld.java' file we created earlier:
// Your First Servlet program
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class HelloWorld extends HttpServlet {
public void doGet(HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException {
res.setContentType("text/html");
PrintWriter out = res.getWriter();
out.println("<HTML>");
out.println("<HEAD><TITLE>Hello, Welcome to the World of Servlet
Programming</TITLE></HEAD>");
out.println("<BODY>");
out.println("<BIG>Hello World</BIG>");
out.println("</BODY></HTML>");
}
}
Our HelloWorld program is extremely simple and displays Hello World in the
Browser. We override method doGet() of HttpServlet class.In the doGet() method we
set the content type to ‘text/html’ ( telling the client browser that it is an HTML
document ). Then get hold of PrintWriter object and using it print our own HTML
content to the client. Once done we close() it.
Compiling and Running the Servlet
Now, we will compile this Servlet using the following command at the DOS prompt:
C:\jakarta-tomcat-4.0-b5\webapps\star\WEBINF\
classes\com\stardeveloper\servlets>javac HelloWorld.java
If all goes well a 'HelloWorld.class' file will be created in that folder. Now, open your
browser and point to the following address:
http://localhost:8080/star/servlet/com.stardeveloper.servlets.HelloWorld
You should see response from the Servlet showing “Helloworld”
I have assumed here that you are running Tomcat at port 8080 ( which is the default
for Tomcat).
Just like an applet, a servlet does not have a main() method. Instead, certain methods
of a servlet are invoked by the server in the process of handling requests. Each time
the server dispatches a request to a servlet, it invokes the servlet’s service() method.
A generic servlet should override its service() method to handle requests as
appropriate for the servlet. The service() method accepts two parameters: a request
object and a response object. The request object tells the servlet about the request,
while the response object is used to return a response. Figure 1 shows how a generic
servlet handles requests.
In contrast, an HTTP servlet usually does not override the service() method. Instead, it
overrides doGet() to handle GET requests and doPost() to handle POST requests. An
HTTP servlet can override either or both of these methods, depending on the type of
requests it needs to handle. The service() method of HttpServlet handles the setup and
dispatching to all the doXXX() methods, which is why it usually should not be
overridden. Figure 2 shows how an HTTP servlet handles GET and POST requests.
An HTTP servlet can override the doPut() and doDelete() methods to handle PUT and
DELETE requests, respectively. However, HTTP servlets generally don’t touch
doHead(), doTrace(), or doOptions(). For these, the default implementations are
almost always sufficient.
No comments:
Post a Comment