(v) Use the Jackson tool to transfer JSON files and objects to each other

Source: Internet
Author: User

1. You need to download the Jackson tool and import it to:

2. Write the HTML page:
<! DOCTYPE html>

3. Writing JavaBean

Analysis: Bo is the business layer, Bo object properties and user input information one by one corresponding to this example user input information has username, passWd, sex, so JavaBean user object also has these three attributes.

Package Bo;public class User {private String  username;private string passwd;private string Sex;public string Getusern Ame () {return userName;} public void Setusername (String userName) {this.username = UserName;} Public String getpasswd () {return passWd;} public void setpasswd (String passWd) {this.passwd = passWd;} Public String Getsex () {return sex;} public void Setsex (String sex) {this.sex = sex;}}

4. Writing a servlet
Package Servlet;import Java.io.ioexception;import Java.io.printwriter;import javax.servlet.servletexception;import Javax.servlet.http.httpservlet;import Javax.servlet.http.httpservletrequest;import Javax.servlet.http.httpservletresponse;import com.fasterxml.jackson.databind.objectmapper;import bo. user;/** * Servlet Implementation Class Register */public class Register extends HttpServlet {private static final long SE    Rialversionuid = 1L;      /** * Default constructor. */Public Register () {//TODO auto-generated constructor stub}/** * @see Httpservlet#doget (httpservletreque St request, httpservletresponse Response) */protected void doget (HttpServletRequest request, HttpServletResponse Response) throws Servletexception, IOException {this.dopost (request, response);} /** * @see Httpservlet#dopost (httpservletrequest request, httpservletresponse response) */protected void DoPost ( HttpServletRequest request, HttpServletResponse response) throws Servletexception, Ioexception {request.setcharacterencoding ("UTF-8");      The //post mode is encoded with this setting, and if it is a Get method, the encoding is set with the String.getbytes ("Iso8859-1", "Utf-8") method. Response.setcontenttype ("Text/html;charset=utf-8");//Get user information from the browserString username=request.getparameter ("UserName"); String password=request.getparameter ("PassWd"); String sex=request.getparameter ("sex");//loading user information into JavaBeanUser User=new User (); user.setusername (userName); user.setpasswd (PassWord); user.setsex (Sex);//Convert JavaBean to JSON objectObjectmapper mapper = new Objectmapper (); String json=mapper.writevalueasstring (user); PrintWriter out=response.getwriter (); Out.print (JSON); Out.close ();}}

Results:

(v) Use the Jackson tool to transfer JSON files and objects to each other

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.