2017-08-22 271 views
0

我在寫一個jsp文件代碼,它可以動態地創建下拉菜單;在dao.QueriesDAO java類中執行的查詢之後,菜單上的條目被插入。此外,還有一個搜索欄。如何從JSP中獲取值到Servlet中<option>

我想從菜單中所有選擇的聲音,加上插在搜索欄的字符串,發送到servlet SearchServelt.java,包含在src/controller/SearchServlet.javaSearch按鈕,它被點擊之後。

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上看到類似的問題,並且我使用了所提出的解決方案。

我想知道我該做什麼錯誤,對於這樣的問題應該是一個好方法,非常感謝。

homeView.jsp文件從不同的servlet HomeServlet.java調用。我應該使用該servlet而不是新的servlet SearchServlet.java?什麼是更好的?

編輯:

我與<form action="${pageContext.request.contextPath}/search" method="get">在JSP頁面解析(以及在具有單一form改性的),並且相應地我改變了SearchServlet.java方法從doPostdoGet

回答

0

你需要一個形式添加到搜索輸入

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

當搜索用戶點擊按鈕,它就會被提交到servlet「SearchServlet」的POST請求。在那裏你可以得到包含用戶輸入的「搜索」參數名稱。

<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} 
+0

爲了異步做到這一點(不刷新頁面),我建議你先學會如何做到這一點同步(與頁面刷新),因爲判斷通過代碼和問題,你需要首先學習如何去做。 –

+0

我已經更新了問題和代碼;我應該使用相同的servlet來調用響應的jsp文件嗎?這是一個更好的方法嗎?如何在選擇按鈕後獲取servlet中的值?目前,該代碼似乎不起作用;當我點擊搜索按鈕時,出現錯誤404。 – Pleasant94

+0

這取決於你。儘管分離邏輯是一種很好的做法。如果您的搜索視圖頁面中包含用戶可以選擇的選項,則它將更加有組織,以便具有Servlet將請求屬性轉發到的結果視圖頁面。在通過執行表單提交表單後,您可以從servlet獲取值:'String search = request.getParameter(「search」); ' 它從窗體中的輸入字段獲取'name'參數。您想要獲得的輸入必須在表單內。 404發生是因爲你的servlet被映射到@WebServlet(urlPatterns = {「/ search」}) –

相關問題