Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

*Originally taken from: http://wiki.apache.org/jakarta-tomcat/Tomcat/Howto*

...

Contents TableOfContents

...

How do I add a question to this page?

Anyone may edit this page to add their own content. That is why this page is part of a Wiki and not a hardcoded static file in the FAQ.

Wiki Markup
However, _do not_ add questions without answers to this page. If you have a question about how to do something in Tomcat which has not been addressed yet, ask the \[http://jakarta.apache.org/tomcat/faq/tomcatuser.html tomcat-user list\]. Once you've figured out how to fix your problem, come back and update the Wiki to allow the rest of us to benefit from what you've learned!

How do I set up and run Tomcat on Macintosh OS X?

Wiki Markup
See \["TomcatOnMacOS"\]

How do I load a properties file?

Here are the two most popular ways::

  • Use a ResourceBundle. See the Java docs for the specifics of how the ResourceBundle class works. Using this method, the properties file must go into the WEB-INF/classes directory or in a jar file contained in the WEB-INF/lib directory.
  • Another way is to use the method getResourceAsStream() from the ServletContext 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
        // 

Can non-root users start/stop TOMCAT

Is there a way to allow normal user(non-root) to start/stop the tomcat server. Tried assigning permision, din work. Read thru some articles, stated that only root has permission to port below 1025. How can i allow a non-root user to do so ? thks in adv. (smile)

  • malathi ranjit singh----

*Originally taken from: http://wiki.apache.org/jakarta-tomcat/Tomcat/Howto*

Contents TableOfContents

How do I add a question to this page?

Anyone may edit this page to add their own content. That is why this page is part of a Wiki and not a hardcoded static file in the FAQ.

Wiki Markup
However, _do not_ add questions without answers to this page. If you have a question about how to do something in Tomcat which has not been addressed yet, ask the \[http://jakarta.apache.org/tomcat/faq/tomcatuser.html tomcat-user list\]. Once you've figured out how to fix your problem, come back and update the Wiki to allow the rest of us to benefit from what you've learned!

How do I set up and run Tomcat on Macintosh OS X?

Wiki Markup
See \["TomcatOnMacOS"\]

How do I load a properties file?

Here are the two most popular ways::

  • Use a ResourceBundle. See the Java docs for the specifics of how the ResourceBundle class works. Using this method, the properties file must go into the WEB-INF/classes directory or in a jar file contained in the WEB-INF/lib directory.
  • Another way is to use the method getResourceAsStream() from the ServletContext 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();  


...

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.setErr(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.

...
		}
		catch(Exception e)
		{
			out.println(e.toString());
		}

		// Restore original STDOUT and STDERR
		System.setOut(oStdOutBackup);
		System.setErr(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!

Can non-root users start/stop TOMCAT

Is there a way to allow normal user(non-root) to start/stop the tomcat server. Tried assigning permision, din work. Read thru some articles, stated that only root has permission to port below 1025. How can i allow a non-root user to do so ? thks in adv. (smile)

  • malathi ranjit singh----

One way is to put Apache httpd with mod_jk before your Tomcat servers, and use ports >=1024 in the Tomcat(s). Since we do it that way, it's the only way I know.

BTW, you should read the 1st howto. (wink)Corrections and comments are most welcome!