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



Home Servlets Context Log Example Using Servlet

Related Tutorials


 
 

Share on Google+Share on Google+

Context Log Example Using Servlet

Advertisement
This example illustrates about how to use of Context Log in servlet. Context Log is used to write specified message to server log file when servlet is called.

Context Log Example Using Servlet

     

This example illustrates about how to use of Context Log in servlet. Context Log is used to write specified message to server log file when servlet is called. In the following JSP page (message.jsp) we have simply taken a text area where user give his/her message and post the form. After posting the form, the servlet ContextLogExample is called. Source code of the message.jsp is given below:

 

 

 

Source code of message.jsp:

<%@page language="java" session="true" contentType="text/html;charset=ISO-8859-1"%> 
<br>
<form name="frm" method="post" action=../ContextLogExample>
  <table border = "0">
  <tr align="left" valign="top">
  <td>Give your Message:</td>
  </tr>
  <tr>
  <td><TEXTAREA NAME="message" COLS=30 ROWS=6></TEXTAREA></td>
  </tr>
  <tr align="left" valign="top">
  <td><input type="submit" name="submit" value="submit"/></td>
  </tr>
  </table>
</form>

Running the jsp page (message.jsp) on this url: http://localhost:8080/JavaExample/JSP/message.jsp 
the will displays as below:

In the following servlet  (ContextLogExample) we get parameter of jsp page in "message" variable and set this message to the log file by log() method of ServletContext interface

The Source code of ContextLogExample.java is given below:

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

public class ContextLogExample extends HttpServlet {
  public void doPost(HttpServletRequest request, 
HttpServletResponse response
)
 
throws ServletException, IOException{
  response.setContentType("text/html");
  PrintWriter out = response.getWriter();
  
  String message = request.getParameter("message");
  ServletContext context = getServletContext();
  if(message == null || message.equals("")){
  context.log("No message received:"
new 
IllegalStateException("Parameter not Found"));
  }else{
  context.log("Parameter Found: Successfully 
received your message: " 
+ message);
  }
  out.println("<html><head><title>Context Log 
Example</title></head><body>"
);
  out.println("<h2><font color='green'>Successfully 
send your Message</font></h2>"
);
  out.println("</body></html>");
  
}

Mapping of servlet (ContextLogExample.java) in web.xml

<servlet>
  <servlet-name>ContextLogExample</servlet-name>
  <servlet-class>ContextLogExample</servlet-class>
</servlet> 
<servlet-mapping>
  <servlet-name>ContextLogExample</servlet-name>
  <url-pattern>/ContextLogExample</url-pattern>
</servlet-mapping>

User enters the  message in the text area that is sent to the servlet (ContextLogExample)

Servlet sets the message in the log file which is shown like below:

Download Source Code

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 5, 2008

Related Tutorials

Discuss: Context Log Example Using Servlet   View All Comments

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments:0
DMCA.com