如何从jsp下拉框中的mySQL数据库中检索和显示图像作为图标

时间:2017-01-16 06:08:30

标签: mysql jsp servlets model-view-controller

我被困住了:(你能帮我看看!!!

所以我想创建这样的东西: DropDown Box with icon and text

但是我被卡住了,我不确切知道如何从数据库中检索图像并将其显示在下拉框中,但我尝试过这样的事情,

我的JSP页面:

<%@ page import="java.util.ArrayList" %>
<!DOCTYPE HTML><%@page language="java"contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>
<html>
<head>
<title>BudgetAdd</title>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<script>
function submitForm() {
    document.forms[0].action="BudgetAddServlet2";
    document.forms[0].method="POST";
    document.forms[0].submit();
}

</script>
</head>
<body>      
    <div id="BudgetMain">
    <div id="BudgetAddMain">
            <form action="/FinancialPlanner/BudgetAddServlet" method="post">
                <table width="100%" border="0" cellpadding="0" cellspacing="0">
                    <tbody>
                    <tr>
                            <td class="BudgetAdd_tb">Month</td>
                            <td class="BudgetAdd_tb2"><select name="ddlMonth">
                                    <option value="Jan">Jan</option>
                                    <option value="Feb">Feb</option>
                                    <option value="Mar">Mar</option>
                                    <option value="Apr">Apr</option>
                                    <option value="May">May</option>
                                    <option value="Jun">Jun</option>
                                    <option value="Jul">Jul</option>
                                    <option value="Aug">Aug</option>
                                    <option value="Sep">Sep</option>
                                    <option value="Oct">Oct</option>
                                    <option value="Nov">Nov</option>
                                    <option value="Dec">Dec</option>
                            </select>
                            </td>
                        </tr>
                        <tr>
                            <td class="BudgetAdd_tb">Budget Name</td>
                            <td class="BudgetAdd_tb2">
                            <select name="ddlBudgetName" id="ddlBudgetName">
                            <% ArrayList<String> categoryList = (ArrayList<String>)session.getAttribute("categoryList"); %>
                            <% for (String category:categoryList) { 
                            %>
                            <option value="<%=category %>"> <img src=''/> <%=category %></option>
                            <%} %>


                            </select></td>
                        </tr>

                        <tr>
                            <td class="BudgetAdd_tb">Amount</td>
                            <td class="BudgetAdd_tb2">$ <input type="text" name="tbAmt" size="20" required></td>

                        </tr>

                        <tr>
                            <td class="BudgetAdd_tb">Note</td>
                            <td class="BudgetAdd_tb2"><textarea rows="2" cols="100" name="tbNote"></textarea>
                            </td>
                        </tr>
                    </tbody>
                </table>
                <input type="submit" id="confirm" name="btnSubmit" value="Add" class="confirmb" style="clear: right; height: 40px; background-color: #1c8942; vertical-align: middle; width: 120px; margin-top: 5px; float: right; text-align: center">
            </form>
        </div>
    </div>
</body>
</html>

我的servlet:

import java.io.IOException;
import java.io.InputStream;
import java.sql.Blob;
import java.sql.ResultSet;
import java.util.ArrayList;
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 FinancialPlanner.DBConn.DBController;

/**
 * Servlet implementation class BudgetAddServlet2
 */
@WebServlet("/BudgetAddServlet2")
public class BudgetAddServlet2 extends HttpServlet {
    private static final long serialVersionUID = 1L;

    /**
     * @see HttpServlet#HttpServlet()
     */
    public BudgetAddServlet2() {
        super();
        // TODO Auto-generated constructor stub
    }

    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
    }

    /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
         InputStream sImage;

        try {
            DBController dbController = new DBController();
            dbController.getConnection();
            String dbQuery="select DISTINCT categoryName, categoryIcon FROM category" ;
//          dbQuery=dbQuery+ddlBudgetName +"')";
            System.out.println(dbQuery);
            ResultSet rs = dbController.readRequest(dbQuery);
            //ArrayList<String> categoryList;
            ArrayList<String> categoryList = new ArrayList<String>();
            //Iterate through the resultset
            while (rs.next())
        {

            String budgetCat = rs.getString(1);
            String budgetIco = rs.getString(2);
            categoryList.add(budgetCat);
            categoryList.add(budgetIco);


            }
            request.getSession().setAttribute("categoryList", categoryList);
        } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
        } 

        //Create an instance of the MovieBookingBean class
        request.getRequestDispatcher("BudgetAdd.jsp").forward(request, response);
    }

}

我的数据库: Database(I have saved the URL already)

所以,我在JSP上遇到了如何检索arraylist并将其显示在我的JSP页面dropdownList上的问题。

0 个答案:

没有答案