Skip to content

Instantly share code, notes, and snippets.

@jami-i
Created February 28, 2014 08:14
Show Gist options
  • Save jami-i/9267259 to your computer and use it in GitHub Desktop.
Save jami-i/9267259 to your computer and use it in GitHub Desktop.
Test queryParameter ?
$ curl --dump-header - "http://localhost:8080/?q1=1&q2=2?q2=2"
HTTP/1.1 200 OK
Server: Apache-Coyote/1.1
Content-Type: text/plain
Content-Length: 21
Date: Fri, 28 Feb 2014 08:15:22 GMT
q2 => 2?q2=2
q1 => 1
$ curl --dump-header - "http://localhost:8080/?q1=1&q2=2&q2=2"
HTTP/1.1 200 OK
Server: Apache-Coyote/1.1
Content-Type: text/plain
Content-Length: 19
Date: Fri, 28 Feb 2014 08:14:52 GMT
q2 => 2, 2
q1 => 1
package org.jami;
import javax.servlet.*;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.Map;
public class TestServlet implements Servlet{
@Override
public void init(ServletConfig servletConfig) throws ServletException {}
@Override
public ServletConfig getServletConfig() {
return null;
}
@Override
public void service(ServletRequest servletRequest, ServletResponse servletResponse) throws ServletException, IOException {
Map map = servletRequest.getParameterMap();
String params = "";
for(Object e : map.entrySet()){
Map.Entry entry = (Map.Entry) e;
String key = entry.getKey().toString();
Object v = entry.getValue();
String value = "";
if(v instanceof String){
value = (String)v;
}else if(v instanceof String[]){
String[] vs = (String[])v;
for(String e2 : Arrays.asList(vs)){
value += e2 + ", ";
}
if(value.endsWith(", ")){
value = value.substring(0, value.length()-2);
}
}else{
value = v.toString();
}
params += (key + " => " + value) + "\n";
}
servletResponse.setContentType("text/plain");
PrintWriter writer = servletResponse.getWriter();
writer.write(params);
writer.close();
}
@Override
public String getServletInfo() {
return null;
}
@Override
public void destroy() {}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment