• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

add values of text boxes

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i am trying to add two no. from the 1st two text boxes and display the sum in the 3rd text box after clicking the submit button and all work should be done on one single jsp page.. please help

the exception i am getting is mentioned below...

HTTP Status 500 -

type Exception report

message

description The server encountered an internal error () that prevented it from fulfilling this request.

exception

org.apache.jasper.JasperException: An exception occurred processing JSP page /index.jsp at line 23

20: <input type="text" name="t3"/><br>
21: <input type="submit" value="ADD"/>
22:
23: <% Integer a = Integer.valueOf(request.getParameter("t1"));
24: Integer b = Integer.valueOf(request.getParameter("t2"));
25: Integer c = Integer.valueOf(request.getParameter("t3"));
26: Integer o = a+b ;


Stacktrace:
org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:510)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:419)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:313)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:260)
javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
org.netbeans.modules.web.monitor.server.MonitorFilter.doFilter(MonitorFilter.java:393)
root cause

java.lang.NumberFormatException: null
java.lang.Integer.parseInt(Integer.java:415)
java.lang.Integer.valueOf(Integer.java:553)
org.apache.jsp.index_jsp._jspService(index_jsp.java:72)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:377)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:313)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:260)
javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
org.netbeans.modules.web.monitor.server.MonitorFilter.doFilter(MonitorFilter.java:393)
note The full stack trace of the root cause is available in the Apache Tomcat/6.0.26 logs.


My jsp code is mentioned below...


<%--
Document : index
Created on : 14 Jul, 2011, 10:41:42 AM
Author : aNsH
--%>

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body>
<form action="index.jsp">
<input type="text" name="t1"/><br>
<input type="text" name="t2"/><br>
<input type="text" name="t3"/><br>
<input type="submit" value="ADD"/>

<% Integer a = Integer.valueOf(request.getParameter("t1"));
Integer b = Integer.valueOf(request.getParameter("t2"));
Integer c = Integer.valueOf(request.getParameter("t3"));
Integer o = a+b ;
session.setAttribute("add", o);
%>

<% if(c==null) {
%>

<input type="text" name="t3" value="<%=session.getAttribute("add") %>"/><br>

<%} %>


</form>
</body>
</html>
 
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Firstly. be aware that putting Java code in a JSP is an old, bad and obsolete practice.

Also be aware that the JSP executes on the server before the page is sent to the browser. So the text fields will have no value when the JSP executes until after the page is submitted a second time.

You should read this article to understand how JSP operates.
 
Divyanshu Arun
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i want to create 3 text fields and one submit button on one page where i can enter any 2 values in the first two text boxes and the sum should be displayed in the third text box on the same page..
and i want the coding in a single page of JSP..
please help.. i have been trying for so long..
 
Bear Bibeault
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Did you understand my post? Did you read the article? If you have not, do it now.

As I said, the first time that the page is displayed (before it is submitted through the form to itself) there can be no parameters. Your code does nothing to account for that situation. Hence, the null pointer exception when you try to use parameters that do not exist.

I trust that this is either homework or a learning exercise?
 
Divyanshu Arun
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
yeah ive gone through that article but could not get any help... and yes it was a question asked by my trainer..

n yeah i know the first time the page will be displayed there would not be any parameters.. so that is what i want to know
that what should i do to display the sum in the third text box in the same page..???
what i was thinking is that i can check if the third text box is empty/null it would calculate the sum n display i the third text box.. but how to do all that in the same page??
at least give me the logic if not code ??
 
Bear Bibeault
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You only have one page, so anything that's going to happen is all going to happen in the "same page" no matter what. If that's the case, you need to check if there are any values, which will only be present the second time that the page displays after the user enters them in the boxes and submits the form.

Or, do you mean that you need to display the answer without the user submitting the form?

I guess you need to explain what you mean by "same page"
 
Divyanshu Arun
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i want the initially there must be 3 empty text boxes and one submit button..
i want the user to enter values in the first two text boxes leaving the third text box empty then after clicking the submit button the first two text boxes remain the same with the previously filed value but their sum should be displayed in the third text box on the same page...
 
Bear Bibeault
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There are a bunch of problems in your original code, but the first to deal with is the fact that when the page is initially displayed, there will be no parameters.

What can you do in your code to account for that eventuality, and therefore avoid any null pointer exceptions?
 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
=>Use a servlet for handling the calculation and store the result in the request.
=>Forward the Servlet back to the same JSP
=>On your JSP,in the value attribute for your text box ,write a scriptlet to get the request value.(Also add additional condition to display empty string if the value is not available in the request to avoi d Null Pointer exceptions)
Refer any basic JSP tutorial for knowing how to do scriptleting.
Though this is a obsolete way of writing stuffs, i recommend only for the demostration purpose.However if you ask me personally , i would give you an idea of using Javascript to aid the calculation and displaying the values with no hassle.
 
reply
    Bookmark Topic Watch Topic
  • New Topic