I learn jsp using netbeans 8 and GlassFish Server. I have MyLog.java
package MyClass;
import java.util.Date;
import java.text.SimpleDateFormat;
import java.io.FileWriter;
import java.io.PrintWriter;
import java.io.IOException;
public class MyLog {
private static final SimpleDateFormat TIME_FMT= new SimpleDateFormat("yyyy-MM-dd HH:mm:ss:SSS");
private static PrintWriter log=null;
public MyLog(String logpath) throws IOException{
log=new PrintWriter(new FileWriter(logpath,true));
}
public static synchronized void println(String s)
{
log.println(TIME_FMT.format(new java.util.Date())+" - "+s);
log.flush();
}
public static synchronized void close(){
log.close();
}
}
and my class usage is
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@page import="MyClass.*" %>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Switch the log ON</title>
</head>
<body>
<%
MyLog log =(MyLog) application.getAttribute("logfile");
if (log==null)
{
try{
log=new MyLog("log/mylog.log");
application.setAttribute("logfile", log);
log.println("Logging enabled");
out.println("Logging enabled");
}
catch (Exception e) {
out.println(e.getMessage());
}
}
else
{
log.println("Attempt to enable logging");
out.println("Logging was already enabled");
}
%>
</body>
</html>
so i get error that couldn't found file "log/mylog.log".
I created log/mylog.log (with directory) in web folder and even in application folder, but it cannot find this file.
I tried to use
new File (filename).exists () in debug to my existing files but it always gives false.
So my question is, where I should place my files so that netbeans (or glassfish) could find it?
The look-up is always done in your current working directory which once your app is deployed in a container depends on the target application server. But, even if found, you really can't depend on that or you'll app will not remain portable anymore.
So, you can move your log
directory inside the WEB_INF
directory of your web application and access the absolute directory location at runtime through the ServletContext
like
log = new MyLog(application.getRealPath("/WEB-INF/log/mylog.log"));
Warning: You must store your log
directory inside WEB-INF
or they'll become accessible through the browser for the whole world wide web to see.
Another alternative if you wouldn't want your log files to go inside the web app's directory or you have some other central location for all logs on that server is to configure any absolute directory location you like as a context parameter in your web.xml
.
<web-app>
...
<context-param>
<param-name>LogPath</param-name>
<param-value>/absolute/log/dir/path/file.log</param-value>
</context-param>
The value can then be accessed through ServletContext
again.
log = new MyLog(application.getInitParam("LogPath"));
For casual logging, another alternative would be to just use the logging capabilities that come with the Servlet spec already implemented by your container. The below will log an error message and the stack trace of the associated exception to a log file, the name and location of which will depend on your servlet container but will be available in its documentation.
application.log("Error message", throwable);
On Tomcat, the logs should go to ${TOMCAT_HOME}/logs/catalina.out
by default.
There's no virtual path here as such. Both the paths are relative; with getRealPath()
you're just resolving that relativeness (if that's a word) against you web application's root directory. With your approach too the relative path was getting resolved but against your app server's current working directory. It's just that you weren't able to find where it is. It's not a fixed location and changes with different app servers or even different versions of the same server.