Note that there are some explanatory texts on larger screens.

plurals
  1. POIs it Possible to Populate a Dropdown (In HTML/JSP) From a Servlet?
    primarykey
    data
    text
    <p>Okay, so I had created my first jsp page which basically created 3 drop downs and populated them with information pulled from a database.</p> <p>However, I was told that this was bad code and I should use a servlet for that database function and error handling, and let the jsp strictly do the displaying.</p> <p>The original jsp code is below:</p> <pre><code>&lt;%@page import="java.sql.*"%&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;meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"&gt; &lt;title&gt;Code Selector&lt;/title&gt; &lt;/head&gt; &lt;body&gt; &lt;h1&gt;Please select the applicable codes:&lt;/h1&gt; &lt;select name='Code' onchange="showState(this.value)"&gt; &lt;option value="none"&gt;Select a code&lt;/option&gt; &lt;% //Pulls the ids and decriptions from the codes table and stores them in the first drop down try { Class.forName("driverName").newInstance(); Connection con = DriverManager.getConnection("serverURL","username","password"); Statement stmt = con.createStatement(); ResultSet rs = stmt.executeQuery("select id, descr from codes"); while(rs.next()) { %&gt; &lt;option value="&lt;%=rs.getString(1)%&gt;"&gt;&lt;%=rs.getString(1)%&gt; &lt;%=rs.getString(2)%&gt;&lt;/option&gt; &lt;% } //Closes the database connection stmt.close(); con.close(); } catch (ClassNotFoundException e) { System.err.println("ClassNotFoundException: " + e.getMessage()); } catch (SQLException e) { System.err.println("SQLException: " + e.getMessage()); } catch (Exception e) { System.err.println("Generic Exception: " + e.getMessage()); } %&gt; &lt;/select&gt; &lt;br&gt; &lt;br&gt; &lt;select name='Code2' onchange="showState(this.value)"&gt; &lt;option value="none"&gt;Select a code&lt;/option&gt; &lt;% //Pulls the ids and decriptions from the codes table and stores them in the second drop down try { Class.forName("driverName").newInstance(); Connection con = DriverManager.getConnection("serverURL","username","password"); Statement stmt = con.createStatement(); ResultSet rs = stmt.executeQuery("select id, descr from codes"); while(rs.next()) { %&gt; &lt;option value="&lt;%=rs.getString(1)%&gt;"&gt;&lt;%=rs.getString(1)%&gt; &lt;%=rs.getString(2)%&gt;&lt;/option&gt; &lt;% } //Closes the database connection stmt.close(); con.close(); } catch (ClassNotFoundException e) { System.err.println("ClassNotFoundException: " + e.getMessage()); } catch (SQLException e) { System.err.println("SQLException: " + e.getMessage()); } catch (Exception e) { System.err.println("Generic Exception: " + e.getMessage()); } %&gt; &lt;/select&gt; &lt;br&gt; &lt;br&gt; &lt;select name='otherCode' onchange="showState(this.value)"&gt; &lt;option value="none"&gt;Select a other code&lt;/option&gt; &lt;% //Pulls the ids and decriptions from the other codes table and stores them in the third drop down try { Class.forName("driverName").newInstance(); Connection con = DriverManager.getConnection("serverURL","username","password"); Statement stmt = con.createStatement(); ResultSet rs2 = stmt.executeQuery("select id, descr from other_codes"); while(rs2.next()) { %&gt; &lt;option value="&lt;%=rs2.getString(1)%&gt;"&gt;&lt;%=rs2.getString(1)%&gt; &lt;%=rs2.getString(2)%&gt;&lt;/option&gt; &lt;% } //Closes the database connection stmt.close(); con.close(); } catch (ClassNotFoundException e) { System.err.println("ClassNotFoundException: " + e.getMessage()); } catch (SQLException e) { System.err.println("SQLException: " + e.getMessage()); } catch (Exception e) { System.err.println("Generic Exception: " + e.getMessage()); } %&gt; &lt;/select&gt; &lt;br&gt; &lt;br&gt; &lt;form method = "post"&gt; &lt;input type="submit" value="Submit"&gt; &lt;% try { String Code = request.getParameter("Code"); String Code2 = request.getParameter("Code2"); String otherCode = request.getParameter("otherCode"); Class.forName("driverName").newInstance(); Connection con = DriverManager.getConnection("serverURL","username","password"); Statement stmt = con.createStatement(); //ResultSet rs3 = stmt.executeQuery(); System.out.println("This is the first code: " + Code); System.out.println("This is the second code: " + Code2); System.out.println("This is the other code: " + otherCode); con.close(); stmt.close(); } catch (ClassNotFoundException e) { System.err.println("ClassNotFoundException: " + e.getMessage()); } catch (SQLException e) { System.err.println("SQLException: " + e.getMessage()); } catch (Exception e) { System.err.println("Generic Exception: " + e.getMessage()); } %&gt; &lt;script&gt; window.close(); &lt;/script&gt; &lt;/form&gt; &lt;/body&gt; &lt;/html&gt; </code></pre> <p>And so far, this is what I have with the new jsp and servlet page:</p> <p><strong>codes-selector.jsp</strong></p> <pre><code>&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;TITLE&gt; Codes &lt;/TITLE&gt; &lt;/HEAD&gt; &lt;BODY BGCOLOR="#FDF5E6"&gt; &lt;H2 ALIGN="CENTER"&gt; Please select the applicable codes: &lt;/H2&gt; &lt;FORM ACTION="http://localhost:8088/SomeProgram" METHOD="GET"&gt; &lt;CENTER&gt; &lt;select name='code' onchange="showState(this.value)"&gt; &lt;option value="none"&gt;Select a code&lt;/option&gt; &lt;/select&gt; &lt;BR&gt; &lt;BR&gt; &lt;select name='code2' onchange="showState(this.value)"&gt; &lt;option value="none"&gt;Select a code&lt;/option&gt; &lt;/select&gt; &lt;BR&gt; &lt;BR&gt; &lt;select name='otherCode' onchange="showState(this.value)"&gt; &lt;option value="none"&gt;Select an other code&lt;/option&gt; &lt;/select&gt; &lt;BR&gt; &lt;BR&gt; &lt;!-- Press this to submit form --&gt; &lt;INPUT TYPE="SUBMIT" VALUE="Submit"/&gt; &lt;/CENTER&gt; &lt;/FORM&gt; &lt;/BODY&gt; &lt;/HTML&gt; </code></pre> <p><strong>PullCodes.java (servlet)</strong>:</p> <pre><code>package com.firstservlet.alfresco; import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.sql.*; /** * Servlet implementation class PullCodes */ @WebServlet("/PullCodes") public class PullCodes extends HttpServlet { private static final long serialVersionUID = 1L; /** * @see HttpServlet#HttpServlet() */ public PullCodes() { super(); } /** * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) */ protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String code = request.getParameter("code"); String code2 = request.getParameter("code2"); String otherCode = request.getParameter("otherCode"); try { Class.forName("driverName").newInstance(); Connection con = DriverManager.getConnection("url","username","password"); Statement stmt = con.createStatement(); ResultSet rs = stmt.executeQuery("select id, descr from ref_codes"); ResultSet rs2 = stmt.executeQuery("select id, descr from ref_other_codes"); try { while(rs.next()) { //Is this correct? code+=("&lt;option value=\"" + rs.getString(1) + "\"&gt;" + rs.getString(1) + " " + rs.getString(2) + "&lt;/option&gt;"); } //Closes the database connection stmt.close(); con.close(); } catch (Exception e) { System.err.println("Insertion Exception: " + e.getMessage()); } } catch (ClassNotFoundException e) { System.err.println("ClassNotFoundException: " + e.getMessage()); } catch (SQLException e) { System.err.println("SQLException: " + e.getMessage()); } catch (Exception e) { System.err.println("Generic Exception: " + e.getMessage()); } } /** * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) */ protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub } } </code></pre> <p>So now, at this point, I'm not sure how to access that dropdown list element from my html page. And Googling around, I was not seeing anything that appeared to show this being done. Also, from what I read it seems most of the time a servlet is just processing information from the html/jsp page. What I'm doing right now is <code>code+=("&lt;option value=\"" + rs.getString(1) + "\"&gt;" + rs.getString(1) + " " + rs.getString(2) + "&lt;/option&gt;");</code>. Is that correct? And if so, how do I link that with the html/jsp page? Or is it not even possible to access that html page on load and populate it using the servlet?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload