Latest Tutorials| Questions and Answers|Ask Questions?|Site Map



Home Servlets Post Message In servlet

Related Tutorials


 
 

Share on Google+Share on Google+

Post Message In servlet

Advertisement
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

     

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.

 

Advertisement

If you enjoyed this post then why not add us on Google+? Add us to your Circles



Liked it!  Share this Tutorial


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: July 28, 2007

Related Tutorials

Discuss: Post Message In servlet   View All Comments

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments:1
Manivannan.P
November 19, 2011
spelling mistake

n this example, we are going to implement posting massage to servlet. In the following program, you will learn how to post massage.
DMCA.com