从select选项调用表单的JDBC servlet onload

时间:2014-06-20 10:44:31

标签: jsp servlets jdbc

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

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

感谢所有帮助。

2 个答案:

答案 0 :(得分:1)

解决方案1. Servlet - &gt; 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 - &gt;的Servlet

首先调用Servlet表单JSP,将authors设置为request属性,并在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>

解决方案3 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>

答案 1 :(得分:0)

这个答案是总结在尝试将数据从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>

问候。