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.

Post Message In servlet

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>
<head>
<title>post servlet</title>
</head>
<body>
<h2>Post Massage</h2>
<form Action="/amar/PostServlet" Method="GET">
<p> Username: <input type="text" name="username" size="20"></p>
<p> Password: <input type="password"  name="password" size="20"></p>
<p> Comment:  <textarea ROWS=COLS=50 height="100" NAME="comment">
</textarea></p><BR>

<input type="submit" VALUE="submit">
<input type="reset" value="reset">
</form>
</body>
</html>

Here is the code of this program:

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;

public class PostServlet extends HttpServlet{
  public void doGet(HttpServletRequest request, 
  HttpServletResponse response
)
   
throws ServletException, IOException {
  response.setContentType("text/html");{
  ServletOutputStream  out = response.getOutputStream();
  try {
  out.println("<html><head><title>" +  "</title></head>");
  out.println("<body><h1>" +  "</h1>");
  String  name = request.getParameter("username" );
  String  password = request.getParameter("password" );
  String  comment = request.getParameter"comment" );
  out.println("Name:" + name + "<BR>");
  out.println("Password:" + password + "<BR>");
  out.println("Comment: " + comment + "<BR>");
  }
  catch(Throwable  t ) {
  out.println("<P><pre>");
  t.printStackTracenew PrintStream(out) );
  out.println ("</pre><P>");
  }
  out.println ("</body></html>");
  }
  }
}

Download of this program:

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.