2016-08-07 8 views
0
HTML page 

計算ボタンをクリックすると、回答テキストフィールドに出力する必要があります。しかし、最初に私はそれがどのように動作するか、いくつかのテキストを渡すことによって、そして回答フィールドのテキストを返すことをクリックすることでチェックしたい。1つのテキストフィールドが入力を通過し、クリックすると結果がサーブレットとhtmlページを使用して別のテキストボックスに表示されます

<!DOCTYPE html> 
<html lang="en"> 
<head> 
    <meta charset="UTF-8"> 
    <title>Title</title> 
</head> 
<body> 
<form name = 'calculate' action="Calculator" method ="get"> 
<h1>Text Based Calculator</h1> 
<p> 
    <label>Equation</label> 
    <input type ="text" style="font-size:10pt;height:20px;width:400px;" 
      id ="textEquation"/> 
    <input type="submit" style="height:25px;width:200px" name="Calculate" value="Calculate""> 


</p> 
<lable>Answer</lable> 
<input type = "text" style="font-size:10pt;height:20px;width:400px;" 
     id = "textAnswer"/> 
</body> 
</html> 


this is servlet class 

package textbasedcalculator; 

import java.io.IOException; 
import java.io.PrintWriter; 

import javax.servlet.ServletException; 
import javax.servlet.annotation.WebServlet; 
import javax.servlet.http.HttpServlet; 
import javax.servlet.http.HttpServletRequest; 
import javax.servlet.http.HttpServletResponse; 

@WebServlet("/Calculator") 
public class Calculator extends HttpServlet { 

    private static final long serialVersionUID = 1L; 

    public Calculator() { 
     super(); 

    } 

    protected void doGet(HttpServletRequest request, HttpServletResponse response) 
      throws ServletException, IOException { 
     response.setContentType("text/html;charset=UTF-8"); 
     PrintWriter out = response.getWriter(); 

     String Equation = request.getParameter("textEquation"); 






     } 
} 

答えて

0

入力タグでは、の代わりにname="textEquation"を使用します。次に、getParameter()メソッドを使用してこの入力値を代行受信できます。 サーブレットでは、この値を要求の属性として代行受信し、要求をJSPページに戻します。転送されたJSPページで今すぐ

String equation = request.getParameter("textEquation"); 
request.setAttribute("answer",equation); 
request.getRequestDispatcher("nameOfJspPage.jsp").forward(request,response); 

これらの行を追加します。

<lable>Answer</lable> 
<input type = "text" style="font-size:10pt;height:20px;width:400px;" 
     id = "textAnswer" value="${requestScope.answer}"/> 

あなたの答えが表示されます。 Note:ビューはjspページではなくHTMLページである必要があります。動的コンテンツにhtmlを使用することはできません。 これが役立つことを願っています。

関連する問題