DQ 2 : Code a servlet In practice, all servlets extend the HttpServlet class. To
ID: 3667749 • Letter: D
Question
DQ 2 : Code a servlet
In practice, all servlets extend the HttpServlet class. To extend this class, the servlet must import the java.io, javax.servlet, and javax.servlet.http packages.
The doGet method overrides the doGet method of the HttpServlet class and processes all HTTP requests that use the Get method.
The doPost method overrides the doPost method of the HttpServlet class and processes all HTTP requests that use the Post method.
The doGet and doPost methods use two objects that are passed to it by the web server: (1) the HttpServletRequest object, or the request object, and (2) the HttpServletResponse object, or the response object.
The setContentType method of the response object sets the content type of the response that’s returned to the browser. Then, the getWriter method of the response object returns a PrintWriter object that can be used to send HTML to the browser.
Before you can create a PrintWriter object, you must set the content type. This allows the getWriter method to return a PrintWriter object that uses the proper content type.
As class, discuss examples of servlets. Make sure your post is at least 250 words.
Citation/Reference requirements: In addition to your text book, include at least one additional high quality reference (examples: IEEE Journals, or most Journal publications).
Explanation / Answer
HelloWorld.java
----------------
This is a Servlet class which has doGet and doPost method . based on the request from Browser the corresponding
method will get executed .
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class HelloWorld extends HttpServlet {
private static final long serialVersionUID = 1L;
@Override
public void init() throws ServletException {
super.init();
// You can initilize your servlet here in this section of method.
}
@Override
protected void doGet(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("<h1> Welcome User: Hello World </h1>");
}
@Override
protected void doPost(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
}
@Override
public void destroy() {
super.destroy();
//Here you can release the reources
}
}
This is the Cinfiguration File where you need to define the Servlet class and Servlet mapping which in our
Case is HelloWorld.
WEB.xml
------------
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
id="WebApp_ID" version="3.0">
<display-name>HelloWorldt</display-name>
<servlet>
<servlet-name>HelloServlet</servlet-name>
<servlet-class>com.javatechig.HelloWorld</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>HelloWorld</servlet-name>
<url-pattern>/HelloWorld</url-pattern>
</servlet-mapping>
</web-app>
Run the Program on Server like TomCat or Jboss.
Below Output will come in Browser:
Welcome User: Hello World
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.