帶有jsp:useBean的Javabean。它們?nèi)绾喂ぷ??我不明?/h1>
瀏覽:102日期:2024-04-20 10:08:06
如何解決帶有jsp:useBean的Javabean。它們?nèi)绾喂ぷ??我不明白?p>我在index.jsp和Servlet之間又做了一個jsp文件: <jsp:useBean scope='session'/><jsp:setProperty name='binop' property='*'/>.
這做到了“魔術(shù)”。
解決方法 我必須使用Java bean從jsp文件中獲取2個數(shù)字和一個運(yùn)算。提交數(shù)字后,將它們從該Java Bean帶到servlet并返回它們的結(jié)果。問題是Javabean字段永遠(yuǎn)不會用文本框中寫的數(shù)字來完成。所以,我有了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='http://www.aoyou183.cn/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
我的錯誤在哪里?
標(biāo)簽:
java
上一條:從單擊它的html表行中預(yù)填充表單字段。(所有這些都應(yīng)該在jsp上發(fā)生)下一條:一段時間不活動后,出現(xiàn)“沒有更多數(shù)據(jù)可從套接字讀取”的信息。Weblogic和Oracle DB
相關(guān)文章:
1. javascript - ionic1的插件如何遷移到ionic2的項目中2. java - 如何在Fragment中調(diào)用Activity的onNewIntent?3. javascript - h5上的手機(jī)號默認(rèn)沒有識別4. mysql里的大表用mycat做水平拆分,是不是要先手動分好,再配置mycat5. css - 關(guān)于input標(biāo)簽disabled問題6. python - 獲取到的數(shù)據(jù)生成新的mysql表7. 怎么用css截取字符?8. window下mysql中文亂碼怎么解決??9. javascript - jquery hide()方法無效10. python的文件讀寫問題?
<jsp:useBean scope='session'/><jsp:setProperty name='binop' property='*'/>.
這做到了“魔術(shù)”。
解決方法我必須使用Java bean從jsp文件中獲取2個數(shù)字和一個運(yùn)算。提交數(shù)字后,將它們從該Java Bean帶到servlet并返回它們的結(jié)果。問題是Javabean字段永遠(yuǎn)不會用文本框中寫的數(shù)字來完成。所以,我有了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='http://www.aoyou183.cn/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
我的錯誤在哪里?
