• Post Reply Bookmark Topic Watch Topic
  • New Topic

jsp/bean/tomcat

 
Celinio Fernandes
Ranch Hand
Posts: 549
Eclipse IDE Google Web Toolkit Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a problem regarding a small application:
in an HTML page, calcpage.html, when I click on the SUBMIT button, it is
supposed to call a JSP page but nothing happens.
The HTML page is located here:
E:\jakarta-tomcat-4.1.24\webapps\ROOT\calcpage.html
The JSP page is located here:
E:\jakarta-tomcat-4.1.24\webapps\ROOT\calculate.jsp
The bean is here :
E:\jakarta-tomcat-4.1.24\webapps\ROOT\WEB-INF\classes\cbean\CalcBean.class
What am i doing wrong ? thanks
The URL is http://localhost:8080/calcpage.html
Here is the code :
CalcBean.java
package cbean ; //1
public class CalcBean {
/**
Calculator bean for Chapter 6
*/
private int operand1 = 0 ; //2
private int operand2 = 0 ;
private double result = 0 ;
private String operation = "" ;
//No-arg constructor for bean....
public CalcBean() { } //3
//Get/Set methods follow
public void setOperand1( int op1 ) { //4
operand1 = op1 ;
}
public void setOperand2( int op2 ) {
operand2 = op2 ;
}
public void setOperation( String oper ) {
operation = oper ;
}
public void setResult( double aResult) {
result = aResult ;
}
public int getOperand1() { //5
return operand1 ;
}
public int getOperand2() {
return operand2 ;
}
public String getOperation() {
return operation;
}
public double getResult() {
return performTheOperation() ; //6
}
/**
Perform the operation.....generate the result
*/
private double performTheOperation( ) {
double aResult = 0.0 ;
if ( operation.equals("+") )
aResult = operand1 + operand2 ;
else
if ( operation.equals("-") )
aResult = operand1 - operand2 ;
else
if ( operation.equals("*") )
aResult = operand1 * operand2 ;
else
if ( operation.equals("/") )
aResult = operand1 / operand2 ;
return aResult ;
}
}
calcpage.html
<html>
<head>
<title>JSP Sample Page - Calculator Bean</title>
</head>
<body bgcolor="#dddddd">
<center>
<br>
<h1>A Simple Calculator</h1>
<!-- Here is the reference to the JSP that uses CalcBean -->
<form name="calcform" action="calculate.jsp" method="POST" >
<p>Enter Operand1 and Operand2 (Integers)
<br>and Select an operation From the Pull Down
<br>Menu, then Click <b>Calculate</b> to Continue
<br>
<hr width="50%">
<table>
<tr>
<td><P>Enter Operand1:</td>
<!-- Entered value to be CalcBean property called operand1 -->
<td><input type="text" name="operand1" value="" width="25"></td>
</tr>
<tr>
<td><P>Enter Operand2:</td>
<!-- Entered value to be CalcBean property called operand2 -->
<td><input type="text" name="operand2" value="" width="25"></td>
</tr>
<tr>
<td><P>Select Operation:</td>
<!-- Selected value to be CalcBean property called operation -->
<td><select NAME="operation">
<option> +
<option> -
<option> *
<option> /
</select>
</td>
<tr>
<!-- Click here to invoke the JSP page coded in the FORM tag -->
<td><input type="button" name="Calculate" value="Calculate"></td>
</tr>
</table>
<hr width="50%">
</center>
</form>
</body>
</html>
calculate.jsp
<%-- Tell JSP that this page renders HTML --%>
<%@ page contentType="text/html" %>
<%-- Tell JSP to use the bean CalcBean --%>
<jsp:useBean id="CalcBean" class="cbean.CalcBean" />
<%-- Tell JSP to map CalcBean properties to like-named input parameters --%>
<jsp:setProperty name="CalcBean" property="*" />
<html>
<head>
<title>Show Calc Results bean</title>
</head>
<body bgcolor="#dddddd">
Here's what was entered from <i>calcpage.html</i>: <p>
<P>The next 4 lines show using the jsp:getProperty action to fetch bean
properties
<%-- jsp:getProperty writes the value of the bean property where coded --%>
<p>Operand1 <b><jsp:getProperty name="CalcBean" property="operand1" /></b>
<p>Operand2 <b><jsp:getProperty name="CalcBean" property="operand2" /></b>
<p>Operation <b><jsp:getProperty name="CalcBean" property="operation" /></b>
<p>Result <b><jsp:getProperty name="CalcBean" property="result" /></b>
<br>
<P>The next 4 lines show using the request implicit object to fetch input
properties
<%-- Access input parameters by accessing the request object. --%>
<p>Operand1 <b><%= request.getParameter("operand1")%></b>
<p>Operand2 <b><%= request.getParameter("operand2")%></b>
<p>Operation <b><%= request.getParameter("operation")%></b>
<p>result - not an input parameter!<b>
<%= request.getParameter("result")%></b>
<br>
<p>The next group of lines access and display bean properties by using
scriptlet code, expressions
and JSP actions
<%-- Invoke the 'get' method of the bean directly to get the property
value --%>
<% int enteredOperand1 = CalcBean.getOperand1();
int enteredOperand2 = CalcBean.getOperand2();
if (enteredOperand1 > enteredOperand2 ) { %>
<%-- You may use expressions, actions and scriptlets together in your JSPs
as shown below --%>
<p> <jsp:getProperty name="CalcBean" property="operand1" /> is
<b>larger</b> than <%= enteredOperand2 %>
<% } else { %>
<p> <%= enteredOperand1 %> is <b>smaller</b> than <jsp:getProperty
name="CalcBean" property="operand2" />
<% } %>
</body>
</html>
 
Celinio Fernandes
Ranch Hand
Posts: 549
Eclipse IDE Google Web Toolkit Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i found the problem:
<input type="submit" etc ...
and not
<input type="button"
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!