130 How to configure Error Servlet when exception is raised in other Servlet programs of Java web ap

Опубликовано: 22 Декабрь 2018
на канале: tech fort
439
37

Example Java web application - #How to #configure #Error #Servlet when #exception is #raised in #other #Servlet #programs of #Java #web #application #advance #java #servlet #programming #tutorial
40Error_ServletWA
-----------------
|-----input.html
|-----WEB-INF
|-------web.xml
|-------lib
|-------ojdbc14.jar
|-------classes
|-----DBSrv.java
|-----ErrSrv.java
|-----*.class

Step 1) Create Context Root Folder "40Error_ServletWA"

C:\Program Files\Apache Software Foundation\Tomcat 9.0\webapps

Step 2) Create WEB-INF folder under
C:\Program Files\Apache Software Foundation\Tomcat 9.0\webapps\40Error_ServletWA folder

Step 3) Create lib , classes folder under C:\Program Files\Apache Software Foundation\Tomcat 9.0\webapps\40Error_ServletWA\WEB-INF

Step 4) Create input.html page and save to
C:\Program Files\Apache Software Foundation\Tomcat 9.0\webapps\40Error_ServletWA

Step 5) Create DBSrv.java and save to
C:\Program Files\Apache Software Foundation\Tomcat 9.0\webapps\40Error_ServletWA\WEB-INF\classes folder

Step 6) Create ErrSrv.java and save to
C:\Program Files\Apache Software Foundation\Tomcat 9.0\webapps\40Error_ServletWA\WEB-INF\classes folder

Step 7) Add ojdbc14.jar to lib folder C:\Program Files\Apache Software Foundation\Tomcat 9.0\webapps\40Error_ServletWA\WEB-INF\lib

Step 8) Create web.xml by configuring above servlets and save to
C:\Program Files\Apache Software Foundation\Tomcat 9.0\webapps\40Error_ServletWA\WEB-INF\

But first of all let me show you The error/exception technical message without configuring ErrSrv.java in web.xml

HTTP Status 500 ? Internal Server Error
Type Exception Report

Message For input string: "233 patil ds"

Description The server encountered an unexpected condition that prevented it from fulfilling the request.

Exception

java.lang.NumberFormatException: For input string: "233 patil ds"
java.lang.NumberFormatException.forInputString(Unknown Source)
java.lang.Integer.parseInt(Unknown Source)
java.lang.Integer.parseInt(Unknown Source)
DBsrv.doGet(DBsrv.java:17)
javax.servlet.http.HttpServlet.service(HttpServlet.java:634)
javax.servlet.http.HttpServlet.service(HttpServlet.java:741)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)
Note The full stack trace of the root cause is available in the server logs.

Next time you may expect Database related error or technical server related error ,

SO we need to configure ErrorServlet in web.xml file
Compile ErrSrv.java

Add the following code in DBSrv.java in cactch block

System.out.println("DBSrv : before rd");
RequestDispatcher rd=req.getRequestDispatcher("/eurl");
rd.forward(req,res);
System.out.println("DBSrv : after rd");

Recompile DBSrv.java
Reload the web application

Reload the web application go tomcat admin cosole and reolad

Step 9)Compile DBSrv.java
Need to add servlet-api.jar into classpath before compiling above servlet programs

Step 10) Start the server
C:\Program Files\Apache Software Foundation\Tomcat 9.0\bin\tomcat9.exe

Step 11) test the web appls
open the browser

localhost:2020/40Error_ServletWA


In excpetion is raised in DBSrv.java program doGet(-,-) method control goes to catch block and rd.forward(-,-) method forwards request to Error Servlet program other wise the output of DBSrv directly goes to browser window.

When Servlet program executes rd.forward(req,res) method the entire html output of source servlet program will be discarded but its System.out.println("message") output will not be discarded and shown to tomcat server console.

Dont commit response/output going to response object by calling pw.close() method before calling rd.forward(req,res) OR rd.include(req,res) methods becuase it causes problems in servlet chaining

(Conclusion: Dont place pw.close() before rd.forward(-,-) OR rd.inclue(-,-) methods)

#Understanding of requestDispatcher.include(request,response) method: