带有jsp:useBean的Javabean它们如何工作?我不明白
我在index.jsp和Servlet之间又做了一个jsp文件:
<jsp:useBean scope='session'/><jsp:setProperty name='binop' property='*'/>.
这做到了“魔术”。
解决方法我必须使用Java bean从jsp文件中获取2个数字和一个运算。提交数字后,将它们从该Java Bean带到servlet并返回它们的结果。问题是Javabean字段永远不会用文本框中写的数字来完成。所以,我有了index.jsp的主体:
<html><head> <meta http-equiv='Content-Type' content='text/html; charset=UTF-8'> <title>JSP Page</title> <link rel='stylesheet' type='text/css' href='https://www.haobala.com/wenda/style.css' /></head><body> <h1>Calculator</h1> <FORM METHOD='POST' action='Controller'>N1: <input type ='text' name='nr1' value='0'>op: <input type ='text' name='op' value='+'>N2: <input type ='text' name='nr2' value='0'><INPUT TYPE='submit' NAME='actiune' VALUE='Calculate'/> </FORM> <jsp:useBean scope='session'/> <jsp:setProperty name='binOp' property='*'/></body>
servlet的processRequest方法Controller.java放在程序包servlet中:
protected void processRequest(HttpServletRequest request,HttpServletResponse response)throws ServletException,IOException { response.setContentType('text/html;charset=UTF-8'); PrintWriter out = response.getWriter(); HttpSession session = request.getSession(true); BinaryOperation binOp = (BinaryOperation) session.getAttribute('binOp'); try {if (!binOp.isComplete()) { System.out.println(binOp.getNr1() + binOp.getNr2() + binOp.getOp()); response.sendRedirect('index.jsp');} else { out.println('<html>'); out.println('<head>'); out.println('<title>Servlet Controller</title>'); out.println('</head>'); out.println('<body>'); out.println('<h1>Bean in controller ' + binOp.getNr1() + '__' + binOp.getOp() + '__' + binOp.getNr2() + '</h1>'); out.println(binOp.toString()); out.println('</body>'); out.println('</html>');} } finally {out.close(); }}
然后将bean BinaryOperation放入程序包bean中:
package beans;public class BinaryOperation {private String nr1;private String op;private String nr2;public void setNr1(String nr1) { this.nr1 = nr1;}public void setOp(String op) { this.op = op;}public void setNr2(String nr2) { this.nr2 = nr2;}public String getNr1() { return nr1;}public String getOp() { return op;}public String getNr2() { return nr2;}public boolean isComplete() { return !(((nr1 == null) || (nr1.length() == 0)) || ((op == null) || (op.length() == 0)) || ((nr2 == null) || (nr2.length() == 0)));}}
在Apache日志中,我有if语句的下一个输出(请参阅servlet-System.out.println(binOp.getNr1()+binOp.getNr2()+ binOp.getOp());):nullnullnull
我的错误在哪里?
相关文章:
1. javascript - 责任具体在哪一方2. python - from ..xxxx import xxxx到底是什么意思呢?3. 老师 我是一个没有学过php语言的准毕业生 我希望您能帮我一下4. php - 想要远程推送emjio ios端怎么搞 需要怎么配合5. mysql - 请教一条sql6. angular.js - 使用requireJS管理JS, angularJS就不需要用ng-app了吗?7. 冒昧问一下,我这php代码哪里出错了???8. CSS清除浮动有几种方法?9. css3 怎么实现锯齿状的剪纸效果(如图)10. javascript - 原生JS实现发送验证码计秒