Results 1 to 4 of 4
  1. #1
    koricha is offline Member
    Join Date
    Feb 2012
    Posts
    3
    Rep Power
    0

    Default jsp Exception error

    i am having a problem with the continue button when i don't enter anything in the text field i get an error "org.apache.jasper.JasperException: An exception occurred processing JSP page /index.jsp at line 18" i know something is wrong with my exceptions but i am not sure what .
    please help .

    Java Code:
    <%@ page import="java.io.*"%><%@
    page import="java.util.*"%><?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    </head>
    <meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
    <%
            String sum = (String) session.getAttribute("sum");
            
                    sum = "5";
                    session.setAttribute("sum",sum);
            
            int isum = Integer.parseInt(sum);
            try {
            isum=Integer.parseInt(request.getParameter("number"));
            } catch (NumberFormatException nfe){ out.println("Error!");}     
            if(request.getParameter("number")!=null && Integer.parseInt(request.getParameter("number"))==isum)
            {
             if(request.getParameter("submit") != null){
                    out.print("Hello");}
            }
    %>
    
    <body>
            <title>MAIN</title>
            <form action="index.jsp" method="post">
            Enter 5 = <input type="text" name="number">
            <input type="submit" value="continue" name="submit">
            </form>
    
    
    </body>
    </html>

  2. #2
    wsaryada is offline Senior Member
    Join Date
    Jun 2007
    Location
    Bali, Indonesia
    Posts
    760
    Rep Power
    8

    Default Re: jsp Exception error

    As the error message said that the error is in the line number 18 in your JSP. The one thing that could throw an exception in that line is the Integer.parseInt() method. When you input no value to the number field that mean you will get an empty string when you try to read it from the request object. When you try to convert empty string to number you will surely get a NumberFormatException.

  3. #3
    koricha is offline Member
    Join Date
    Feb 2012
    Posts
    3
    Rep Power
    0

    Default Re: jsp Exception error

    so how can i fix it?

  4. #4
    wsaryada is offline Senior Member
    Join Date
    Jun 2007
    Location
    Bali, Indonesia
    Posts
    760
    Rep Power
    8

    Default Re: jsp Exception error

    First you have to decided if this parameter is required by your program or not. If it is required then you have to check it when the submit button is pressed. When the value is not supplied you can just say that the number field is required. Some JavaScript will help you, or you can use the required attribute of HTML5.

    On the server side you need to validate whether the number supplied by the user is a valid number (if you don't validate it whether it is a valid number on the browser side). A try-catch block for NumberFormatException can help you to validate it. And when the exception happen how will you handle the invalid number? should you default the number to zero, or should you stop the process, as an example.

    There are couple of things that I can suggest for your scriptlet. You can store object type to the session's attribute so that you don't need to convert from string back to integer. Just store integer instead in the session attribute.

    You repeat the request.getParameter("number") three times in your code. It is better to call this once, store it on a variable and then use this variable for the next line in your code.

Similar Threads

  1. Why is this getting an exception error?
    By kyle_maddisson in forum New To Java
    Replies: 16
    Last Post: 11-04-2011, 03:17 PM
  2. Exception Error
    By unexpert in forum New To Java
    Replies: 1
    Last Post: 02-09-2011, 05:30 PM
  3. exception error
    By kira137 in forum New To Java
    Replies: 5
    Last Post: 10-12-2009, 07:46 AM
  4. Exception error
    By jaiminparikh in forum New To Java
    Replies: 0
    Last Post: 03-20-2009, 09:06 PM
  5. JSF error+exception
    By Peter in forum SWT / JFace
    Replies: 1
    Last Post: 07-04-2007, 06:29 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •