• jason的简单的应用


    jsp的代码如下:

    <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>

    <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
    <%
    String path = request.getContextPath();
    String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
    %>


    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
    <html>
      <head>
        <base href="<%=basePath%>">
        
        <title>My JSP 'demo.jsp' starting page</title>
        
    <meta http-equiv="pragma" content="no-cache">
    <meta http-equiv="cache-control" content="no-cache">
    <meta http-equiv="expires" content="0">    
    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    <meta http-equiv="description" content="This is my page">
    <!--
    <link rel="stylesheet" type="text/css" href="styles.css">
    -->


      </head>
      <script type="text/javascript">
      //创建异步对象
      function createXMLHttpRequest(){
     try {
    return new XMLHttpRequest();//返回大多数浏览器都支持的对象
    } catch (e) {
    try {
    return new ActiveXObject("Msxml12.XMLHTTP");//返回ie6支持的对象
    } catch (e) {
    try {
    return ActiveXObject("Microsoft.XMLHTTP");//返回ie5.5支持的版本
    } catch (e) {
    alert("兄弟,你使用的是自定义浏览器吗?");
    throw e;//最后都不符合的话就会抛出一个异常
    }
    }
    }
      }
      
      window.οnlοad=function(){//文档加载完毕之后就执行该代码
     var btn=document.getElementById("btn");//获得鼠标点击的对象
     btn.οnclick=function(){//加上鼠标监听事件
    var request=createXMLHttpRequest();//得到XMLHttpRequest对象
    request.open("GET","<c:url value='/jasonServlet'/>",true);  //设置请求为GET求求 并且与服务器相连接
    request.send(null);//设置请求体为null
    request.onreadystatechange=function(){//为状态码发生改变注册一个事件
    if(request.readyState==4&&request.status)//如果request的状态为4而且返回的是200就代表响应成功和结束了
    {
    var text=requ!est.responseText;//得到服务器的文本内容
    var pharse=eval("("+text+")");//执行jason串
    h1.innerHTML=pharse.a+"?"+pharse.b+"?"+pharse.c+"?"+pharse.d+"!";//将jason串显示在h1上
    }
    };

     };
     
     
     
      };
      
      
      
      
      </script>
      <body>
       <button id="btn">点我啊</button>
       <h1>jason的一个小实验!!</h1>
       <h2 id="h1"></h2>

      </body>

    servlet的代码如下:

    response.setContentType("text/html;charset=utf-8");//设置返回的编码类型
    request.setCharacterEncoding("UTF-8");//设置收到是查看的编码类型
    String demo="{\"a\":\"我爱你\",\"b\":\"你信吗\",\"c\":\"我不信\",\"d\":\"但是我信啊\"}";//设置字符串的内容
    PrintWriter out = response.getWriter();
    out.println(demo);//输出到客户端页面中
    out.close();

  • 相关阅读:
    【20220125】连岳摘抄
    【20220127】连岳摘抄
    【20220124】心胸狭窄容易生事
    【20220201】连岳摘抄
    【20220123】连岳摘抄
    【20220130】连岳摘抄
    【一句日历】2022年2月
    【20220126】傅雷家书
    【20220128】勤奋和从容并不冲突
    深入理解JUC:第六章:Semaphore信号灯
  • 原文地址:https://www.cnblogs.com/csnd/p/16675722.html
Copyright © 2020-2023  润新知