Results 1 to 6 of 6

Thread: Logging a Message in a Servlet

  1. #1
    Join Date
    Nov 2009
    Posts
    140

    Logging a Message in a Servlet

    Hello, I am learning java programming and I have come to know that you will able to log a message in java. I want to perform Logging a Message in a Servlet. If anyone knows how to achieve it, then please help me to perform it, I will be thankful to you. So, please help me to get it.

  2. #2
    Join Date
    Apr 2008
    Posts
    2,005

    Re: Logging a Message in a Servlet

    Hello, just make use of the code below and get your problem solved. I think from the code below you will able to get the basic idea behind it.
    Code:
    getServletContext().log("Message for logging");
    try 
    {
     } 
    catch (Exception e) 
    {
    getServletContext().log("Message In Exception ", e);
    }

  3. #3
    Join Date
    May 2008
    Posts
    2,297

    Re: Logging a Message in a Servlet

    Hello, I think you must need to make use of the object of the servlet context class and by using that object you can simply able to use the method context.log() which is helpful to you for writing the message for your servlet log file. So, you just need to frame such type of program and then you can able to get the solution for your problme. So, just make use of this method in your code and get your problem solved. In that you can make use of the name and type of the servlet log file which is specific to the servlet container.

  4. #4
    Join Date
    May 2008
    Posts
    2,389

    Re: Logging a Message in a Servlet

    Hey, I have got the log file which is created in tomcat from the logging a message in a servlet. It is having logging message as below:
    COPY
    2010-20-02 05:53:54 A message 2002-20-02 05:53:54 An exception occurred java.io.IOException at com.mycompany.MyServlet.doGet(Unknown Source) at javax.servlet.http.HttpServlet.service(HttpServlet.java:740) at javax.servlet.http.HttpServlet.service(HttpServlet.java:853)

  5. #5
    Join Date
    Oct 2005
    Posts
    2,393

    Re: Logging a Message in a Servlet

    Source code provided below will provide you Logging a Message in a Servlet:
    Code:
    import java.io.IOException;
    import javax.servlet.ServletConfig;
    import javax.servlet.ServletContext;
    import javax.servlet.ServletException;
    import javax.servlet.ServletOutputStream;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    
    public class TestServlet extends HttpServlet
     {
    private ServletContext SC;
    public void init(ServletConfig Sconfig) throws ServletException
     {
        super.init(Sconfig);
        SC = getServletContext();
        SC.log("Hello!");
      }
    public void doGet(HttpServletRequest request, HttpServletResponse response)throws IOException 
    {
        ServletOutputStream servletoutputstream = response.getOutputStream();
        SC.log("Message Written");
        response.setContentType("text/html");
        servletoutputstream.println("<html><head><title>Log in ServletContext</title></head>");
        servletoutputstream.println("<body>Go to the tomcat-home\\logs and open the xx file to see your log entries");
        servletoutputstream.println("</body></html>");
      }
    }

  6. #6
    Join Date
    Feb 2008
    Posts
    1,852

    Re: Logging a Message in a Servlet

    Hello, I think you can perform this simply with the function as given below:
    Code:
    public void log(String message, int Message) 
    {
       if (debug >= Message)
          log(message);
    }

Similar Threads

  1. Replies: 5
    Last Post: 11-06-2011, 10:17 PM
  2. Replies: 3
    Last Post: 07-02-2011, 01:57 PM
  3. While logging in Microsoft office error message is comes
    By Nrithya in forum Windows Software
    Replies: 3
    Last Post: 07-01-2011, 03:29 PM
  4. Replies: 5
    Last Post: 19-08-2010, 02:22 PM
  5. Error message while logging off
    By Asuman in forum Operating Systems
    Replies: 3
    Last Post: 06-03-2009, 11:16 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,714,196,666.14698 seconds with 17 queries