Put your text ad here
WestNIC provides reliable web hosting services
Web hosting directory, find affordable web hosting
Fastwebhost offers cheap web hosting & reseller hosting services
This FAQ is part of the Code Style Help and FAQ section. Join our premium content service for full access all FAQ answers.
/examples/WEB-INF/classes/HelloWorld!
if and else code is executed!
getRemoteUser method returns null after basic authentication!
getWrite()!
Exception!
IndexOutOfBoundsException... ?
AccessControlException!
IllegalStateException with my forward() call!
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?
/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.
… full answer hidden
Click for full answers to FAQs
Premium members click below for full answer
I get a servlet 404 error at /examples/WEB-INF/classes/HelloWorld!
if and else code is executed!
A: The code sample you have given is correct. Where you have the if / else conditions the servlet should execute the contents of the relevant statement block, between the curly braces, and not the other. It seems most likely that this working version of the controller servlet has not successfully been deployed to the servlet container and you are actually testing an earlier version of the servlet.
When you are working with servlets it helps to have a standard deployment process that ensures that the latest version of your code is successfully compiled and deployed before you test. Ideally you should write unit tests to check the logic of your application too. Apache Ant is a Java build and deployment application that works with JUnit to test your application code.
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.
… full answer hidden
Click for full answers to FAQs
Premium members click below for full answer
My servlet cannot locate my XSLT file!
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.
… full answer hidden
Click for full answers to FAQs
Premium members click below for full answer
The getRemoteUser method returns null after basic authentication!
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.
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.
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.
… full answer hidden
Click for full answers to FAQs
Premium members click below for full answer
Could compiling a package cause an internal server error?
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.
… full answer hidden
Click for full answers to FAQs
Premium members click below for full answer
I get "HTTP GET method is not in use"!
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.
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.
A: It is difficult to solve Java servlet errors without more detailed diagnostic information but the most likely reason for this error is a class definition not found. In other words, your servlet class or one of the classes required by your servlet was not found by the servlet container when it tried to bring the servlet into service.
The most likely solution is to re-compile your servlet and associated classes and re-deploy them to your servlet container. If you use third party libraries with your servlet, make sure they are deployed to the appropriate Web application directory or common container library directory. You may need to stop and re-start your servlet container.
A build tool like Apache Ant can help ensure that you do not miss vital steps in the compilation and deployment of your Web application.
IllegalStateException with my forward() call!
A: The RequestDispatcher forward() method call will effectively end the servlet response because the response stream will be committed in the process, but the controller servlet will continue to execute any remaining statements in its doGet() or doPost() method. This can be useful if you want to log status or store data after the response has been handled for example, but if those statements attempt to make further calls on the servlet response it will throw an IllegalStateException. For this reason it is best to handle forwarding logic with exclusive if / else blocks, to be sure only one response is attempted.
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.
… full answer hidden
Click for full answers to FAQs
Premium members click below for full answer
Internet Explorer does not show my custom 500 error page!
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.
A: Web server errors caused by Java servlets are the responsibility of the Web site owner and you should alert them to the problem. Server errors can be caused by temporary problems with a Web server or there may be fundamental programming errors that are only seen in certain circumstances. It is possible that those problem circumstances include unexpected differences in the way your Web browser works or your computer is set up. Most Web site managers would welcome the opportunity to make a correction if you can provide information about the browser you use and what happened immediately before the error.
One possibility is that your Web browser has an exceptionally long "user agent", a software code name that is a hidden part of your browser's Web page requests. Your browser's code name is 2063 characters long, which may be more than the servlet expects. Use the Code Style EchoRequest servlet to see the user-agent for yourself. Try the problem page with a different Web browser to see if you can isolate the problem.
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.
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.
| Front-end FAQs | Back-end FAQs | Learn Java |
|---|---|---|
About us: site help, text ads and premium content FAQs.