1. You need to download the Jackson tool and import it to:
2. Write the HTML page:
<! DOCTYPE html>3. Writing JavaBeanAnalysis: 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 servletPackage 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