Home | Ajax | BioInformatics | Dojo | EAI | EJB | Hibernate | J2ME | Java | Java Glossary | Java Servlets | JavaScript | Jboss | JDBC | JDO | Jmeter | JSF | JSP | JUnit | Maven | MySQL | Spring Framework | SQL | Struts | Technology | WAP | Web Services | XML


 
  
 
Programming Tutorials: Ajax | Articles | JSP | Bioinformatics | Database | Free Books | Hibernate | J2EE | J2ME | Java | JavaScript | JDBC | JMS | Linux | MS Technology | PHP | RMI | Web-Services | Servlets | Struts | UML
 

 
Facing Programming Problem?
Ask Questions?, Browse Latest Questions, Question-Answer Guidelines
Java Servlets
  JDO Tutorials
  EAI Articles
  Struts Tutorials
  Java Tutorials
  Java Certification
  Java Applet
Questions
Comments

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

                         

Leave your comment:

Name:

Email:

URL:

Title:

Comments:


Enter Code:

Audio Version
Reload Image
 

Note: Emails will not be visible or used in any way, and are not required. Please keep comments relevant. Any content deemed inappropriate or offensive may be edited and/or deleted.

No HTML code is allowed. Line breaks will be converted automatically. URLs will be auto-linked. Please use BBCode to format your text.

Add This Tutorial To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Latest Searches:
Convert Integer to Flo
create du table
struts application fro
JSP scriptlets
how to retrieve databa
jsf selectitems
jsp laglib
J2ee Tutorials
array java
converting a floating
java how to get hostna
struts2 enum
Photoshop Animation Gr
Photoshop Digital Art
Jini and JavaSpaces: t
crud application using
program in java to dis
Write a Java program t
java bean
Write a java program t
header footer menu bod
edit xml
database operations wi
remove rectangle
java ChecksumCRC32 ja
redirect
max chars 500 chars i
Open Source E-mail
goto statment
myfaces tomahawk
Dâ??аâ?¬â?£?â??аâ?¬â
string variable
read hexadecimal numbe
appointment
infix to posfix
difference between Byt
joining points
Photoshop Effects Ener
jQuery to Image Scroll
j2se faq
json lib/componen...]=
Create XML file from f
jsp:usebean
how to insert data int
convert linked list to
XML document java
draw triangle
json lib/componen...]=
jdk1.6.0_07
Convert a String into
jsp bean get property
html grids
Merge Sort In Javascri
Photoshop Effects Blue
Make a Slicing effect
jpanel
jfreechart
group
how to create tree str
retreiving multiple Fi
field validator
bubble sort code
code for a web applica
database with procedur
���?����
table model
string array add eleme
uploading struts
create new set in java
how to input 2 xml fil
automative mailer page
how to pass same varia
difference between exe
flex
jboss connection pool
Premiere Video Capturi
data base sech in jsp
netbeans hibernate
covert uppercase to lo
Get Calling Class
Java String toLowerCase Example
Java String toCharArray Example
Java String substring Example
Java String indexOf Example
Java String startsWith Example
Java String hashCode Example
Java String matches Example
Java String length Example
Java String lastIndexOf Example
Java String isEmpty Example
Java String equalsIgnoreCase Example
Java String equals Example
Java String endsWith Example
Java String copyValueOf Example
Java String contentEquals Example
  EAI Articles
  Java Certification
Tell A Friend
Your Friend Name
Search Tutorials

 

 
 
Browse all Java Tutorials
Java JSP Struts Servlets Hibernate XML
Ajax JDBC EJB MySQL JavaScript JSF
Maven2 Tutorial JEE5 Tutorial Java Threading Tutorial Photoshop Tutorials Linux Technology
Technology Revolutions Eclipse Spring Tutorial Bioinformatics Tutorials Tools SQL
 

Home | JSP | EJB | JDBC | Java Servlets | WAP  | Free JSP Hosting  | Search Engine | News Archive | Jboss 3.0 tutorial | Free Linux CD's | Forum | Blogs

About Us | Advertising On RoseIndia.net  | Site Map

India News

Indian Software Development Company | iPhone Development Company in India | Java Training Delhi | Java Training at Noida |

Send your comments, Suggestions or Queries regarding this site at roseindia_net@yahoo.com.

Copyright © 2008. All rights reserved.