title: Radio button Selection after submitting form in jsp/servlet
body: <p>i am submitting form with radio button. and i want that radio button as checked after submitting a form. basically i am doing some searching process on page. please check code.</p>
  
  <p>on my jsp.</p>
  
  <pre><code>&lt;%@page contentType="text/html" pageEncoding="UTF-8"%&gt;
  &lt;!DOCTYPE html&gt;
  &lt;html&gt;
      &lt;head&gt;
          &lt;meta http-equiv="Content-Type" content="text/html; charset=UTF-8"&gt;
          &lt;title&gt;JSP Page&lt;/title&gt;
      &lt;/head&gt;
      &lt;body&gt;
          &lt;% 
                  String buttonvalue = (String) request.getParameter("radio");
          %&gt;
          &lt;%= buttonvalue %&gt;
  
          &lt;form method="post" action="SearchServlet"&gt;
  
              &lt;script type='text/javascript'&gt;
              var button = "&lt;%= buttonvalue %&gt;";
              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");
              }
          &lt;/script&gt;
  
              &lt;input id="rdone" type="radio" name="radio" value="one"/&gt;
              &lt;input id="rdtwo" type="radio" name="radio" value="two"/&gt;
              &lt;input id="rdthree" type="radio" name="radio" value="three"/&gt;
              &lt;input type="submit" value="Submit"/&gt;
          &lt;/form&gt;
      &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>and in Servlet.</p>
  
  <pre><code>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();
          }
  
      }
  </code></pre>
  
  <p>all working fine. but i am getting java script error. please check following screen shot. because of this java script i am not getting exact output. </p>
  
  <p><img src="https://i.stack.imgur.com/FA83H.png" alt="enter image description here"></p>