Java servlet有两个属性,一个是servlet,一个是重定向

时间:2018-04-04 18:04:32

标签: java jsp servlets

这是我的第一个问题。我很抱歉,如果已经回答但我不知道该怎么问? 我的问题是,在网页上我有大桌子,我正在做一些排序列表。我使用" onselect"创建列表。将值发送给servlet。 Servlet需要接受第一个或第二个属性,而不是创建新的session.setAttribute。 也许如果你查看我的代码,你可以理解我的意思:

servlet代码:

package servlets;

import baza.Baza;
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 javax.servlet.http.HttpSession;

@WebServlet(name = "FilterServlet", urlPatterns = {"/FilterServlet"})
public class FilterServlet extends HttpServlet {

@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {

}

@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    String filterDobavljac = request.getParameter("grupa1");
    String filterKategorija = request.getParameter("grupa2");

    System.out.println("FilterDobavljac:" + "'" + filterDobavljac + "'");
    System.out.println("FilterKategorija:" + "'" + filterKategorija + "'");

    HttpSession sesija = request.getSession(true);

    if (!filterDobavljac.equals("ALL") || !filterDobavljac.equals("")) {

        sesija.setAttribute("proizvodi", Baza.FiltriranjeProizvodaGrupa1(filterDobavljac).values());
    }

    if (!filterKategorija.equals("ALL") || !filterKategorija.equals("")) {

        sesija.setAttribute("proizvodi", Baza.FiltriranjeProizvodaGrupa2(filterKategorija).values());
    }

    System.out.println("FilterDobavljac:" + "'" + filterDobavljac + "'");
    System.out.println("FilterKategorija:" + "'" + filterKategorija + "'");
    response.sendRedirect("proizvodi.jsp");
}
}

System.out.println  - are here only for fast debug

数据库类的一部分:

public static Hashtable<Integer,Proizvod> FiltriranjeProizvodaGrupa1(String filter){
    System.out.println(filter);
    Hashtable<Integer,Proizvod> proizvodiFiltered = new Hashtable<Integer, Proizvod>();

    if (!filter.equals("ALL")) {
        for (Proizvod p : proizvodi.values()) {
            if (p.getGrupa1().getNaziv().equals(filter)) {
                proizvodiFiltered.put(p.getId(), p);
            }
        }return proizvodiFiltered;
    }
    return proizvodi;
}

另一个Hashtable的代码是相同的 这是网络部分:

        <form action="FilterServlet" method="post" name="productForm">

        <table class="center style">
            <tr>
                <th>Bar kod</th>
                <th>Sifra</th>
                <th>Naziv</th>
                <th>kolicina</th>
                <th>Dobavljac</th>
                <th>Kategorija</th>
                <th>Cena</th>
            </tr>
            <tr>
                <td>
                    <select>
                        <c:forEach items="${proizvodi}" var="m">
                            <option style="display:none"></option>
                            <option value="${m.barkod}">
                                ${m.barkod}
                            </option>
                        </c:forEach>
                    </select>
                </td>
                <td>
                    <select>
                        <c:forEach items="${proizvodi}" var="m">
                            <option style="display:none"></option>
                            <option value="${m.sifra}">
                                ${m.sifra}
                            </option>
                        </c:forEach>
                    </select>
                </td>
                <td>
                    <select>
                        <c:forEach items="${proizvodi}" var="m">
                            <option style="display:none"></option>
                            <option value="${m.naziv}">
                                ${m.naziv}
                            </option>
                        </c:forEach>
                    </select>
                </td>
                <td></td>
                <td>
                    <select id="grupa1" name="grupa1" onchange="return setValue();">
                        <option style="display:none"></option>
                        <option>ALL</option>
                        <c:forEach items="${grupa1Filter}" var="g1">
                            <option value="${g1}">
                                ${g1}
                            </option>
                        </c:forEach>
                    </select>
                </td>
                <td>
                    <select id="grupa2" name="grupa2" onchange="return setValue();">
                        <option style="display:none"></option>
                        <option>ALL</option>
                        <c:forEach items="${grupa2Filter}" var="g2">
                            <option value="${g2}">
                                ${g2}
                            </option>
                        </c:forEach>
                    </select>
                </td>
                <td></td>

            </tr>
            <c:forEach items="${proizvodi}" var="p">
                <tr>
                    <td>${p.barkod}</td>
                    <td>${p.sifra}</td>
                    <td>${p.naziv}</td>
                    <td>${p.kolicina}</td>
                    <td>${p.grupa1.naziv}</td>
                    <td>${p.grupa2.naziv}</td>
                    <td>${p.cena}</td>
                </tr>
            </c:forEach>
        </table>
        <input type="hidden" name="dropdown" id="dropdown">
        <input type="submit" value="Filter" name="btn_dropdown">
    </form>
</body>

我有两个不同的属性,这不是问题。 当我选择第一个属性&#34; grupa1&#34;时,问题出在servlet中。下拉列表我得到空表但当我选择第二个&#34; grupa2&#34;一切都好。

JavaScript代码:

function setValue() {
document.getElementById("dropdown").value = document.getElementById("grupa1").value;
document.getElementById("dropdown").value = document.getElementById("grupa2").value;
document.productForm.submit();
return true;
}

0 个答案:

没有答案