从选择选项调用JDBC servlet加载表单



我有一个JSP页面,其中包含用于使用JDBC进行DB连接的java bean。现在我想用servlet做同样的事情。JDBC连接代码在Servlet中。JSP页面有一个选择选项,我需要在表单加载它的数据库值。我已经搜索过了,但大多数例子都是使用struts和ajax。我还不需要struts,因为它是在表单加载上,不依赖于选择选项的改变,我无法通过它。

JSP页面(相关代码段):
<%
//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"

    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接受属性作者到选择选项来替换bean。

感谢所有的帮助。

解决方案1。Servlet -> JSP

只需首先调用将作者列表设置为请求属性的Servlet,然后将请求转发给JSP。

Servlet:

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:

<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,将authors设置为请求属性,并在JSP中读取它。

Servlet:

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:

<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>

方案三jQuery (AJAX)

JQuery是一种异步加载数据的好方法,可以带来更好的用户体验。在上述两种解决方案中,整个JSP页面将不会加载,除非Servlet没有返回响应,从而增加了延迟以生成用户界面。

这是非常简单的代码。只需在Servlet中将响应流中的作者名称以逗号分隔,并在返回响应时将其在JSP中拆分即可。

示例代码:(读取内联注释以获取更多信息)

Servlet:

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:

<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>.
            }
        });
    });
</script>
<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":

<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中的相关代码:

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相关的代码片段,在标签内:这是缺失的,我不得不手动添加它:

<servlet>
          <servlet-name>ZServlet</servlet-name>
          <jsp-file>/Index.jsp</jsp-file>
    </servlet>

问候。

最新更新