Servlet Exception and Error Handling Example Tutorial

About Pankaj Kumar

Sometime back I wrote a post about Exception Handling in Java but when it comes to web application, we need more than normal exception handling in java.

Servlet Exception

If you notice, doGet() and doPost() methods throw ServletException and IOException, let’s see what happens when we throw these exception from our application. I will write a simple servlet that will throw the ServletException.
 
 
 
 
MyExceptionServlet.java

package com.journaldev.servlet.exception;import java.io.IOException;import javax.servlet.ServletException;import javax.servlet.annotation.WebServlet;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;@WebServlet("/MyExceptionServlet")public class MyExceptionServlet extends HttpServlet {	private static final long serialVersionUID = 1L;	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {		throw new ServletException("GET method is not supported.");	}}

Source : http://www.javacodegeeks.com/2013/08/servlet-exception-and-error-handling-example-tutorial.html

Back to Top