如何将JSP中的抓取值传递给Servlet?

时间:2014-01-17 02:39:58

标签: java jsp servlets

考虑以下JSP:

<!-- 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 个答案:

答案 0 :(得分:2)

通常,为此我们可以使用隐藏输入:

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

但是,由于您不想使用<input>标记,因此您可以尝试如下:

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

答案 1 :(得分:1)

您可以将这些值保留在会话中,因为这些值与人员的会话信息相关,并在用户登录时发送。然后,每次将请求发送到服务器并保持会话时,都会发回这些值。