首页 > 代码库 > (五)利用JackSon工具将JSON文件和对象互转
(五)利用JackSon工具将JSON文件和对象互转
1. 需要下载JackSon工具,并导入到:
2. 编写html页面:
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>用户注册</title> <script type="text/javascript" src="http://www.mamicode.com/js/lib/jquery.js"></script> <script type="text/javascript"> /** * 验证两次输入密码是否一致 */ $(function(){ $(‘#yanzheng‘).blur(function(){ if($(‘#passwd‘)!=null){ //第一次输入的密码不为空 if($(‘#passwd‘).val()==$(‘#yanzheng‘).val()){ }else{ alert("两次输入的密码不一样"); } }else{ //第一次输入的密码为空 } }); }); </script> </head> <body> <div style="position:relative;left:50%"> <h1 style=>用户注册</h1></div> <div style="position:relative;left:45%"> <form action="servlet/regist" method="post"> 用户名:<input type="text" name="userName"/><br/> 密 码:<input type="password" name="passWd" id="passwd" /><br/> 确认密码:<input type="password" name="rePassWd" id="yanzheng"/><br/> 性 别:<input type="radio" name="sex" value="http://www.mamicode.com/男" />男 <input type="radio" name="sex" value="http://www.mamicode.com/女" />女<br/> <input type="submit" value="http://www.mamicode.com/注册" /> </form> </div> </body> </html>
3. 编写 javabean
解析: bo是业务层,bo对象属性与用户输入的信息一一对应,本例用户输入的信息有userName、passWd、sex,所以javabean的User对象也有这三个属性。
package bo; public class User { private String userName; private String passWd; private String sex; public String getUserName() { 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. 编写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 serialVersionUID = 1L; /** * Default constructor. */ public Register() { // TODO Auto-generated constructor stub } /** * @see HttpServlet#doGet(HttpServletRequest 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"); //post方式用这种设置编码,如果是get方式,则用string.getBytes("ISO8859-1","utf-8")方法设置编码。 response.setContentType("text/html;charset=UTF-8"); //从浏览器获取用户信息 String userName=request.getParameter("userName"); String passWord=request.getParameter("passWd"); String sex=request.getParameter("sex"); //将用户信息装入javabean里 User user=new User(); user.setUserName(userName); user.setPassWd(passWord); user.setSex(sex); //将javabean转为json对象 ObjectMapper mapper = new ObjectMapper(); String json=mapper.writeValueAsString(user); PrintWriter out=response.getWriter(); out.print(json); out.close(); } }
结果:
(五)利用JackSon工具将JSON文件和对象互转
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。