考慮下面的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。
我該怎麼做?
有*沒有*任何「現有字段」。通過提交它們,您可以從JSP獲得值到servlet。 –
Dave是正確的 - 這裏的標準是將它們與您的表單一起提交。如果你已經有了頁面上的靜態值,那麼它們也可能在隱藏的輸入中。 –
@CraigOtis:我如何讓它們隱藏輸入? – ron