簡體   English   中英

如何將參數從jsp頁面傳遞到servlet?

[英]How to pass parameter from a jsp page to a servlet?

我的問題是我在servlet類中創建了一個ArrayList,該類由html頁面內的表單填充。 然后,我將數組列表傳遞給一個打印所有對象的jsp頁面。 現在,我打印的每個對象都變成了一個“ href”,它調用了servlet的“ doGet”方法。 我需要通過單擊鏈接來傳遞所選對象的索引。

//這是我的servlet的doGet方法的實現:

//我知道使用getAttribute是錯誤的,但是我不知道還有什么可以真正起作用。

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

    **String ciao = request.getAttribute("id");** //this is the error
    int k = Integer.parseInt(ciao); // this is because i pass a String object and i'm already sure that it will be a number.
    String invio = lista.get(k);
    request.setAttribute("key", invio);
    RequestDispatcher dispatcher =
            getServletContext().getRequestDispatcher("/views/es3-item.jsp");
            dispatcher.forward(request, response);
    }

這是我的jsp(es3-list.jsp)頁面,用於打印對象:

<c:forEach  var="valore" items="${requestScope.message}" varStatus="theCount">//message is the key to pass the ArrayList "lista" to this jsp page.
<a href ="./es3"> <c:out value="${valore}"/> </a>
<a id="${theCount.index}"></a>
</c:forEach> 

您可以只將參數附加到請求url。 我看到您使用的是doGet,因此您可以在問號后附加參數,例如

myURL?param1=value1&param2=value2

以上是錨標簽的href。 您只需要如上所述創建href。 但是,您可以將表單提交給doGEt,例如

<form action="myservlet">
<input type="text" name="param1"/>  
<input type="text" name="param2"/>      
 </form>

在這兩種情況下,都可以從servlet訪問值,如下所示:

request.getParameter("param1");

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM