2017-08-22 13 views
0

私は動的にドロップダウンメニューを作成するJSPファイルコードを書いています。メニューのエントリは、dao.QueriesDAO javaクラスで実行されたクエリの後にdinamically挿入されます。さらに、検索バーがあります。JSPからサーブレットへの値の取得方法<option>

は、私はすべてのメニューから選択した声、プラス検索バーに挿入された文字列は、それがクリックされていますSearchボタンの後、src/controller/SearchServlet.javaに含まれるサーブレットSearchServelt.java、に送られたいです。 (WebContent/jsp/homeView.jsp中)

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> 

サーブレットファイル:私は、サーブレットから値を処理できるようにしたいと、最終的に他のJavaメソッドを呼び出す

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 + "."); 


    } 

} 

および/またはjspファイル。

私はstackoverflowで同様の質問を見て、提案されたソリューションを利用したので、何が間違っているのか分かりません。

私は何が間違っているのかを知りたいと思います。このような問題に対しては、どのようなアプローチが良いか、非常にありがとうございます。

homeView.jspファイルは、別のサーブレットから呼び出されます。新しいサーブレットの代わりにそのサーブレットを使用する必要がありますSearchServlet.java?何が良いですか?

EDIT:

Iは、(単一formを有するで修飾)JSPページで<form action="${pageContext.request.contextPath}/search" method="get">で解決し、それに応じて私はdoPostからdoGetSearchServlet.java方法を変更しました。あなたは、ユーザーが検索ボタンをクリックすると、それはサーブレットのPOSTリクエストに提出します

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

検索入力にフォームを追加する必要が

答えて

0

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> 

次に、サーブレットからユーザー入力でデータベースにクエリを実行し、結果を設定します。

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

質問とコードを更新しました。応答のjspファイルを呼び出す同じサーブレットを使用する必要がありますか?これはより良いアプローチですか?ボタンを選択した後、サーブレットの値を取得するにはどうすればよいですか?今のところ、そのコードは機能していないようです。検索ボタンをクリックすると、エラー404が表示されます。 – Pleasant94

+0

それはあなた次第です。しかし、ロジックを分けることは良い習慣です。ユーザーが選択するオプションを含む検索ビューページがある場合、サーブレットが要求属性を転送する結果ビューページを持つように構成されます。フォームが送信された後、サーブレットから次のようにして値を取得します。 'String search = request.getParameter( "search"); ' フォームの入力フィールドから' name 'パラメータを取得します。取得する入力は、フォーム内にある必要があります。あなたのサーブレットが@WebServlet(urlPatterns = {"/ search"}) –

関連する問題