an exception occurred processing jsp page

an exception occurred processing jsp page

I am JSP beginner and I have developed a code to calculate factorial of numbers. But I am facing a problem.
I am getting error on this code . Can any one suggest me solution as well as what is the problem in this code ??
(I want to do full coding in a single JSP file.)

 

view plaincopy to clipboardprint?
Note: Text content in the code blocks is automatically word-wrapped
<%@page language="java" contentType="text/html" pageEncoding="UTF-8" %>  
<!DOCTYPE HTML >  
<html>  
<head>  
  <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">  
  <title>Factorial</title>  
</head>  
       
<body>  
  <b>Enter Name:-</b>  
    
    <form>  
    
    <table>  
            <tr><td><input type="text" name="n1" ></td></tr>  
            <tr><td><input type="text" name="n2" ></td></tr>  
            <tr><td><center><input type="submit" value="Submit"></center></tr>  
    </table>  
    
    </form>  
 
<%  
int num1=Integer.parseInt(request.getParameter("n1"));  
int num2=Integer.parseInt(request.getParameter("n2"));  
%>  
 
 
<%!  
    int factorial(int n)    {  
        if (n == 1) {  
            return n;  
        }  
        else {  
            return n * factorial(n - 1);  
        }  
    }  
%>  
 
    <h3>Fact are:-</h3>  
 
 
 
    <%  
    out.println("The factorial of  " + num1 +"  is : " + factorial(num1));  
    out.println("The factorial of  " + num2 +"  is : " + factorial(num2));  
    %>  
 
 
 
  <br /><a href="<%= request.getRequestURI() %>">Back</a>   
<body>  
</html> 
<%@page language="java" contentType="text/html" pageEncoding="UTF-8" %>
<!DOCTYPE HTML >
<html>
<head>
  <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  <title>Factorial</title>
</head>
    
<body>
  <b>Enter Name:-</b>
 
    <form>
 
 <table>
     <tr><td><input type="text" name="n1" ></td></tr>
     <tr><td><input type="text" name="n2" ></td></tr>
      <tr><td><center><input type="submit" value="Submit"></center></tr>
 </table>
 
   </form>

<%
int num1=Integer.parseInt(request.getParameter("n1"));
int num2=Integer.parseInt(request.getParameter("n2"));
%>


<%!
    int factorial(int n) {
        if (n == 1) {
            return n;
        }
        else {
            return n * factorial(n - 1);
        }
    }
%>

    <h3>Fact are:-</h3>

 

 <%
    out.println("The factorial of  " + num1 +"  is : " + factorial(num1));
    out.println("The factorial of  " + num2 +"  is : " + factorial(num2));
 %>

 

  <br /><a href="<%= request.getRequestURI() %>">Back</a>
<body>
</html>

 


Error
view plaincopy to clipboardprint?
Note: Text content in the code blocks is automatically word-wrapped
   
 
org.apache.jasper.JasperException: An exception occurred processing JSP page /test0.jsp at line 23 
 
20:     </form>  
21:   
22: <%  
23: int num1=Integer.parseInt(request.getParameter("n1"));  
24: int num2=Integer.parseInt(request.getParameter("n2"));  
25: %>  
26:   
 
 
Stacktrace:  
    org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:568)  
    org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:470)  
    org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:390)  
    org.apache.jasper.servlet.JspServlet.service(JspServlet.java:334)  
    javax.servlet.http.HttpServlet.service(HttpServlet.java:728)  
 
 
root cause   
 
java.lang.NumberFormatException: null 
    java.lang.Integer.parseInt(Unknown Source)  
    java.lang.Integer.parseInt(Unknown Source)  
    org.apache.jsp.test0_jsp._jspService(test0_jsp.java:94)  
    org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)  
    javax.servlet.http.HttpServlet.service(HttpServlet.java:728)  
    org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:432)  
    org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:390)  
    org.apache.jasper.servlet.JspServlet.service(JspServlet.java:334)  
    javax.servlet.http.HttpServlet.service(HttpServlet.java:728) 

For starters, you should not keep Java code in JSPs; that's a thoroughly outdated and obsolete way of creating web apps.

As to the problem at hand, when you access this JSP, n1 and n2 are not present, and so Integer.parseInt is trying to parse a null value. All that code should only be executed if n1 and n2 are present - after the form has been submitted. 

 
Fortunately "if" can be used in the code snippets in JSP making possible the conditional executing of code! 
 
If you tried something you should tell us what that was. You must not pass a null value to Integer.parseInt, that's for sure. Alternatively, do proper exception handling with a try/catch block.

  • Back aarticle:
  • Next aarticle: No
  • Copyright © 2007-2012 www.chuibin.com Chuibin Copyright