2014-01-17 2 views
-1

은 다음과 JSP를 고려JSP에서 잡은 값을 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}을 전달하려는 :

<!-- 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라는 서블릿을 가지고있다.

어떻게하면됩니까?

우리가 숨겨진 입력을 사용할 수 있습니다 이것에 대한
+2

* * "기존 필드"가 * 없습니다. JSP에서 서블릿으로 값을 전송하면됩니다. –

+0

Dave는 정확합니다. 여기에있는 표준은 양식과 함께 제출하는 것입니다. 페이지에 이미 정적 값이있는 경우에는 숨겨진 입력 값이있을 수도 있습니다. –

+0

@CraigOtis : 숨겨진 입력을 어떻게 만들 수 있습니까? – ron

답변

2

일반적으로 :

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

가능한 한 OP에 이미 자신의 페이지에 '

'이 표시되어 있습니다. 숨겨진 입력을 사용하는 것보다 훨씬 구문이 읽기 쉽고 읽기가 훨씬 쉬워 보입니다. 또한 서블릿 표현식을 사용하지 않아도됩니다. –

+0

예, OP가 '' 태그를 사용하고 싶지 않기 때문에 제안합니다. – Baby

+0

@RafaEl :'& lastName' 다음에'='가 누락 된 앵커 태그에 있습니다. ''Scriptlet을 사용하지 마라. –

관련 문제