How to define servlet filter order of execution using annotations in WAR

You can indeed not define the filter execution order using @WebFilter annotation. However, to minimize the web.xml usage, it’s sufficient to annotate all filters with just a filterName so that you don’t need the <filter> definition, but just a <filter-mapping> definition in the desired order. For example, @WebFilter(filterName=”filter1″) public class Filter1 implements Filter {} @WebFilter(filterName=”filter2″) … Read more

HTTP request parameters are not available by request.getAttribute()

Here, String url = (String) request.getAttribute(“url”); you’re trying to get a request parameter as a request attribute instead of as a request parameter. This will obviously not do what you want. You need to get a request parameter as a request parameter, not as a request attribute. String url = request.getParameter(“url”); Unrelated to the concrete … Read more

How do I execute multiple servlets in sequence?

Use RequestDispatcher#include() on an URL matching the url-pattern of the Servlet. public class Populate_ALL extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType(“text/plain”); request.getRequestDispatcher(“/populateServlet1”).include(request, response); request.getRequestDispatcher(“/populateServlet2”).include(request, response); request.getRequestDispatcher(“/populateServlet3”).include(request, response); //… } } Note: if those servlets cannot be used independently, then this is the wrong approach and you should be … Read more

HTTP Status 405 – HTTP method is not supported by this URL

This is the default response of the default implementation of HttpServlet#doXxx() method (doGet(), doPost(), doHead(), doPut(), etc). This means that when the doXxx() method is not properly being @Overriden in your servlet class, or when it is explicitly being called via super, then you will face a HTTP 405 “Method not allowed” error. So, you … Read more

java.lang.ClassNotFoundException: HttpServletRequest

Your web app has servlet container specific libraries like servlet-api.jar file in its /WEB-INF/lib. This is not right. Remove them all. The /WEB-INF/lib should contain only the libraries specific to the Web app, not to the servlet container. The servlet container (like Tomcat) is the one who should already provide the servlet container specific libraries. … Read more

Submitting form to Servlet which interacts with database results in blank page

You need to properly handle exceptions. You should not only print them but really throw them. Replace } catch (Exception e) { e.printStackTrace(); // Or System.out.println(e); } by } catch (Exception e) { throw new ServletException(“Login failed”, e); } With this change, you will now get a normal error page with a complete stacktrace about … Read more

Where to place and how to read configuration resource files in servlet based application?

It’s your choice. There are basically three ways in a Java web application archive (WAR): 1. Put it in classpath So that you can load it by ClassLoader#getResourceAsStream() with a classpath-relative path: ClassLoader classLoader = Thread.currentThread().getContextClassLoader(); InputStream input = classLoader.getResourceAsStream(“foo.properties”); // … Properties properties = new Properties(); properties.load(input); Here foo.properties is supposed to be placed … Read more