programming4us
programming4us
WEBSITE

Servlet Development and Deployment : Writing our first servlet & Compiling the servlet

- How To Install Windows Server 2012 On VirtualBox
- How To Bypass Torrent Connection Blocking By Your ISP
- How To Install Actual Facebook App On Kindle Fire
2/20/2012 5:36:00 PM

What is a servlet?

A servlet is a Java class that is used to extend the capabilities of servers that host applications. Servlets can respond to requests and generate responses. The base class for all servlets is javax.servlet.GenericServlet. This class defines a generic, protocol-independent servlet.

By far, the most common type of servlet is an HTTP servlet. This type of servlet is used in handling HTTP requests and generating HTTP responses. An HTTP servlet is a class that extends the javax.servlet.http.HttpServlet class, which is a subclass of javax.servlet.GenericServlet.

A servlet must implement one or more methods to respond to specific HTTP requests. These methods are overridden from the parent HttpServlet class. As can be seen in the following table, these methods are named so that knowing which one to use is intuitive:

HTTP request HttpServlet method
GET doGet(HttpServletRequest request, HttpServletResponse response)
POST doPost(HttpServletRequest request, HttpServletResponse response)
PUT doPut(HttpServletRequest request, HttpServletResponse response)
DELETE doDelete(HttpServletRequest request, HttpServletResponse response)

Each of these methods take the same two parameters, namely an instance of a class implementing the javax.servlet.http.HttpServletRequest interface and an instance of a class implementing the javax.servlet.http.HttpServletResponse interface.

Application developers never call these methods directly. They are called automatically by the application server whenever it receives the corresponding HTTP request.


Of the four methods listed previously, doGet() and doPost() are by far the most commonly used.

An HTTP GET request is generated whenever a user types the servlet's URL in the browser, when a user clicks on a link pointing to the servlet's URL, or when a user submits an HTML form using the GET method, where the form's action points to the servlet's URL. In any of these cases, the code inside the servlet's doGet() method gets executed.

An HTTP POST request is typically generated when a user submits an HTML form using the POST method and an action pointing to the servlet's URL. In this case, the servlet's code inside the doPost() method gets executed.

Writing our first servlet

In this section, we will see how that servlet was developed, configured, and packaged.

The code for the servlet is as follows:

package net.ensode.glassfishbook.simpleapp;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class SimpleServlet extends HttpServlet
{
protected void doGet(HttpServletRequest request, HttpServletResponse response)
{
try
{
response.setContentType("text/html");
PrintWriter printWriter = response.getWriter();
printWriter.println("<h2>");
printWriter.println("If you are reading this, your application server is good to go!");
printWriter.println("</h2>");
}
catch (IOException ioException)
{
ioException.printStackTrace();
}
}
}



As this servlet is meant to execute when a user enters its URL in the browser window, we need to override the doGet() method from the parent HttpServlet class. Like we explained previously, this method takes two parameters: an instance of a class implementing the javax.servlet.http.HttpServletRequest interface, and an instance of a class implementing the javax.servlet.http.HttpServletResponse interface.

Even though HttpServletRequest and HttpServletResponse are interfaces, application developers don't typically write classes implementing them. When control goes to a servlet from an HTTP request, the application server (in our case, GlassFish) provides objects implementing these interfaces.


The first thing our doGet() method does is set the content type for the HttpServletResponse object to "text/html". If we forget to do this, the default content type used is "text/plain", which means that the HTML tags used a couple of lines down will be displayed on the browser, as opposed to them being interpreted as HTML tags.

We then obtain an instance of java.io.PrintWriter by calling the HttpServletResponse.getWriter() method. We can then send text output to the browser by calling the PrintWriter.print() and PrintWriter.println() methods (the previous example uses println() exclusively). As we set the content type to "text/html", any HTML tags are properly interpreted by the browser.

Compiling the servlet

To compile the servlet, the Java library included with GlassFish must be in the CLASSPATH. This library is called javaee.jar and it can be found in the [glassfish installation directory]/glassfish/lib folder.

To compile from the command line using the javac compiler, a command like the following must be issued (all in one line):

javac -cp /opt/sges-v3/glassfish/lib/javaee.jar net/ensode/glassfishbook/simpleapp/SimpleServlet.java


Of course, these days very few developers compile code with the "raw" javac compiler. Instead, either a graphical IDE or a command line build tool such as Apache ANT or Apache Maven is used. Consult your IDE or build tool documentation for information on how to add the javaee.jar library to its CLASSPATH.

Maven

Apache Maven is a build tool similar to ANT. However, Maven offers a number of advantages over ANT, including automatic download of dependencies and standard commands for compilation and packaging of applications.

When using Maven, the code can be compiled and packaged by issuing the following command in the project's root directory (in this case, simpleapp): mvn package

Maven can be downloaded from http://maven.apache.org/.


Other  
 
Top 10
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Finding containers and lists in Visio (part 2) - Wireframes,Legends
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Finding containers and lists in Visio (part 1) - Swimlanes
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Formatting and sizing lists
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Adding shapes to lists
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Sizing containers
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 3) - The Other Properties of a Control
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 2) - The Data Properties of a Control
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 1) - The Format Properties of a Control
- Microsoft Access 2010 : Form Properties and Why Should You Use Them - Working with the Properties Window
- Microsoft Visio 2013 : Using the Organization Chart Wizard with new data
REVIEW
- First look: Apple Watch

- 3 Tips for Maintaining Your Cell Phone Battery (part 1)

- 3 Tips for Maintaining Your Cell Phone Battery (part 2)
Video Sports
programming4us programming4us
programming4us
 
 
programming4us