Java Servlet ile İstisna ve Hata yakalama ve kullanıcı ekranına basma kodu kullanımı örneği

Java Servlet ile İstisna(Exception) ve Hata(Error) hata yakalama ve kullanıcı ekranına basma kodu kullanımı örneği aşağıdaki gibidir.

package net.yazilimcity.application.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.io.StringWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class ExceptionErrorHandler
 * 
 * @author oguzhankinik
 */

public class ExceptionErrorHandler extends HttpServlet {
	private static final long serialVersionUID = 1L;

	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		processError(request, response);
	}

	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		processError(request, response);
	}

	private void processError(HttpServletRequest request,
			HttpServletResponse response) throws IOException {
		
		Throwable throwable = (Throwable) request.getAttribute("javax.servlet.error.exception");
		Integer statusCode = (Integer) request.getAttribute("javax.servlet.error.status_code");
		String servletName = (String) request.getAttribute("javax.servlet.error.servlet_name");
		if (servletName == null) {
			servletName = "Unknown";
		}
		String requestUri = (String) request.getAttribute("javax.servlet.error.request_uri");
		if (requestUri == null) {
			requestUri = "Unknown";
		}

		// Set response content type
		response.setContentType("text/html");

		PrintWriter out = response.getWriter();
		out.write("<html><head><title>Exception/Error Details</title></head><body>");
		if (statusCode != 500) {
			out.write("<h3>Error Details</h3>");
			out.write("<strong>Status Code</strong>:" + statusCode + "<br>");
			out.write("<strong>Requested URI</strong>:" + requestUri);
		} else {
			StringWriter stackStringWriter = new StringWriter();
			PrintWriter printWriter = new PrintWriter(stackStringWriter);
			throwable.printStackTrace(printWriter);
			String stackTrace = stackStringWriter.toString();

			out.write("<h3>Exception Details</h3>");
			out.write("<ul><li>Servlet Name:" + servletName + "</li>");
			out.write("<li>Exception Name:" + throwable.getClass().getName()+ "</li>");
			out.write("<li>Requested URI:" + requestUri + "</li>");
			out.write("<li>Exception Message:" + throwable.getMessage()+ "</li>");
			out.write("<li>Exception StackTrace:" + stackTrace + "</li>");
			out.write("</ul>");
		}

		out.write("<br><br>");
		out.write("<a href=\"index.html\">Home Page</a>");
		out.write("</body></html>");
	}
}

web.xml içeriğinde aşağıdaki kod bloğunu tanımlamalısınız.

  <servlet>
    <servlet-name>ExceptionErrorHandler</servlet-name>
    <servlet-class>net.yazilimcity.application.servlet.ExceptionErrorHandler</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>ExceptionErrorHandler</servlet-name>
    <url-pattern>/ExceptionErrorHandler</url-pattern>
  </servlet-mapping>

  <error-page>
    <exception-type>java.lang.Throwable</exception-type>
    <location>/ExceptionErrorHandler</location>
  </error-page>

Kullanıcı ekran görüntüsünde ise bir örnek hata olarak aşağıdaki gibi bir sayfa ile karşılaşır.


Exception/Error Details

Exception Details

  • Servlet Name:ViewerServlet
  • Exception Name:java.lang.NullPointerException
  • Requested URI:/EInvoice/ViewerServlet
  • Exception Message:while trying to invoke the method java.lang.String.toLowerCase(java.util.Locale) of a null object returned from javax.servlet.http.HttpServletRequest.getParameter(java.lang.String)
  • Exception StackTrace:java.lang.NullPointerException: while trying to invoke the method java.lang.String.toLowerCase(java.util.Locale) of a null object returned from javax.servlet.http.HttpServletRequest.getParameter(java.lang.String) at com.btcag.einvoice.servlet.ViewerServlet.doGet(ViewerServlet.java:77) at javax.servlet.http.HttpServlet.service(HttpServlet.java:734) at javax.servlet.http.HttpServlet.service(HttpServlet.java:847) at com.sap.engine.services.servlets_jsp.server.Invokable.invoke(Invokable.java:152) at com.sap.engine.services.servlets_jsp.server.Invokable.invoke(Invokable.java:38) at com.sap.engine.services.servlets_jsp.server.HttpHandlerImpl.runServlet(HttpHandlerImpl.java:466) at com.sap.engine.services.servlets_jsp.server.HttpHandlerImpl.handleRequest(HttpHandlerImpl.java:210) at com.sap.engine.services.httpserver.server.RequestAnalizer.startServlet(RequestAnalizer.java:441) at com.sap.engine.services.httpserver.server.RequestAnalizer.startServlet(RequestAnalizer.java:430) at com.sap.engine.services.servlets_jsp.filters.DSRWebContainerFilter.process(DSRWebContainerFilter.java:38) at com.sap.engine.services.httpserver.chain.AbstractChain.process(AbstractChain.java:78) at com.sap.engine.services.servlets_jsp.filters.ServletSelector.process(ServletSelector.java:81) at com.sap.engine.services.httpserver.chain.AbstractChain.process(AbstractChain.java:78) at com.sap.engine.services.servlets_jsp.filters.ApplicationSelector.process(ApplicationSelector.java:278) at com.sap.engine.services.httpserver.chain.AbstractChain.process(AbstractChain.java:78) at com.sap.engine.services.httpserver.filters.WebContainerInvoker.process(WebContainerInvoker.java:81) at com.sap.engine.services.httpserver.chain.HostFilter.process(HostFilter.java:9) at com.sap.engine.services.httpserver.chain.AbstractChain.process(AbstractChain.java:78) at com.sap.engine.services.httpserver.filters.ResponseLogWriter.process(ResponseLogWriter.java:60) at com.sap.engine.services.httpserver.chain.HostFilter.process(HostFilter.java:9) at com.sap.engine.services.httpserver.chain.AbstractChain.process(AbstractChain.java:78) at com.sap.engine.services.httpserver.filters.DefineHostFilter.process(DefineHostFilter.java:27) at com.sap.engine.services.httpserver.chain.ServerFilter.process(ServerFilter.java:12) at com.sap.engine.services.httpserver.chain.AbstractChain.process(AbstractChain.java:78) at com.sap.engine.services.httpserver.filters.MonitoringFilter.process(MonitoringFilter.java:29) at com.sap.engine.services.httpserver.chain.ServerFilter.process(ServerFilter.java:12) at com.sap.engine.services.httpserver.chain.AbstractChain.process(AbstractChain.java:78) at com.sap.engine.services.httpserver.filters.SessionSizeFilter.process(SessionSizeFilter.java:26) at com.sap.engine.services.httpserver.chain.ServerFilter.process(ServerFilter.java:12) at com.sap.engine.services.httpserver.chain.AbstractChain.process(AbstractChain.java:78) at com.sap.engine.services.httpserver.filters.MemoryStatisticFilter.process(MemoryStatisticFilter.java:57) at com.sap.engine.services.httpserver.chain.ServerFilter.process(ServerFilter.java:12) at com.sap.engine.services.httpserver.chain.AbstractChain.process(AbstractChain.java:78) at com.sap.engine.services.httpserver.filters.DSRHttpFilter.process(DSRHttpFilter.java:43) at com.sap.engine.services.httpserver.chain.ServerFilter.process(ServerFilter.java:12) at com.sap.engine.services.httpserver.chain.AbstractChain.process(AbstractChain.java:78) at com.sap.engine.services.httpserver.server.Processor.chainedRequest(Processor.java:475) at com.sap.engine.services.httpserver.server.Processor$FCAProcessorThread.process(Processor.java:269) at com.sap.engine.services.httpserver.server.rcm.RequestProcessorThread.run(RequestProcessorThread.java:56) at com.sap.engine.core.thread.execution.Executable.run(Executable.java:122) at com.sap.engine.core.thread.execution.Executable.run(Executable.java:101) at com.sap.engine.core.thread.execution.CentralExecutor$SingleThread.run(CentralExecutor.java:328)


Home Page
Share on Facebook0Tweet about this on TwitterShare on Google+0Share on StumbleUpon0Share on Reddit0

Leave a Reply


*