Post Message In servlet
In this example, we are going to implement posting massage to servlet. In the following program, you will learn how to post massage.
Code Description:
The following program uses getOutputStream() method. This is the method that defines an object to assist a servlet in sending a response to the client . The servlet container creates a ServletResponse object and passes it as an argument to the servlet's service method. ServletOutputStream is a constructor. This constructor provides an output stream for sending data to the client. A ServletOutputStream object is created using response.getOutputStream() method. The servlet extends the HttpServlet and overrides the doGet() method which is inherited from HttpServlet class. The server invokes doGet() method whenever web server receives the GET request from the servlet.
In this example we are going to make one html in which we post the massage given by user. The controller will check if the username , password and comment entered by the user is blank then servlet will display massage null, if the username, password and comment entered by the user are not blank then servlet will display the massage entered by the user.
Html file for this program:
<html>
|
Here is the code of this program:
import java.io.*;
|
xml file of this program.
<?xml version="1.0" encoding="ISO-8859-1"?> <!--<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd"> --> <web-app> <servlet> <servlet-name>amar</servlet-name> <servlet-class>PostServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>amar</servlet-name> <url-pattern>/PostServlet</url-pattern> </servlet-mapping> </web-app> |
Output of this program.