简体   繁体   中英

How to read HTTP POST request parameters

I get an application/x-www-form-urlencoded HTTP POST request with several name-value-pair parameters. Some of the parameters I get are operators,operands and values. The parameters for operator run from operator1 through operator20 and similarly the parameters for operand and value run from operand1 through operand20 and value1 through value20 respectively.

Sample POST data ....operator1=equals&operator2=equals&operand2=City&value1=John&operand1=Name&value2=Miami&operator3=&operator4=&operand3=&......

My objective is to create a POJO for each of the operator, operand and value triad.

public class Criteria {

 private String operand;
 private String value;
 private String operator;

 public Criteria(String operand, String value, String operator) {
    super();
    this.operand = operand;
    this.value = value;
    this.operator = operator;
 }
}

What is the best way to read the parameters and correctly map operator, operand and value triads. Right now I have two maps. First map associates operand and operator = {operand1=operator1,operand2=operator2,....} Second map associates operand and value = {operand1=value1,operand2=value2,....}

Using these maps, I associate the three parameters to form Criteria POJO.

Please provide any suggestions on how to achieve this more efficiently and elegantly.

create a new servlet and override doPost :

 @WebServlet("/PostMe")
public class PostMe extends HttpServlet {

@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    ArrayList<String> Params = new ArrayList<>();
    for(int i=1;i<20;i++){
    Params.add(request.getParameter("operand"+i.toString()); }
    }

What you get is an arrayList containing all your parameters.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM