如何使用AJAX—I

在這裏插入圖片描述

在這裏插入圖片描述
在這裏插入圖片描述

package ajax;

import java.io.IOException;
import java.io.OutputStream;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson.JSON;

public class myServlet extends HttpServlet {

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		OutputStream out = resp.getOutputStream();

//		out.write("hell world!".getBytes());
		Map map=new HashMap();
		map.put("name", "小名");
		map.put("age",18);
		map.put("sex", "男");
		Object obj=JSON.toJSON(map);
		out.write(obj.toString().getBytes());
		out.close();
	}

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		super.doPost(req, resp);
	}

}

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <title>abc.html</title>
	
    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    <meta http-equiv="description" content="this is my page">
    <meta http-equiv="content-type" content="text/html; charset=UTF-8">
    
    <!--<link rel="stylesheet" type="text/css" href="./styles.css">-->

  </head>
  
  <body>
試試就試試
  </body>
</html>

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
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%>">
    <script type="text/javascript" src="js/jquery.js"></script>
    <title>My JSP 'index.jsp' starting page</title>
	
  </head>
  
  <body>
   <div id="myid">xxxx</div>
   <button id="b001">點擊我試試<button>
  </body>
  <script type="text/javascript">

$("#b001").click(function(){
   $.ajax(
   {
   url:"abc.html",
   success:function(data){
   $("#myid").html(data);
   }
   }
   );
});
</script>
<html>

web-xml




	<servlet>
		<servlet-name>abc</servlet-name>
		<servlet-class>ajax.myServlet</servlet-class>

	</servlet>
	<servlet-mapping>
		<servlet-name>abc</servlet-name>
		<url-pattern>/asd</url-pattern>
	</servlet-mapping>

在這裏插入圖片描述

在這裏插入圖片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章