package com.jcuckoo.util;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import javax.servlet.http.HttpServletRequest;
import net.sf.json.JSONObject;

public class JsonReader {
	public static JSONObject receivePost(HttpServletRequest request) throws IOException, UnsupportedEncodingException {

		// 读取请求内容
		BufferedReader br = new BufferedReader(new InputStreamReader(request.getInputStream(),"utf-8"));
		String line = null;
		StringBuilder sb = new StringBuilder();
		while ((line = br.readLine()) != null) {
			sb.append(line);
		}
		//将json字符串转换为json对象
		JSONObject json=JSONObject.fromObject(sb.toString());
		return json;
	}
}


package com.jcuckoo.servlet;

import java.io.IOException;
import java.io.Writer;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.jcuckoo.pojos.User;
import com.jcuckoo.service.UserService;
import com.jcuckoo.util.JsonReader;
import net.sf.json.JSONObject;
@WebServlet("/LoginUserServlet")
public class LoginUserServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	public LoginUserServlet() {
		super();
	}

	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		doPost(request, response);
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// response.setContentType("text/html");
		// 设置字符编码为UTF-8, 这样支持汉字显示
		// response.setCharacterEncoding("UTF-8");

		response.setContentType("text/html;charset=utf-8");

		/** 设置响应头允许ajax跨域访问 **/
		response.setHeader("Access-Control-Allow-Origin", "*");
		/* 星号表示所有的异域请求都可以接受, */
		response.setHeader("Access-Control-Allow-Methods", "GET,POST");

		Writer out = response.getWriter();


		JSONObject json=JsonReader.receivePost(request);
		System.out.println(json);
		
		UserService userService=new UserService();
		
		//将建json对象转换为java对象
		User loginUser = (User)JSONObject.toBean(json,User.class);
		User user = userService.LoginUser(loginUser);
		JSONObject jsonObject=new JSONObject() ;
		if(user!=null){
			//将java对象转换为json对象
			jsonObject.put("user", JSONObject.fromObject(user));
			jsonObject.put("message", "用户登录成功!");
		}else{

			jsonObject.put("message", "用户登录失败!");
		}
		out.write(jsonObject.toString());
		out.flush();
	}
}

<%@ page language="java" contentType="text/html; charset=utf-8"%>
<!DOCTYPE html >
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Servlet-jCuckoo</title>
<script type="text/javascript" src="js/jquery-3.1.1.min.js"></script>
</head>
<body>
	<form >
		用户名:<input type="text" id="userName"/><br/>
		密码:<input type="password" id="userPwd"/><br/>
		<input type="button" value="登录" id="sender"> 
	</form>
	<div id="messageDiv"></div>
	
	<script>
	$('#sender').click(function(){
		var userName=document.getElementById('userName').value;
		var userPwd=document.getElementById('userPwd').value;
		var user={userName:userName,userPwd:userPwd};
		var url="LoginUserServlet";
		
		$.post(url, JSON.stringify(user), function(data) {
			  console.log(data);
             $("#messageDiv").html(data);
             var json=JSON.parse(data);
             alert(json.message);
         }); 
		 /* $.ajax({
			type:'post',
			url:url,
			dataType:"json",			
			data:JSON.stringify(user),
			success: function (data) {
				var user=data.user;
				 $("#messageDiv").html(JSON.stringify(user));
	             alert(data.message);
	        },
	        error: function (data) {
	             alert(data.message);
	             $("#messageDiv").html("");
	        }
		});  */
		
	});
	</script>
</body>
</html>


GitHub 加速计划 / js / json
18
5
下载
适用于现代 C++ 的 JSON。
最近提交(Master分支:2 个月前 )
6be4e856 4 天前
663058e7 6 天前
Logo

旨在为数千万中国开发者提供一个无缝且高效的云端环境,以支持学习、使用和贡献开源项目。

更多推荐