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.
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);
}
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.
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:
Quote:
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)
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>");
}
}
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);
}