• 通过Servlet获取HTML页面的JavaScript中的json数组


    ​<label>用户名</label>

    <input type="text" id="userName"><br>

    <label>密    码</label>

    <input type="password" id="password"><br>

    <input type="submit" class="submitLogin" value="提交" >

    $(".submitLogin").click(function(e) {

    var UserName = $("#userName").val();

    var Password = $("#password").val();

    var user ={person:{Name:UserName,Pass:Password}};

    $.ajax({

    type : "POST",

    url : "login",

    data :user,

    dataType:"json",

    success : function(msg) {

    console.info(JSON.stringify(user)+" -->1");

    console.log(msg);

    },

    error:function(){

    console.info(JSON.stringify(user)+" Error");

    console.info(xmlHttp.readyState);

    console.info(xmlHttp.status);

    console.info(xmlHttp.statusText);

    },

    fail:function(){

    console.info("Fail");

    }

    });

    }

    });

     <servlet>

      <servlet-name>LoginServlet</servlet-name>

      <servlet-class>com.servlet.LoginServlet</servlet-class>

      </servlet>

      

      <servlet-mapping>

      <servlet-name>LoginServlet</servlet-name>

      <url-pattern>/login</url-pattern>

      </servlet-mapping>

    public class MyServlet extends HttpServlet{

    private static final long serialVersionUID = 1L;

    private static int i = 0;

    @Override

    public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    System.out.println("处理POST请求...");

    //获取前台data数据中的uName和uPass

    request.setCharacterEncoding("utf-8");

    String user= request.getParameter("person");

    String name = request.getParameter("Name");

    String password = request.getParameter("Pass");

    System.out.println(i++);

    JsonObject jo = new JsonObject();

    jo.addProperty("Result", "Success");

    jo.addProperty("user", user);

    jo.addProperty("name", name);

    jo.addProperty("password", password);

    PrintWriter out = response.getWriter();

    out.println(jo);

    System.out.println(jo);

    }

    }

    HTML页面输入:
     

    前台输出:
      

    后台输出:

    海量自学资源,找Java开发学习交流君羊02里君羊主小姐姐

  • 相关阅读:
    使用简单的反射技术重构组合查询串功能
    沤血分享之:使用Opera浏览器技巧全集
    项目中用到的RE分析
    关于调用新浪微博与腾讯微博
    正则 (?i,m,s,x,g)
    求职路 第二章 深圳篇
    12320平台架构及部署
    网站会员密码
    求职路 第二章 技术篇
    TFS故障一二
  • 原文地址:https://www.cnblogs.com/xsns/p/6756098.html
Copyright © 2020-2023  润新知