JavaScript实现AJAx的使用

工程目录结构:
JavaScript实现AJAx的使用_第1张图片
代码:serlvet代码:

@WebServlet("/ajaxServlet")
public class ajaxServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		//得到目标文件路径
		String url=request.getServletContext().getRealPath("demo.txt");
		File file= new File(url);
		//创建输入流
		FileInputStream  in =new FileInputStream(file);
		ServletOutputStream out =response.getOutputStream();
		int len=0; 
		byte[] bs= new byte[1024];
		while((len=in.read(bs))>0) {
			out.write(bs, 0, len);
		}
		
	}
jsp页面:





异步请求




点击进行异步请求:



结果图:JavaScript实现AJAx的使用_第2张图片

你可能感兴趣的:(Javaweb)