zoukankan      html  css  js  c++  java
  • 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();
            }
        }
    }
  • 相关阅读:
    Servlet生命周期
    DAO 开发模式的几个类
    Iterator用法
    mysql相似于oracle的to_char() to_date()方法
    Java Web页面跳转
    JSP 连接MySQL 5.1
    采用DOM进行表格的修改操作
    使用css让XML文件按照HTML的风格显示出来
    正则表达式Regular Expression
    什么是“堆”,"栈","堆栈","队列",它们的区别
  • 原文地址:https://www.cnblogs.com/dingyingsi/p/3313213.html
Copyright © 2011-2022 走看看