2013-04-11 163 views
0

我使用單選按鈕提交表單。我希望在提交表單後檢查單選按鈕。基本上我正在頁面上進行一些搜索過程。請檢查代碼。在jsp/servlet中提交表單後的單選按鈕選擇

在我的jsp上。

<%@page contentType="text/html" pageEncoding="UTF-8"%> 
<!DOCTYPE html> 
<html> 
    <head> 
     <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 
     <title>JSP Page</title> 
    </head> 
    <body> 
     <% 
       String buttonvalue = (String) request.getParameter("radio"); 
     %> 
     <%= buttonvalue %> 

     <form method="post" action="SearchServlet"> 

      <script type='text/javascript'> 
      var button = "<%= buttonvalue %>"; 
      if(button == "one") 
      { 
       alert("1"); 
       document.getElementById("rdone").checked = true; 
       document.getElementById("rdtwo").checked = false; 
       document.getElementById("rdthree").checked = false; 
      } 
      else if(button == "two") 
      { 
       document.getElementById("rdone").checked = false; 
       document.getElementById("rdtwo").checked = true; 
       document.getElementById("rdthree").checked = false; 
      } 
      else if(button == "three") 
      { 
       alert("3"); 
       document.getElementById("rdone").checked = false; 
       document.getElementById("rdtwo").checked = false; 
       document.getElementById("rdthree").checked = true; 
       alert("2"); 
      } 
      else 
      { 
        alert("4"); 
      } 
     </script> 

      <input id="rdone" type="radio" name="radio" value="one"/> 
      <input id="rdtwo" type="radio" name="radio" value="two"/> 
      <input id="rdthree" type="radio" name="radio" value="three"/> 
      <input type="submit" value="Submit"/> 
     </form> 
    </body> 
</html> 

和在Servlet中。

protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException 
    { 
     response.setContentType("text/html;charset=UTF-8"); 
     PrintWriter out = response.getWriter(); 
     try 
     { 
      System.out.println(request.getParameter("radio")); 
      RequestDispatcher dispatcher = request.getRequestDispatcher("/radiobutton.jsp"); 
      System.out.println("--------------"+request.getParameter("radio")); 
      dispatcher.forward(request, response); 
     } 
     finally 
     {    
      out.close(); 
     } 

    } 

所有工作正常。但我得到的Java腳本錯誤。請檢查以下屏幕截圖。由於這個Java腳本我沒有得到確切的輸出。

enter image description here

+2

* *危險**:您正在將URL中的數據直接輸出到頁面中。這使你容易受到[XSS攻擊](http://en.wikipedia.org/wiki/Cross-site_scripting)的影響。 – Quentin 2013-04-11 10:05:57

回答

1

你的腳本之前的單選按鈕出現,而不是由該被延遲,直到後來的函數。

您試圖在按鈕存在之前對其進行修改。

將腳本移動到HTML源代碼中的按鈕之後。

+0

準確答案。謝謝。 – KSHiTiJ 2013-04-11 10:07:40

0

我想建議你,當你的JSP頁面提交您可以使用下面的代碼處理它的servlet:

protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException 
     { 
      response.setContentType("text/html;charset=UTF-8"); 
      PrintWriter out = response.getWriter(); 
      try 
      { 
       System.out.println(request.getParameter("radio")); 
if(request.getParameter("radio")!= "" || request.getParameter("radio")!=null){ 
request.setAttribute("radio"); 
} 
       RequestDispatcher dispatcher = request.getRequestDispatcher("/radiobutton.jsp"); 
       System.out.println("--------------"+request.getParameter("radio")); 
       dispatcher.forward(request, response); 
      } 
      finally 
      {    
       out.close(); 
      } 

     } 

現在在你的JSP文件中添加以下行:

<input id="rdone" type="radio" name="radio" value="one" <%=((request.getAttribute("radio")!=null && request.getAttribute("radio").toString().equals("one")) ? "checked" : "")%> /> 
<input id="rdtwo" type="radio" name="radio" value="two" <%=((request.getAttribute("radio")!=null && request.getAttribute("radio").toString().equals("two")) ? "checked" : "")%> /> 
<input id="rdthree" type="radio" name="radio" value="three" <%=((request.getAttribute("radio")!=null && request.getAttribute("radio").toString().equals("three")) ? "checked" : "")%> /> 
相關問題