2015-02-10 26 views
1

出於好奇,我發送同名和不同值的get和post參數。讓參數獲得重要的參數嗎?

JSP:

<form action="actionName?param1=value1" method="post"> 
<input type="text" value="value2" name="param1" id="param1"> 
<input type="submit" value="Submit"> 
</form> 

的Servlet:

public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException 
{ 
    doPost(request, response); 
} 
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException 
{ 
    String strParam1 = request.getParameter("param1"); 
} 

我一直都想與strParam1的價值 「VALUE1」。

那麼,這是否意味着獲取參數對post參數的重要性還是取決於?

+0

你是否覆蓋了'的doPost()'在servlet的方法? – 2015-02-10 05:48:14

+0

yes ..會在一分鐘內更新我的servlet代碼.. – Kartic 2015-02-10 05:49:27

+0

請求清除您的問題後請檢查您的瀏覽器控制檯 – 2015-02-10 05:53:35

回答

0

獲取和發佈顯然處理方式不同。因爲我看到很多例子以與獲取請求相同的方式處理提交後的內容,令人困惑。

以下文章深入探討了它。

http://www.cse.iitb.ac.in/dbms/Data/Courses/DBIS/Software/servlets/servlet_tutorial.html#user-interaction

doPost方法使用getParameterNames和getParameterValues 方法來獲取表單數據。

public void doPost(HttpServletRequest req, HttpServletResponse res) 
    throws ServletException, IOException { 
    // first, set the "content type" header of the response 
    res.setContentType("text/html"); 

    //Get the response's PrintWriter to return text to the client. 
     PrintWriter toClient = res.getWriter(); 

     try { 
      //Open the file for writing the survey results. 
      String surveyName = req.getParameterValues("survey")[0]; 
      FileWriter resultsFile = new FileWriter(resultsDir 
      + System.getProperty("file.separator") 
      + surveyName + ".txt", true); 
      PrintWriter toFile = new PrintWriter(resultsFile); 

     // Get client's form data & store it in the file 
      toFile.println("<BEGIN>"); 
      Enumeration values = req.getParameterNames(); 
      while(values.hasMoreElements()) { 
       String name = (String)values.nextElement(); 
     String value = req.getParameterValues(name)[0]; 
       if(name.compareTo("submit") != 0) { 
        toFile.println(name + ": " + value); 
       } 
      } 
      toFile.println("<END>"); 

     //Close the file. 
      resultsFile.close(); 

     // Respond to client with a thank you 
     toClient.println("<html>"); 
     toClient.println("<title>Thank you!</title>"); 
      toClient.println("Thank you for participating"); 
     toClient.println("</html>"); 

     } catch(IOException e) { 
      e.printStackTrace(); 
      toClient.println(
     "A problem occured while recording your answers. " 
     + "Please try again."); 
     } 

     // Close the writer; the response is done. 
    toClient.close(); 
    }