2010-04-18 53 views
0

我正在嘗試使用JSP創建一個在線考試。我想要逐個獲取問題並將它們顯示在屏幕上,然後創建一個「下一步」按鈕,然後用戶可以看到下一個問題,但問題是我不知道如何找出用戶有問題點擊「下一步」按鈕,我知道如何在PHP中:如何找到表單在JSP中提交?

if($_SERVER['REQUEST_METHOD']=='GET') 
    if($_GET['action']=='Next') 

但我不知道如何在JSP中做到這一點。請幫我 這是一張我的代碼:

String result = ""; 
    if (database.DatabaseManager.getInstance().connectionOK()) { 
     database.SQLSelectStatement sqlselect = new database.SQLSelectStatement("question", "question", "0"); 
     ResultSet _userExist = sqlselect.executeWithNoCondition(); 
     ResultSetMetaData rsm = _userExist.getMetaData(); 

     result+="<form method='post'>"; 
     result += "<table border=2>"; 
     for (int i = 0; i < rsm.getColumnCount(); i++) { 
      result += "<th>" + rsm.getColumnName(i + 1) + "</th>"; 
     } 

     if (_userExist.next()) {     
      result += "<tr>"; 
      result += "<td>" + _userExist.getInt(1) + "</td>"; 
      result += "<td>" + _userExist.getString(2) + "</td>"; 
      result += "</tr>"; 
      result += "<tr>"; 
      result += "<tr> <td colspan='2'>asdas</td></tr>"; 
      result += "</tr>";     
     } 
     result += "</table>"; 
     result+="<input type='submit' value='next' name='next'/></form>"; 
    } 

回答

1

所有相關的HTML輸入元素的name-value對可作爲請求參數。

<input type="submit" name="action" value="prev"> 
<input type="submit" name="action" value="next"> 

String action = request.getParameter("action"); 
if ("prev".equals(action)) { 
    // Prev button pressed. 
} else if ("next".equals(action)) { 
    // Next button pressed. 
} 

或替代,

<input type="submit" name="prev" value="prev"> 
<input type="submit" name="next" value="next"> 

if (request.getParameter("prev") != null) { 
    // Prev button pressed. 
} else if (request.getParameter("next") != null) { 
    // Next button pressed. 
} 

也就是說,模板文本屬於一個JSP文件,而不是在一個Servlet類。我建議通過those tutorials瞭解如何以正確的方式編寫JSP/Servlet/MVC/JDBC。