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.*;
|
Mapping of servlet (ContextLogExample.java) in web.xml
<servlet> |
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: