...
- Use a
ResourceBundle
. See the Java docs for the specifics of how theResourceBundle
class works. Using this method, the properties file must go into theWEB-INF/classes
directory or in a jar file contained in theWEB-INF/lib
directory. - Another way is to use the method
getResourceAsStream()
from theServletContext
class. This allows you update the file without having to reload the webapp as required by the first method. Here is an example code snippet, without any error trapping:No Format // Assuming you are in a Servlet extending HttpServlet // This will look for a file called "/more/cowbell.properties" relative // to your servlet Root Context InputStream is = getServletContext().getResourceAsStream("/more/cowbell.properties"); Properties p = new Properties(); p.load(is); is.close();
How do I use log4j for all Tomcat log output?
...
Thus, if you have JNI code that follows the convention of including a static initilaizer like this:
No Format |
---|
class FooWrapper { static { System.loadLibrary("foo"); } native void doFoo(); } |
then both this class and the shared library should be placed in the $CATALINA_HOME/shared/lib
directory.
...
No Format |
---|
java.lang.UnsatisfiedLinkError: Native Library WEB-INF/lib/libfoo.so already loaded in another classloader
at java.lang.ClassLoader.loadLibrary0(ClassLoader.java:1525) |
...
No Format |
---|
<html>
<head>
<meta http-equiv="refresh" content="0;http://mydomain.com/some/path/to/servlet/homepage/">
</head>
<body>
</body>
</html>
|
This change takes effect immediately and does not require a restart of Tomcat.
How do I redirect System.out and System.err to my web page?
I have met a situation where I needed to redirect a portion of standard ouput (System.out
, STDOUT) and standard error (System.err
, STDERR) to my web page instead of a log file. An example of such an application is a compiler research platform that our resarch team is putting online for anybody to be able to quickly compile-test their programs on line. Naturally, the compilers dump some of their stuff to STDERR or STDOUT and they are not web application .jar
. Thus, I needed badly these streams related to the compiler output to be redirected to my web editor interface. Having found no easy instructions on how to do that lead me writing up this quick HOWTO. The HOWTO is based on Servlets, but similar arrangements can be done for JSPs. The below example shows the essentials, with most non-essentials removed.
No Format |
---|
public class WebEditor
extends HttpServlet
{
...
public void doGet
(
HttpServletRequest poHTTPRequest,
HttpServletResponse poHTTPResponse
)
throws IOException, ServletException
{
poHTTPResponse.setContentType("text/html");
ServletOutputStream out = poHTTPResponse.getOutputStream();
out.println("<html>");
out.println("<body>");
out.println("<head>");
out.println("<title>WebEditor Test $Revision: 1.6 $</title>");
out.println("</head>");
out.println("<body>");
out.println("<h3>WebEditor Test $Revision: 1.6 $</h3>");
out.println("<hr />");
// Backup the streams
PrintStream oStdOutBackup = System.out;
PrintStream oStdErrBackup = System.err;
// Redired STDOUT and STDERR to the ServletOuputStream
System.setOut(new PrintStream(out));
System.setErr(new PrintStream(out));
try
{
// ... call compiler here that produces
// tons of STDOUT/STDERR messages ...
}
catch(Exception e)
{
out.println(e.toString());
}
// Restore original STDOUT and STDERR
System.setOut(oStdOutBackup);
System.setOut(oStdErrBackup);
out.println("<hr />");
out.println("</body>");
out.println("</html>");
}
}
|
A few caveats arise, as for instance while the System.out
and System.err
are deirected as per above, no logging of these is done to files. You will need more legwork to do to make the additional logging. It is important to backup and restore the original streams as the above example does. Also, notice the use of getOutputStream()
:
when this method is called, the getWriter()
method can no longer be used in the same response object.
Corrections and comments are most welcome!