2013-01-09 39 views
0

我有一個奇怪的場景。查詢字符串具有價值first=second=12123423423423432323234Java字符串(查詢字符串)操作

String queryString = request.getParameter("first=second=12123423423423432323234") 

所以我要:

  1. 捕獲「第一」和「第二」值
  2. 驗證查詢字符串有「第一」和「第二」。

請問有人可以分享我如何以最好的方式實現這一目標嗎?

我真的很感激你對此的幫助。

+4

它看起來不像'first =&second = 12123423423423432323234'嗎?如果沒有'key = value'對之間的分隔符/分隔符,那麼您被刷新 – Bohemian

+3

這不是一個有效的查詢字符串... – fge

+0

您需要用'&'然後用'='分割並使用Map 來映射查詢參數鍵和查詢參數值。這幾乎是Java中所有的Web框架都可以完成的。 – Srinivas

回答

0

我相信您的查詢字符串應該像

first=firstvalue&second=secondvalue 

你可以使用這個在你的servlet打印查詢字符串

String firstValue = request.getParameter("first"); 
String secondValue = request.getParameter("second"); 
System.out.println("Query String:first="+firstValue+"second=+"secondValue); 

在你的情況,那裏的查詢字符串

first=second=12123423423423432323234 

您可以做到這一點

String first = request.getParameter("first"); 
String second = request.getParameter("second"); 
if(first.contains("second=")){ 
    second = first.split("second=")[1]; 
    first = first.split("second=")[0]; 
} 
out.println("[First:"+first+"][Second:"+second+"]"); 
+0

它爲我的用例工作。謝謝你Avinash奈爾。 – user1635014

0

如果您的參數被&(即: first=&second=something),然後簡單地.getParameter("first").getParameter("second")

否則,你需要用字符串玩 - 大概分成左右=,並且直到second首次降息的值遇到。雖然我沒有看到如果first的值爲:first=foosecond=bar,那麼這將如何工作?