2014-07-23 33 views
0

我對兩種不同的表單數據使用相同的dpPost方法。我無法訪問第二種形式的請求參數。無法訪問java servlets中的請求參數

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

    if("schema".equals(session.getAttribute("which"))) { 
      second_html(); 
      //call second page here 
      } 

      String btnClicked= request.getParameter("p2"); // This is getting null after submitting second_html() 
     if("edit".equals(session.getAttribute("which"))){ 
    /* process second page here after Submit on the second page 
        I am trying to access request.getParameter() but value is null     here for the fields in the second page  */ 
       second_html(); 
    } 
    } 
    first_html() { 
    // have form and submit button 
    session.setAttribute("which","schema"); 

} 
second_html() { 
// have form and submit button 
    <input type='text' name='p2' id='p2' size='3' > 
session.setAttribute("which","edit"); 
} 

編輯:我的會話getters工作正常。但是request.getParameter不起作用。

+0

'我不能夠訪問請求參數爲第二form'。很不清楚。你能詳細說明嗎? –

回答

2

您正在訪問會話變量,而不是您的請求參數。

可以使用

request.getParameter("which") 
+0

請參閱編輯。這個問題不是關於會議 – user3153014

+0

當然不是。你問如何訪問請求參數,我們給你一個答案。請更精確地問。 – f1sh

+0

如何訪問p2。 request.getParameter(「p2」) – user3153014

2

訪問他們,如果我理解你的問題,你應該使用ServletRequest.getParameter(String)

String v = request.getParameter("which"); 
if (v.equals("schema")) { 

} else if (v.equals("edit")) { 

} 
+0

請參閱編輯。問題不是關於會話 – user3153014

+0

顯然,'submit'不是一個請求參數。如果你的問題不是關於會話,那麼爲什麼你發佈的代碼? –

+0

提交是參數。我發佈了代碼,因爲我認爲由於這種代碼結構在獲取請求參數方面存在問題。 – user3153014