java none怎么用tomcat_关于如何在Tomcat中使用JavaBean

对于没有使用myeclipse,NetBean等IDE工具的用户,如果在编写JSP时,用到了java文件,就必须配置JAVAbean了,网上也有很多在Tomcat中配置JAVABean的例子,这里我简单的说下自己配置的过程。

在此之前,相信初学者已经配置好了Tomcat的虚拟路径了,有些虚拟路径并不适合使用javabean,会不停的报错,在这时,首先找到tomcat文件夹中的webapps文件夹,然后将自己以前配置的虚拟路径剪切过来,例如我的虚拟目录是ysw文件,剪切过来后,在ysw文件夹下的WEB-INF文件里再创建一个classes文件,(WEB-INF有web.xml文件不用管)然后将自己要使用的.class文件复杂粘贴到classes文件夹中,例如我要使用到一个Register.class,此时它的路径时:E:\Program Files\Apache Software Foundation\Tomcat 6.0\webapps\ysw\WEB-INF\classes\yesiwei;yesiwei文件夹是一个包,里面包含了Register.class文件。然后在jsp中调用时 ,直接使用如下语句:;这时,便可以成功在JSP文件中调用javabean了(id和scope根据具体使用情况选择,不是固定的)。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,下面是一个简单的Java Web计算器的实现过程: 1. 首先创建一个JSP页面,用于显示计算器界面。在该页面,需要有一些按钮,如数字、运算符、清除、等于等。 2. 创建一个JavaBean类,用于处理计算器的逻辑。在该类,需要定义一些成员变量,如操作数1、操作数2、运算符等,以及一些方法,如加、减、乘、除等。 3. 在JSP页面使用`<jsp:useBean>`指令引用JavaBean类,并通过`<jsp:setProperty>`指令设置JavaBean类的属性。 4. 在JSP页面使用`<% %>`标签引用JavaBean的方法,用于处理用户的操作。例如,当用户点击加号按钮时,调用JavaBean的加法方法。 5. 最后,使用`<%= %>`标签输出计算结果。 下面是一个简单的Java Web计算器的代码实现: Calculator.jsp: ```jsp <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ page import="com.example.bean.CalculatorBean" %> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Calculator</title> </head> <body> <h1>Calculator</h1> <form method="post"> <table> <tr> <td colspan="4"><input type="text" name="result" value="${calculatorBean.getResult()}" /></td> </tr> <tr> <td><input type="button" value="7" onclick="document.forms[0].result.value += '7'" /></td> <td><input type="button" value="8" onclick="document.forms[0].result.value += '8'" /></td> <td><input type="button" value="9" onclick="document.forms[0].result.value += '9'" /></td> <td><input type="button" value="+" onclick="document.forms[0].operator.value = '+'" /></td> </tr> <tr> <td><input type="button" value="4" onclick="document.forms[0].result.value += '4'" /></td> <td><input type="button" value="5" onclick="document.forms[0].result.value += '5'" /></td> <td><input type="button" value="6" onclick="document.forms[0].result.value += '6'" /></td> <td><input type="button" value="-" onclick="document.forms[0].operator.value = '-'" /></td> </tr> <tr> <td><input type="button" value="1" onclick="document.forms[0].result.value += '1'" /></td> <td><input type="button" value="2" onclick="document.forms[0].result.value += '2'" /></td> <td><input type="button" value="3" onclick="document.forms[0].result.value += '3'" /></td> <td><input type="button" value="*" onclick="document.forms[0].operator.value = '*'" /></td> </tr> <tr> <td><input type="button" value="0" onclick="document.forms[0].result.value += '0'" /></td> <td><input type="button" value="C" onclick="document.forms[0].result.value = ''" /></td> <td><input type="submit" value="=" /></td> <td><input type="button" value="/" onclick="document.forms[0].operator.value = '/'" /></td> </tr> <tr> <td colspan="4"><input type="hidden" name="operator" /></td> </tr> </table> </form> </body> </html> ``` CalculatorBean.java: ```java package com.example.bean; public class CalculatorBean { private double operand1; private double operand2; private String operator; private double result; public CalculatorBean() { } public double getOperand1() { return operand1; } public void setOperand1(double operand1) { this.operand1 = operand1; } public double getOperand2() { return operand2; } public void setOperand2(double operand2) { this.operand2 = operand2; } public String getOperator() { return operator; } public void setOperator(String operator) { this.operator = operator; } public double getResult() { return result; } public void setResult(double result) { this.result = result; } public void add() { result = operand1 + operand2; } public void subtract() { result = operand1 - operand2; } public void multiply() { result = operand1 * operand2; } public void divide() { result = operand1 / operand2; } } ``` 在以上代码,我们通过`<jsp:useBean>`指令引用了CalculatorBean类,并使用`<jsp:setProperty>`指令设置了CalculatorBean类的属性。在表单提交时,我们通过JavaScript获取到用户的操作数和运算符,然后调用相应的JavaBean方法处理计算逻辑,最后通过`<%= %>`标签输出计算结果。 希望这篇文章能够帮助你实现一个简单的Java Web计算器。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值