Sambar Server Documentation

Servlets FAQ
Pro Server Only


Should the URL path be "servlets" or "servlet" ?
How can I get the name of the currently executing script ?
How can I debug my servlet ?

Should the URL path be "servlets" or "servlet" ?
The directory for your code is "servlets":
c:\sambar43\servlets\Hello.class

Whereas, the URL path uses "servlet" as the default alias for identifying servlets:

http://localhost/servlet/Hello

How can I get the name of the currently executing script ?
Use req.getRequestURI() or req.getServletPath(). The former returns the path to the script including any extra path information following the name of the servlet; the latter strips the extra path info.

How can I debug my servlet ?
A few IDEs support servlet debugging (such as Symantec Cafe). Beyond these, the most important thing you can do is to always catch your exceptions. An uncaught exception can terminate your servlet, and write a minimal error the the server.log file.

The following code sets up a catch block that will trap any exception, and print its value to standard error output and to the ServletOutputStream so that the exception shows up on the browser. (Note: If you see "Compiled Code" instead of line numbers in the exception stack trace, then turn off the JIT in the server.)

res.setContentType("text/html");
ServletOutputStream out = res.getOutputStream();
try 
{
	// do your thing here
	...
}
catch (Exception e) 
{
	StringWriter sw = new StringWriter();
	PrintWriter pw = new PrintWriter(sw);
	e.printStackTrace(pw);	
	out.println("<pre>");
	out.print(sw.toString());
	out.println("</pre>");		
}

© 1999 Sambar Technologies. All rights reserved. Terms of Use.