title: Add value to a textbox dynamically
body: <p>Jsp</p>
  
  <pre><code>&lt;%@page contentType="text/html" pageEncoding="UTF-8"%&gt;
  &lt;!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
     "http://www.w3.org/TR/html4/loose.dtd"&gt;
  
  &lt;html&gt;
      &lt;head&gt;
          &lt;script type="text/javascript" language="Javascript" &gt;
  
          &lt;/script&gt;
          &lt;meta http-equiv="Content-Type" content="text/html; charset=UTF-8"&gt;
          &lt;title&gt;Add Course_Subject&lt;/title&gt;
  &lt;style type="text/css"&gt;
              &lt;!--
  body {
      background-color: #FFCCFF;
  }
  .style1 {
      color: #0066FF;
      font-weight: bold;
  }
  .style2 {font-size: 18px}
  .style17 {  font-family: "Monotype Corsiva";
      font-size: 24px;
      font-weight: bold;
      font-style: italic;
      color: #6633CC;
  }
  .style19 {color: #000099}
  .style21 {color: #000099; font-weight: bold; }
  --&gt;
  &lt;/style&gt;
  
  
      &lt;/head&gt;
      &lt;body&gt;
          &lt;jsp:include page="Log_Admin.jsp"/&gt;&lt;br/&gt;
          &lt;form action="" method="post" name="form1" id="form1"&gt;
          &lt;table width="46%" height="43" border="3" bgcolor="##CCCC99" align="center"&gt;
              &lt;tr&gt;
                  &lt;td width="85%" align="center" bgcolor="#FFFF99"&gt;&lt;label&gt;&lt;span class="style17"&gt;Course and Subject Information&lt;/span&gt;&lt;/label&gt;&lt;/td&gt;
              &lt;/tr&gt;
  
              &lt;tr&gt;&lt;td&gt;
                  &lt;table width="666" height="207" border="0" align="center" bordercolor="#F0F0F0" bgcolor="#CCCC99" &gt;
                      &lt;tr&gt;
                          &lt;td width="186" height="46" align="left"&gt;&lt;div align="left"&gt;&lt;span class="style19"&gt;
                          &lt;label&gt;&lt;strong&gt;Coourse ID&lt;/strong&gt;&lt;/label&gt;
                          &lt;/span&gt;&lt;/div&gt;&lt;/td&gt;
                          &lt;td&gt;&lt;label&gt;
                              &lt;select name="cid" size="1" id="cid" align="left" onclick="loaadCourseName()"&gt;
                              &lt;option selected="selected"&gt;None&lt;/option&gt;
                              &lt;option&gt;C001&lt;/option&gt;
                              &lt;option&gt;C002&lt;/option&gt;
                              &lt;option&gt;C003&lt;/option&gt;
                              &lt;option&gt;C004&lt;/option&gt;
                              &lt;option&gt;C005&lt;/option&gt;
                              &lt;option&gt;C006&lt;/option&gt;
                              &lt;option&gt;C007&lt;/option&gt;
                              &lt;option&gt;C008&lt;/option&gt;
                              &lt;option&gt;C009&lt;/option&gt;
                              &lt;option&gt;C010&lt;/option&gt;
                              &lt;/select&gt;
                          &lt;/label&gt;&lt;/td&gt;
                          &lt;td width="186" height="46" align="left"&gt;&lt;div align="left"&gt;&lt;span class="style19"&gt;
                          &lt;label&gt;&lt;strong&gt;Coourse Name&lt;/strong&gt;&lt;/label&gt;
                          &lt;/span&gt;&lt;/div&gt;&lt;/td&gt;
                          &lt;td width="310" align="left"&gt;&lt;input name="cname" type="text" id="cname" size="25" maxlength="50" /&gt;&lt;/td&gt;
                      &lt;/tr&gt;
  
                      &lt;tr&gt;
                          &lt;td width="186" height="46" align="left"&gt;&lt;div align="left"&gt;&lt;span class="style19"&gt;
                          &lt;label&gt;&lt;strong&gt;Subject ID&lt;/strong&gt;&lt;/label&gt;
                          &lt;/span&gt;&lt;/div&gt;&lt;/td&gt;
                          &lt;td&gt;&lt;label&gt;
                              &lt;select name="sid" size="1" id="sid" align="left"&gt;
                              &lt;option selected="selected"&gt;None&lt;/option&gt;
                              &lt;option&gt;S01&lt;/option&gt;
                              &lt;option&gt;S02&lt;/option&gt;
                              &lt;option&gt;S03&lt;/option&gt;
                              &lt;option&gt;S04&lt;/option&gt;
                              &lt;option&gt;S05&lt;/option&gt;
                              &lt;option&gt;S06&lt;/option&gt;
                              &lt;option&gt;S07&lt;/option&gt;
                              &lt;option&gt;S08&lt;/option&gt;
                              &lt;option&gt;S09&lt;/option&gt;
                              &lt;option&gt;S10&lt;/option&gt;
                              &lt;/select&gt;
                          &lt;/label&gt;&lt;/td&gt;
                          &lt;td width="186" height="46" align="left"&gt;&lt;div align="left"&gt;&lt;span class="style19"&gt;
                          &lt;label&gt;&lt;strong&gt;Subject Name&lt;/strong&gt;&lt;/label&gt;
                          &lt;/span&gt;&lt;/div&gt;&lt;/td&gt;
                          &lt;td width="310" align="left"&gt;&lt;input name="sname" type="text" id="sname" size="25" maxlength="50" /&gt;&lt;/td&gt;
                      &lt;/tr&gt;
  
                      &lt;tr&gt;
                          &lt;td&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/td&gt;
                          &lt;td&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;input name="save" type="submit" id="save" value="Save" onclick="validate(this.form)"/&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;
                          &lt;input name="reset" type="reset" id="reset" value="Reset" /&gt;&lt;/td&gt;
                      &lt;/tr&gt;
                  &lt;/table&gt;
              &lt;/td&gt;&lt;/tr&gt;
          &lt;/table&gt;
      &lt;/form&gt;
      &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>Servlet</p>
  
  <pre><code>package DBCon;
  
  import java.io.*;
  import java.net.*;
  
  import javax.servlet.*;
  import javax.servlet.http.*;
  import java.sql.*;
  /**
   *
   * @author Nayan
   */
  public class searchCourseName extends HttpServlet {
  
      /** 
      * Processes requests for both HTTP &lt;code&gt;GET&lt;/code&gt; and &lt;code&gt;POST&lt;/code&gt; methods.
      * @param request servlet request
      * @param response servlet response
      */
      protected void processRequest(HttpServletRequest request, HttpServletResponse response)
      throws ServletException, IOException {
          response.setContentType("text/html;charset=UTF-8");
          PrintWriter out = response.getWriter();
          String cname=null,courseid;
          try
          {
              Class.forName("com.mysql.jdbc.Driver").newInstance();
              Connection con=DriverManager.getConnection("jdbc:mysql://localhost/online_exam?"+"user=root&amp;password=pass");
              Statement stmt=con.createStatement();
  
              courseid=request.getParameter("cid");
              ResultSet rs=stmt.executeQuery("select course_name from course where course_id='"+courseid+"'");
  
              while(rs.next())
              {
                  cname=rs.getNString("course_name");
                  //String s=rs.getString(1);
              }
              request.getSession().setAttribute("courseName",cname);
              //RequestDispatcher requestDispatcher=getServletContext().getRequestDispatcher("http://localhost:8080/ONLINEEXAMINATION/removeCourse2.jsp");
              //requestDispatcher.forward(request,response);
  
          }
           catch(Exception e) { 
              out.println("&lt;h1&gt;"+e.getStackTrace()+"&lt;/h1&gt;");
          }
      } 
  
      // &lt;editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code."&gt;
      /** 
      * Handles the HTTP &lt;code&gt;GET&lt;/code&gt; method.
      * @param request servlet request
      * @param response servlet response
      */
      protected void doGet(HttpServletRequest request, HttpServletResponse response)
      throws ServletException, IOException {
          processRequest(request, response);
      } 
  
      /** 
      * Handles the HTTP &lt;code&gt;POST&lt;/code&gt; method.
      * @param request servlet request
      * @param response servlet response
      */
      protected void doPost(HttpServletRequest request, HttpServletResponse response)
      throws ServletException, IOException {
          processRequest(request, response);
      }
  
      /** 
      * Returns a short description of the servlet.
      */
      public String getServletInfo() {
          return "Short description";
      }
      // &lt;/editor-fold&gt;
  }
  </code></pre>
  
  <p>Now when I select an item in the dropdown <code>cid</code> I want to display the corresponding <code>courname</code> in the <code>cname</code> textfield. How can I achieve this?</p>