• JQuery JSON Servlet


    <script type="text/javascript" src="js/jquery-1.10.2.js"></script>
    <script type="text/javascript">
        $(document).ready(function(){
            $.post("LoginServlet", {name:"ding", pass:"ying"}, function(data){
                alert(data.label + ", " + data.value);
            }, "json");
        });
    </script>
    import java.io.IOException;
    import java.io.PrintWriter;
    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.dys.model.Message;
    import com.google.gson.Gson;
    
    @WebServlet("/LoginServlet")
    public class LoginServlet extends HttpServlet {
        private static final long serialVersionUID = 1L;
      
        public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            this.doPost(request, response);
        }
        public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
            String name = request.getParameter("name");
            String pass = request.getParameter("pass");
            
            if("ding".equals(name) && "ying".equals(pass)) {
                Message message = new Message();
                message.setLabel("系统提示");
                message.setValue("用户登录成功!");
                Gson gson = new Gson();
                String messageGSon = gson.toJson(message);
                response.setContentType("text/plain;charset=UTF-8");
                response.setCharacterEncoding("UTF-8");
                PrintWriter printWriter = response.getWriter();
                printWriter.print(messageGSon);
                printWriter.flush();
            }
        }
    }
  • 相关阅读:
    HTML5边玩边学
    全面理解javascript的caller,callee,call,apply概念
    什么是HTTP Headers?
    Jquery的$命名冲突:
    jQuery 核心 noConflict() 方法
    js 两个小括号 ()() 的用法
    前端工程师的编码遭遇战
    Canvas之图像与文字
    Internet Explorer 9.0 新功能介绍
    firebug1.7调试实用技巧指南—web前端开发七武器
  • 原文地址:https://www.cnblogs.com/dingyingsi/p/3313213.html
Copyright © 2020-2023  润新知