Calling a JDBC servlet onload of a form from a select option
我有一个JSP页面,其中包含用于通过JDBC作为选择选项进行数据库连接的Java Bean。现在我想使用servlet来做同样的事情。 Servlet中有JDBC连接代码。 JSP页面有一个选择选项,在表单加载时我需要数据库值。我已经搜索过,但是大多数示例都使用struts和ajax。我还不需要支柱,并且由于它处于加载状态,并且不依赖于select选项的更改,因此我无法通过它。
JSP页面(相关代码段):
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | <% //Connectivity code which works, just mentioning setAttribute pageContext.setAttribute("authors", rt); %> <form name="foo"> <td >Shipper</td> <td > <FONT COLOR=WHITE> to </FONT> <select name="database1" style="width: 150px"> <c:forEach var="item" items="${authors}"> <option> <c:out value="${item}" /> </option> </c:forEach> </select> </td> </form |
Servlet:" ZServlet.java "
1 2 3 4 5 | protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { ArrayList<String> rt = new ArrayList<String>(); //Same JDBC connection code as in JSP page beans request.setAttribute("authors", rt); } |
现在我的疑问是如何通过在表单加载期间通过将servlet的属性作者接受到select选项中来替换bean。
感谢所有帮助。
解决方案1. Servlet-> JSP
首先调用Servlet,该Servlet将作者列表设置为请求属性,然后将请求转发到JSP。
Servlet:
1 2 3 4 5 6 7 8 | protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { ArrayList<String> rt = new ArrayList<String>(); //Same JDBC connection code as in JSP page beans request.setAttribute("authors", rt); RequestDispatcher view = request.getRequestDispatcher("authors.jsp"); view.forward(request, response); } |
JSP:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | <form name="foo"> <table> <tr> <td>Shipper</td> <td><FONT COLOR=WHITE> to </FONT> <select name="database1" style="width: 150px"> <c:forEach var="item" items="${authors}"> <option> <c:out value="${item}" /> </option> </c:forEach> </select> </td> </tr> </table> </form> |
解决方案2 JSP-> Servlet
首先调用Servlet表单JSP,在其中将作者设置为请求属性,并在JSP中读取它。
Servlet:
1 2 3 4 5 | protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { ArrayList<String> rt = new ArrayList<String>(); //Same JDBC connection code as in JSP page beans request.setAttribute("authors", rt); } |
JSP:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | <jsp:include page="/authorsServlet" /> <form name="foo"> <table> <tr> <td>Shipper</td> <td><FONT COLOR=WHITE> to </FONT> <select name="database1" style="width: 150px"> <c:forEach var="item" items="${authors}"> <option> <c:out value="${item}" /> </option> </c:forEach> </select> </td> </tr> </table> </form> |
解决方案3 jQuery(AJAX)
JQuery是异步加载数据的好方法,从而可以带来更好的用户体验。在以上两种解决方案中,直到(除非)没有从Servlet返回响应(这会增加生成用户界面的延迟)时,才会加载整个JSP页面。
这是非常简单的代码。只需将响应者流中的作者姓名写为Servlet中的逗号分隔,并在返回响应时在JSP中将其拆分。
示例代码:(请阅读内联注释以获取更多信息)
Servlet:
1 2 3 4 5 6 7 8 9 | protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { ArrayList<String> rt = new ArrayList<String>(); //Same JDBC connection code as in JSP page beans PrintWriter writer=response.getWriter(); writer.print(<comma separated list of author names>); writer.flush(); writer.close(); } |
JSP:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | <script type="text/javascript"> $(document).ready(function() { // When the HTML DOM is ready loading, then execute the following function... // Handler for .load() called. $.get('servletURL', function(response) { // Execute Ajax GET request on URL of"someservlet" and execute the following function with Ajax response... alert(response); var $select = $('#database1'); // Locate HTML DOM element with ID"someselect". $select.find('option').remove(); // Find all child elements with tag name"option" and remove them (just to prevent duplicate options when button is pressed again). var items = response.split(','); for ( var i = 0; i < items.length; i++) { $('<option>').val(items[i]).text(items[i]).appendTo($select); // Create HTML <option> element, set its value with currently iterated key and its text content with currently iterated item and finally append it to the <select>. } }); }); <form name="foo" id="foo"> <table> <tr> <td>Shipper</td> <td><FONT COLOR=WHITE> to </FONT> <select id="database1" style="width: 150px"> </select></td> </tr> </table> </form> |
这个答案是总结在尝试将数据从servlet转发到JSP页面时代码中缺少的内容:
JSP页面" Index.JSP "中的相关代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | <body> <form name="foo"> <table> <tr> <td >Shipper</td> <td > <FONT COLOR=WHITE> to </FONT> <select name="database1" style="width: 150px"> <c:forEach var="item" items="${authors}"> <option> <c:out value="${item}" /> </option> </c:forEach> </select> </td> </tr> </form> </body> |
servlet中的相关代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 | public class ZServlet extends HttpServlet { private static final long serialVersionUID = 1L; //JDBC Connection code, not relevant here. ArrayList<String> rt = new ArrayList<String>(); /*"rt" holds string of column data spooled during JDBC connection*/ request.setAttribute("authors", rt); RequestDispatcher view = request.getRequestDispatcher("Index.jsp"); view.forward(request, response); } //Servlet"ZServlet" ends. |
web.xml相关代码,在标记内:丢失了,我不得不手动添加它:
1 2 3 4 | <servlet> <servlet-name>ZServlet</servlet-name> <jsp-file>/Index.jsp</jsp-file> </servlet> |
致谢。