如何在<option>中从JSP到Servlet获取值

时间:2017-08-22 16:14:17

标签: java jsp servlets

我正在编写一个动态创建下拉菜单的jsp文件代码;在dao.QueriesDAO java类中执行查询后,菜单上的条目以动态方式插入。此外,还有一个搜索栏。

我希望菜单中的所有选定语音以及搜索栏中插入的字符串都会发送到SearchServelt.java中包含的servlet src/controller/SearchServlet.java,在Search按钮之后#39;点击了。

JSP文件(在WebContent/jsp/homeView.jsp中):

<%@ page language="java" contentType="text/html; charset=UTF-8"
  pageEncoding="UTF-8" import="java.util.List, java.util.Iterator" %>

<!DOCTYPE html>

<html>

    <head></head>

    <body>

        <jsp:include page="_header.jsp"></jsp:include>
        <jsp:include page="_menu.jsp"></jsp:include>

        <div style = "text-align: center">

            <form action="/Search" method="post">

            Search <input name="search"> <input type="submit" value="Search"/>

            </form>

        </div>

        <div style = "text-align: center">

            <%-- select value brand from drop-downlist --%>
            <div style = "display: inline-block">
            <%
                List<String> brands = dao.QueriesDAO.getBrands();
                Iterator<String> iterBrands = brands.iterator();
            %>
            <form name="f1" method="post" action="/Search">
                Select brand:
                <select name="brand">
                    <option value="All">All</option>
                    <%  while(iterBrands.hasNext()){ %>
                    <option><%= (String) iterBrands.next() %></option>
                     <% } %>
                </select>
            </form>
            </div>

            <%-- select value of instrument type from drop-downlist --%>
            <div style = "display: inline-block">
            <%
                List<String> instrumentTypes = dao.QueriesDAO.getInstrumentType();
                Iterator<String> iterInstrumentTypes = instrumentTypes.iterator();
            %>
            <form name="f2" method="post" action="/Search">
                Select instrument type:
                <select name="instrumentType">
                    <option value="All">All</option>
                    <%  while(iterInstrumentTypes.hasNext()){ %>
                    <option><%= (String) iterInstrumentTypes.next() %></option>
                     <% } %>
                </select>
            </form>
            </div>

            <%-- select value used from drop-downlist --%>
            <div style = "display: inline-block">
            <form name="f3" method="post" action="/Search">
                Select used status:
                <select name="used">
                    <option value="0">All</option>
                    <option value="false">Not used</option>
                    <option value="true">used</option>
                </select>
            </form>
            </div>

            <%-- select value product type from drop-downlist --%>
            <div style = "display: inline-block">
            <form name="f4" method="post" action="/Search">
                Select product type:
                <select name="productType">
                    <option value="All">All</option>
                    <option value="2">Professional product</option>
                    <option value="1">Scholastic product</option>
                    <option value="0">Classic product</option>
                </select>
            </form>
            </div>

        </div>

        <jsp:include page="_footer.jsp"></jsp:include>

    </body>

</html>

Servlet文件:

package controller;

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;

@WebServlet(urlPatterns = { "/search"})
public class SearchServlet extends HttpServlet {

    private static final long serialVersionUID = -1953084286713579746L;

    public SearchServlet() {
        super();
    }

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

        String searchParameters= request.getParameter("search"); 

        String brandSelected= request.getParameter("brand"); 
        String selectedInstrumentType= request.getParameter("instrumentType"); 
        String selectedUsedStatus= request.getParameter("used"); 
        String selectedProductType= request.getParameter("productType");

        System.out.println("Inserted: " + searchParameters + ", "
                            + brandSelected + ", "
                            + selectedInstrumentType + ", "
                            + selectedUsedStatus + ", "
                            + selectedProductType + ".");


    }

}

我希望能够使用servlet中的值,然后最终调用其他java方法和/或jsp文件。

我不知道出了什么问题,因为我在stackoverflow上看到了类似的问题,并且我使用了所提出的解决方案。

我想知道我做错了什么,应该对这样的问题采取什么样的方法,非常感谢你。

从另一个servlet homeView.jsp调用HomeServlet.java文件。我应该使用该servlet而不是新的servlet SearchServlet.java吗?什么更好?

编辑:

我在JSP页面中使用<form action="${pageContext.request.contextPath}/search" method="get">解决(并在单个form中进行了修改),因此我将SearchServlet.java方法从doPost更改为{{1 }}

1 个答案:

答案 0 :(得分:0)

您需要在搜索输入中添加表单

    Search <input name="search"> <input type="submit" name="submit" value="Search"/>

当用户点击搜索按钮时,它会被提交到servlet“ SearchServlet ”的发布请求。在那里,您将获得“搜索”参数名称,该名称将包含用户的输入。

    <form action="SearchServlet" method="post">

            Search <input name="search"> 
            <input type="submit" value="Search"/>

// here you can add other inputs like brand selected, instrumentType, productType etc...

    </form>

然后从servlet中使用用户输入查询数据库并设置结果。

   protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 

        String brandSelected= request.getParameter("search"); 
        //if you add more options in the form you can get those also

       //query database and get arraylist of instrumentTypes by brand. 
       List<String> instrumentTypes = dao.QueriesDAO.getInstrumentType(brandSelected);


        //set attribute for jsp
        request.setAttribute("instrumentTypes", instrumentTypes);

       //add the name of the jsp file you want to view the attribute you just set
      RequestDispatcher rd = request.getRequestDispatcher("searchview.jsp");
        rd.forward(request, response);


    }

}

在JSP中查看属性,执行:

${instrumentTypes}