Your banner ad here

WestNIC provides reliable web hosting services

25% off cpanel web hosting and reseller hosting deals. Promo: codestyle25off

Site navigation below

This FAQ is part of the Code Style Help and FAQ section. Join our premium content service for full access all FAQs and more.

RSS news feed Get the latest answers in this FAQ

Follow the Code Style Twitter feed for free one-to-one help sessions by instant messenger: MSN, AIM and Yahoo! Messenger. Sessions are held most Saturdays. Join the Twitter feed then check your direct messages for details.

Servlet problems

Q: My custom error page doesn't show!

A: There are many things that could be wrong with your configuration. What happens when you force the error, which error page is issued? What does the error log tell you? Is the new configuration being deployed properly, to replace the previous one?

Check the webapps directory for the deployed web.xml file. Does it include your custom error configuration? Check the work directories under the Tomcat installation to see the which compiled JSP servlets are present. Perhaps there is a compilation error with your custom error page?

Actions: Follow-up, clarify or correct this answer. Submit a new question.

Q: I get a servlet 404 error at /examples/WEB-INF/classes/HelloWorld!

A: It appears you have deployed your servlet to the correct location, but you have probably not configured the servlet in the web.xml file for your application and you are requesting the wrong URL for the servlet. You should add web.xml entries like those below, restart and request the URL http://localhost:8080/examples/HelloWorld.

Premium Content: Follow this link for subscription information More details available to premium content service subscribers:
I get a servlet 404 error at /examples/WEB-INF/classes/HelloWorld!

Actions: Follow-up, clarify or correct this answer. Submit a new question.

Q: My servlet cannot locate my XSLT file!

A: On Windows systems, the path to files referenced in servlet classes should be given in full, including the drive letter and the path separator backslashes escaped.

Premium Content: Follow this link for subscription information More details available to premium content service subscribers:
My servlet cannot locate my XSLT file!

Actions: Follow-up, clarify or correct this answer. Submit a new question.

Q: The getRemoteUser method returns null after basic authentication!

A: Basic authentication details are carried in the HTTP headers that a browser passes to the servlet container when it makes a request. When the browser makes an initial request, it does not include an Authorization header, so the first servlet request.getRemoteUser() method returns null.

Your servlet must trigger the process by which the browser prompts for the login. The browser will then issue a second HTTP request with an Authorization header that carries the login details.

Premium Content: Follow this link for subscription information More details available to premium content service subscribers:
The getRemoteUser method returns null after basic authentication!

Actions: Follow-up, clarify or correct this answer. Submit a new question.

Q: I get "cannot find symbol" with getWrite()!

A: The HttpServletResponse class method for getting a PrintWriter is called getWriter(), not getWrite(), and may throw an IOException. The method may also throw the runtime exception UnsupportedEncodingException if the character set specified by the setContentType() method is not supported, and IllegalStateException if the getOutputStream() method has already been called.

Actions: Follow-up, clarify or correct this answer. Submit a new question.

Q: I get "cannot override doGet()" throwing an Exception!

A: This compiler error means that your method throws an exception that is not declared by the superclass method it is intended to override. The servlet doGet() method may throw a ServletException, an IOException or any subclass of those types. Since Exception is a superclass of the declared exceptions, it is not permitted.

If your doGet() method includes calls that may throw undeclared exceptions you should catch those exceptions and handle them. For example, you may report the problem in the servlet output, fall back to a simpler output or fixed error response, or throw a new ServletException with the details of the primary exception. If the exception originates from invalid servlet input, you may choose to send an HTTP 400, bad request, error response.

Actions: Follow-up, clarify or correct this answer. Submit a new question.

Servlet exceptions

Q: Could compiling a package cause an internal server error?

A: Possibly, but compiling a package wouldn't normally cause a 500 error directly. If you attempted to compile a servlet or a utility class on the host machine and the classpath environment setting was not configured properly, the classes would just fail to compile.

Premium Content: Follow this link for subscription information More details available to premium content service subscribers:
Could compiling a package cause an internal server error?

Actions: Follow-up, clarify or correct this answer. Submit a new question.

Q: What does this servlet stack trace mean: IndexOutOfBoundsException... ?

A: The answer to the question is in the first line of the stack trace. The interpreter attempted to get the array list item at index 4, but the size of the array list was zero. Whatever code was supposed to fill the list has not done so. To guard against this case, you should add a check for the size of the array before you reference an item at a specific index. This may be a symptom of concurrent modification of the array list by a separate thread.

Actions: Follow-up, clarify or correct this answer. Submit a new question.

Q: My file throws an AccessControlException!

A: The AccessControlException explains the problem without having to look any code extracts. The user profile that the servlet container is using does not have permission to access this file. Modify the permissions on the file to allow read access by the servlet container user if one exists. Otherwise, there may be a security policy for your servlet container that restricts the rights your applications have to access the file system. Check your servlet container documentation and adjust the configuration as necessary. The container may be configured with strict security policy by default.

Actions: Follow-up, clarify or correct this answer. Submit a new question.

Q: I get "HTTP GET method is not in use"!

A: The servlet you are referring to has not implemented the doGet(HttpServletRequest, HttpServletResponse) method, which is required to handle the type of requests made by typing the address into a Web browser. It is possible the servlet has been designed to handle HTTPpost requests from HTML form submissions only. In this case, you would have to create a form whose action attribute is the servlet URL with the method attribute post, as below.

Premium Content: Follow this link for subscription information More details available to premium content service subscribers:
I get "HTTP GET method is not in use"!

Actions: Follow-up, clarify or correct this answer. Submit a new question.

HTTP status errors

Q: Internet Explorer does not show my custom 500 error page!

A: The problem is with your browser, not your servlet container. Internet Explorer shows what it regards as "friendly" error messages when it receives HTTP error codes such as 500, 404, etc. This is a problem because it may obscure any genuinely helpful error messages you try to present to the user.

Premium Content: Follow this link for subscription information More details available to premium content service subscribers:
Internet Explorer does not show my custom 500 error page!

Actions: Follow-up, clarify or correct this answer. Submit a new question.

Q: My 500 error is java.lang.Exception!

A: It is very difficult to tell from this information alone what the problem is. The java.lang.Exception class is the superclass of all exceptions, so we cannot tell whether it is a runtime exception or checked exception thrown by your application. You should put debugging log output in your servlet to find the point at which the exception is thrown. Use the servlet method log(String) or log(String, Throwable).

If you have overridden the init(ServletConfig config) method, you must call super.init(config) in the first line of the method to enable these log methods.

Actions: Follow-up, clarify or correct this answer. Submit a new question.

Q: Why does my servlet give a 404 error?

A: There are many reasons why a servlet container may issue an HTTP 404 error for a servlet. You should check you have added a servlet configuration and mapping to your web.xml file and make sure you are requesting the URL path specified in the mapping.

If your servlet compiles successfully, another possibility is that it throws a ServletException in the init(ServletConfig) method. If so, the servlet container will log the exception and take the servlet out of service. Check your log files for any details.

Actions: Follow-up, clarify or correct this answer. Submit a new question.

Q: What does HTTP status 405, method not supported mean?

A: The HTTP 405 status, "Method not supported", means that the submitted request method is not implemented by the servlet that handled the response. For example, a servlet may only have a doGet() method, not doPost(), and therefore be unable to issue an HTML document for the response body. In other words, the response means you can't make that kind of request for this servlet.

One common technique to easily enable a servlet to support POST requests is for the doPost() method to call the doGet() method, though some the HttpServletRequest object properties will vary from a normal GET request.

Actions: Follow-up, clarify or correct this answer. Submit a new question.

Add this page to your chosen social bookmarking service

Style warning - please read

Home · CSS · Java · Javascript · HTML · Help · Log