2013年5月3日 星期五

[Java Servlet]網址的參數有空白(Spaces in URL)


更改該參數值的編碼
利用URLEncoder和URLDecoder

For example,

import java.net.URLEncoder;
import java.net.URLDecoder;


Sent:

PrintWriter out = null;
out = response.getWriter();

out.print("<a href=");
out.print("Query?qryKeywords=" + URLEncoder.encode(qryKeywords, "UTF-8"));
out.println(">" + qryKeywords + "</a>");

---
receive接收可能有空白鍵值網址的程式碼:



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

...
       URLDecoder.decode(request.getParameter("qryKeywords"), "UTF-8")


..
}