Obtain the JSON parameters of Ajax at the front end based on the Jersey pojo object

Source: Internet
Author: User
Tags apache tomcat

Jersey is not introduced here. It is a restful Web Service implemented by Sun. For details, refer to the following link: use jersey and Apache Tomcat to build a restful web service.

What I want to talk about here is how to obtain the JSON data of the front-end Ajax request. I did not talk about this part after I searched it on the Internet, or I used the client to simulate it. I have successfully tested it here. I will first give a method.

To put it bluntly, first go to the Code:

The first is the JSP page Ajax request:

<%@ page language="java" contentType="text/html; charset=UTF-8"pageEncoding="UTF-8"%><!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

Background resource processing:

@Path("/")public class HelloWorld {    @GET    @Path("/helloWorld")    @Produces(MediaType.TEXT_PLAIN)    public String abc() {        String ret = "Hello World!";        return ret;    }    @POST    @Path("/update")    @Produces("application/json")    public Person update(@FormParam(value = "a1") String a1, @FormParam(value = "a2") String a2) {        System.out.println(a1);        System.out.println(a2);                return new Person("testPerson", "22");    }        @GET    @Path("/requestTest")    @Produces("application/json")    public Person update(@Context HttpServletRequest request) {        System.out.println("request:" + request.getParameter("a11"));        System.out.println("request:" + request.getParameter("a22"));        System.out.println("request:" + request.getQueryString());        return new Person("testPerson", "33");    }}

Note that the first Ajax request is the update method of the post request, @ formparam (value = "A1") string A1, @ formparam (value = "A2 ") string A2 here A1 and A2 are parameters sent from the front end, which can be used in the update method. However, if there is a lot of JSON data, we cannot list them one by one in the method. Another method is to obtain the @ context httpservletrequest request through the request. Note that the request method is get. In this way, we can easily get the parameters sent from the front-end JSON. You can also obtain the value in the session.

This is the test. Thank you.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.