2014-01-17 55 views
-1

考慮下面的JSP:如何將JSP中的抓取值傳遞給Servlet?

<!-- Client's Page - permissions --> 

<!DOCTYPE html> 
<html> 
<head><title>Product Inventory Program</title> 
<link rel="stylesheet" 
     href="./css/styles.css" 
     type="text/css"/> 

<link rel="stylesheet" 
     href="./css/bar.css" 
     type="text/css"/>   
</head> 

<div id ="right" class="bar"> 
<a href="loggingOut">LOG-OUT</a> 
</div> 

<body> 
<table class="title"> 
    <tr><th>Client's page - Please choose one of the options below</th></tr> 
</table> 
<body> 

<h1>Hello ${name.firstName} ${name.lastName} , You've logged in successfully!</h1> 
<h1> 
Please choose one of the following options 
</h1> 



<!-- Client Transactions --> 
<fieldset> 
    <legend>View all the products in the store</legend> 
    <form action="blablabla"> 
    <a href="clientAction1">Press here to continue</a>  
    </form> 
</fieldset> 


</body></html> 

我有一個名爲clientAction1的Servlet:

package controller.client; 

import java.io.IOException; 
import java.sql.SQLException; 

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; 

import db.DatabaseInventory; 

/** 
* Servlet implementation class Client1_before 
*/ 
@WebServlet("/clientAction1") 
public class Client1_inventory extends HttpServlet { 
    private static final long serialVersionUID = 1L; 

    protected void doGet(HttpServletRequest request, HttpServletResponse response) 
      throws ServletException, IOException 
    { 
     HttpSession session = request.getSession(); 
     synchronized(session) 
     { 
      String grabbedNameAndLastName = request.getParameter("nameAndLastName"); 
      // do something with 

      // uncomment this code if you want to use a for loop 
      // ArrayList<String> inventory = null; 

      String inventoryStringVersion = null; 

      DatabaseInventory myDatabase = new DatabaseInventory(); 
      try 
      { 
       myDatabase.initiateConnection();  // open connection 
      } 
      catch (ClassNotFoundException e1) {e1.printStackTrace();} 

      try 
      { 
       // uncomment this code if you want to use a for loop 
       // inventory = myDatabase.getInventoryProducts(); 
       inventoryStringVersion = myDatabase.returnInventoryWholeString(); 
      } 

      catch (SQLException e) {e.printStackTrace();} 

      // uncomment this code if you want to use a for loop 
      // request.setAttribute("inventoryList", inventory); 

      request.setAttribute("inventoryBigString", inventoryStringVersion); 

      // forwards to the page employeeOpenNewAccount.jsp 
      request.getRequestDispatcher("/WEB-INF/results/client" 
        + "/client1_seeInventory.jsp").forward(request, response); 
     } 
    } 

} 

我想通過從JSP等領域${name.firstName}${name.lastName}到 我的servlet。

我該怎麼做?

+2

有*沒有*任何「現有字段」。通過提交它們,您可以從JSP獲得值到servlet。 –

+0

Dave是正確的 - 這裏的標準是將它們與您的表單一起提交。如果你已經有了頁面上的靜態值,那麼它們也可能在隱藏的輸入中。 –

+0

@CraigOtis:我如何讓它們隱藏輸入? – ron

回答

2

通常情況下,這個我們可以使用隱藏輸入:

<input type='hidden' id='firstName' value=${name.firstName}> 

但既然你不想使用<input>標籤,你可以如下嘗試:

<a href="clientAction1?firstName=${name.firstName}&lastName=${name.lastName}"> 
+0

雖然可能,OP已在其頁面上列出了一個'

'。它看起來更不容易出現語法問題,並且更容易閱讀,只使用隱藏的輸入。另外,它避免了使用servlet表達式。 –

+0

是的,但我建議這是因爲OP不想使用任何''標籤 – Baby

+0

@RafaEl:錨標籤在'&lastName'後面缺少'='。您可以使用''不要使用scriptlet。 –