Basic Program using HttpServlet Class

Step 1: Projects Window After Adding Servlet Page in Source Package

Step 2:
java.​io Package
public class IOException extends Exception
Signals that an I/O exception of some sort has occurred. This class is the general class of exceptions produced by failed or interrupted I/O operations.

public class PrintWriter extends Writer
Prints formatted representations of objects to a text-output stream. This class implements all of the print methods found in PrintStream.

javax.​servlet Package
public class ServletException extends Exception
Defines a general exception a servlet can throw when it encounters difficulty.

javax.​servlet.​http Package


public abstract class HttpServlet extends GenericServlet implements Serializable
Provides an abstract class to be subclassed to create an HTTP servlet suitable for a Web site. A subclass of HttpServlet must override at least one method, usually one of these:
1.doGet, if the servlet supports HTTP GET requests
2.doPost, for HTTP POST requests
3.doPut, for HTTP PUT requests
4.doDelete, for HTTP DELETE requests
5.init and destroy, to manage resources that are held for the life of the servlet
6.getServletInfo, which the servlet uses to provide information about itself

public interface HttpServletRequest extends ServletRequest
Extends the ServletRequest interface to provide request information for HTTP servlets.
The servlet container creates an HttpServletRequest object and passes it as an argument to the servlet's service methods (doGet, doPost, etc).

public interface HttpServletResponse extends ServletResponse
Extends the ServletResponse interface to provide HTTP-specific functionality in sending a response. For example, it has methods to access HTTP headers and cookies.
The servlet container creates an HttpServletResponse object and passes it as an argument to the servlet's service methods (doGet, doPost, etc).

java.​io.​PrintWriter
public void println()
Terminates the current line by writing the line separator string. The line separator string is defined by the system property line.separator, and is not necessarily a single newline character ('\n').

Source Code
fServlet.java


Step 3: Web.xml code

Download Code Link 2

Step 4: Right click on fServlet.java code page and click on Run File or press Shift+F6
Click ok button

Output:

More Servlet Topics :

No comments: