Followers

springMVC : Exception Handling using Interface

index.jsp
======

<body>
    <h1>Library Transaction </h1>
    <form action="transaction.htm" >
        Enter the Employee number : <input type="text" name="empno" /> <br /><br />
        Enter the Book number : <input type="text" name="bookno" />
        <input type="submit" value="Issue" />
    </form>
   
</body>


web.xml
=====

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.1" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd">
    <servlet>
        <servlet-name>dispatcher</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <load-on-startup>2</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>dispatcher</servlet-name>
        <url-pattern>*.htm</url-pattern>
    </servlet-mapping>
    <session-config>
        <session-timeout>
            30
        </session-timeout>
    </session-config>
    <welcome-file-list>
        <welcome-file>index.jsp</welcome-file>
    </welcome-file-list>
</web-app>


dispatcher-servlet.xml
==============

<?xml version='1.0' encoding='UTF-8' ?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:p="http://www.springframework.org/schema/p"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
       http://www.springframework.org/schema/context  http://www.springframework.org/schema/context/spring-context-4.0.xsd
       http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
       http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd">

   
    <context:component-scan base-package="myspring" />
    <bean class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver" >
        <property name="exceptionMappings" >
            <map>
                <entry key="ArithmeticException" value="/WEB-INF/jsp/failure.jsp" ></entry>               
            </map>
           
        </property>
        <property name="defaultErrorView" value="/WEB-INF/jsp/failure-general.jsp" ></property>         
       
    </bean>
</beans>


MyController.java
===========

package myspring;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

@Controller
public class MyController {
   
     @RequestMapping("/transaction.htm")
    public String issue(@RequestParam("empno") int empno,@RequestParam("bookno") int bookno,HttpServletRequest request,HttpSession session) throws Exception
    {
        if(bookno == 0){
            request.setAttribute("empno", empno);
            request.setAttribute("bookno", bookno);
            session.setAttribute("empno", empno);
            session.setAttribute("bookno", bookno);
            int n = 12/bookno;
            throw new ArithmeticException("Please enter the valid Book number. ");
        }else{
            request.setAttribute("empno", empno);
            request.setAttribute("bookno", bookno);
            System.out.println("Book No "+bookno+" Employee No. "+empno);
        }
       
    return "/WEB-INF/jsp/success.jsp";
    }      
}

success.jsp
=======

<body>

    <%
       
        System.out.println("In View");
       
int bookno = (Integer)request.getAttribute("bookno");
int empno = (Integer)request.getAttribute("empno");
out.println("Book no : "+bookno+" is issued to Employee "+empno);
       
    %>
</body>


failure.jsp
======

<body>

    <%
        out.println("<br /><br />");
        out.println("In failure.jsp");
        String empno = (String)request.getParameter("empno");
        out.println("<br /><br />");
        out.println("Employee number is (request)"+empno);
        String bookno = (String)request.getParameter("bookno");
        out.println("<br /><br />");
        out.println("Book number is (request)"+bookno);
       
        Integer empno1 = (Integer)session.getAttribute("empno");
        out.println("<br /><br />");
        out.println("Employee number is(session) "+empno1);
        Integer bookno1 = (Integer)session.getAttribute("bookno");
        out.println("<br /><br />");
        out.println("Book number is (session) "+bookno1);
       
       
    %>
</body>


failure-general.jsp
============

<body>

    <%
        String message = (String)request.getAttribute("exc");
       
        System.out.println(message);
        out.println("I am general " + message);
       
    %>
</body>





No comments:

Post a Comment