Java EE

Throwing custom exception in servlet

Suppose we want to throw ‘InvalidParamException’ from within our controller when user doesn’t specify a parameter e.g. ‘page_id’ but the problem is, we can’t simply throw an exception from inside the doGet method (except for RuntimeException, we can throw RuntimeException without including it in the exceptions list). Either we need to handle it by sorrounding […]

Ajax Filter : Prohibit unwanted direct access to ajax requests

The code If we don’t want to give unwanted access to the url’s that we only need to access via ajax, we can simply do it using a Filter. Here is the code: Filter mapping Now we need to specify for which controller’s we want to apply this filter. We can make this process simple […]

JSP custom tag with custom number of attributes

JSP custom tags are very powerful. In this writing, I’m going to show a way to reduce repetitive lines that we write in almost every JSP pages using JSP custom tags. starting from < !DOCTYPE> to the end of < head>tag. We almost include same javascript, css, favicon etc in all JSP pages. The only […]

Servlet mapping for web root

Suppose, you don’t want to put an index.jsp file in the web root and give access to that directly when people go to “http://example.com/”, rather you want to have a HomeController (or whatever) Servlet which will handle the request that comes to the web root. And here comes the problem, there’s no url-pattern for that […]