Simple HTTP servlet

Example-Simple Http Servlet package itso.servjsp.servletapi; import java.io.*; import javax.servlet.*; import javax.servlet.http.*; pu...


Example-Simple Http Servlet
package itso.servjsp.servletapi;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class SimpleHttpServlet extends HttpServlet {
protected void service(HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException {
res.setContentType("text/html");
PrintWriter out = res.getWriter();
out.println("<HTML><TITLE>SimpleHttpServlet</TITLE><BODY>");
out.println("<H2>Servlet API Example - SimpleHttpServlet</H2><HR>");
out.println("<H4>This is about as simple a servlet as it gets!</H4>");
out.println("</BODY><HTML>");
out.close();
}
}

As the title indicates, SimpleHttpServlet is a very simple HTTP servlet that accepts a request and writes a response. Let’s break out the components of this servlet so we can discuss them individually.

Basic servlet structure

Example given below shows that we have defined this servlet to be part of an itso.servjsp.servletapi Java package.
package itso.servjsp.servletapi;
Code given below shows the import statements used to give us access to other Java packages. The import of java.io is so that we have access to some standard IO classes. More importantly, the javax.servlet.* and javax.servlet.http.* import statements give us access to the Java Servlet API set of classes and interfaces
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;

Next section is the heart of this servlet, the implementation of the service method for the handling of the request and response objects of the servlet.
protected void service (HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException {
res.setContentType("text/html");
PrintWriter out = res.getWriter();
out.println("<HTML><TITLE>SimpleHttpServlet</TITLE><BODY>");
out.println("<H2>Servlet API Example - SimpleHttpServlet</H2><HR>");
out.println("<H4>This is about as simple a servlet as it gets!</H4>");
out.println("</BODY><HTML>");
out.close();
}
What the service method does

Let’s examine this service method in more detail. Notice that the method accepts two parameters, HttpServletRequest and HttpServletResponse. The request object contains information about and from the client. In this example, we don’t do anything with the request.

This method is declared Abstract in the basic GenericServlet class, and so subclasses, such as HttpServlet, must override it. In our subclass of HttpServlet, when using this method, we must implement this method according to the signature defined in HttpServlet, namely, that it accepts HttpServletRequest and HttpServletResponse arguments.

We do some handling of the response object, which is responsible for sending our response back to the client. Our response here is a formatted HTML page, so we first set the response content type to text/html by coding res.setContentType("text/html"). Next, we request a PrintWriter object to write text to the response by coding 
PrintWriter out = res.getWriter(). 
We could also have used a ServletOutputStream object to write out our response, but getWriter gives us more flexibility with Internationalization. In either case, the content type of the response must be set before references to these objects can be made.

The remaining out.println statements write our HTML to the PrintWriter, which is sent back to the client as our response. It is pretty simple HTML, so we do not display it here. We use out.close more for completeness, because the Web application server automatically closes the PrintWriter when the service method exits.
Name

ADO,131,ASP,3,C++,61,CORE JAVA,1,CSS,115,HTML,297,index,5,JAVASCRIPT,210,OS,47,PHP,65,SAD,53,SERVLETS,23,SOFTWARE ENGINEERING,245,SQL,71,TCP/IP,1,XHTML,9,XML,18,
ltr
item
Best Online Tutorials | Source codes | Programming Languages: Simple HTTP servlet
Simple HTTP servlet
Best Online Tutorials | Source codes | Programming Languages
https://www.1000sourcecodes.com/2012/05/simple-http-servlet.html
https://www.1000sourcecodes.com/
https://www.1000sourcecodes.com/
https://www.1000sourcecodes.com/2012/05/simple-http-servlet.html
true
357226456970214079
UTF-8
Loaded All Posts Not found any posts VIEW ALL Readmore Reply Cancel reply Delete By Home PAGES POSTS View All RECOMMENDED FOR YOU LABEL ARCHIVE SEARCH ALL POSTS Not found any post match with your request Back Home Sunday Monday Tuesday Wednesday Thursday Friday Saturday Sun Mon Tue Wed Thu Fri Sat January February March April May June July August September October November December Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec just now 1 minute ago $$1$$ minutes ago 1 hour ago $$1$$ hours ago Yesterday $$1$$ days ago $$1$$ weeks ago more than 5 weeks ago Followers Follow THIS PREMIUM CONTENT IS LOCKED STEP 1: Share to a social network STEP 2: Click the link on your social network Copy All Code Select All Code All codes were copied to your clipboard Can not copy the codes / texts, please press [CTRL]+[C] (or CMD+C with Mac) to copy Table of Content