使用servlet获取所有请求参数

使用servlet获取HTTP请求中的所有请求参数。

源代码
PrtReqParas.java
[java]
import java.io.PrintWriter;
import java.io.IOException;
import java.util.Enumeration;
import javax.servlet.annotation.WebServlet;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@WebServlet(name = “printParas”, urlPatterns = {“/printParas/“}, loadOnStartup = 1)
public class PrtReqParas extends HttpServlet {

@Override
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws IOException, ServletException {

printParas(request, response);
}

@Override
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws IOException, ServletException {

printParas(request, response);
}

public void printParas(HttpServletRequest request, HttpServletResponse response)
throws IOException, ServletException {

response.setContentType(“text/html”);

PrintWriter out = response.getWriter();
out.println(““);
out.println(“Request Parameters“);
out.println(““);
out.println(“

“);
out.println(““);

String para = null;
Enumeration e = request.getParameterNames();
while(e.hasMoreElements()) {
para = e.nextElement();
if(para != null){
out.println(“

“);
out.println(““);
}
}

out.println(“

ParametersValue
“ + para + ““ + request.getParameter(para) + “
“);
out.println(““);
}
}
[/java]

输出
使用URL http://127.0.0.1/hello/printParas/?a=1&b=2&c=3&d=4 访问样例程序,输出如下:

Parameters

Value

d

4

b

2

c

3

a

1