当前位置: 代码迷 >> Web前端 >> 前端展示server端的数据
  详细解决方案

前端展示server端的数据

热度:376   发布时间:2012-08-17 02:08:34.0
前端显示server端的数据

在使用ajax的时候,server端处理完相关的逻辑之后,根据操作完的结果,需要给前端展现不同的提示,这种提示可以在前端展示也可以在后端操作,这里我就说说在后端如何给前端发送数据的

?

?

1、前端发送相应的ajax请求

?

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
<script type="text/javascript" src="../js/jquery-1.4.4.js"></script>
</head>
<script type="text/javascript">
function test(){

	var date = new Date();
	timestamp = date.getTime();
	//alert(timestamp.getTime());
	//参数后面加上时间戳是以为ajax机制利用了缓存,如果参数一致就不会再次发送请求了
	//前面的../表示路径要在上一个目录,才能发送请求
	$.get("../AjaxWrite", { name: "John", time: "2pm",timestamp:timestamp},
		//function方法是接受server端传递过来的方法
		function(data){
			alert(data);
	});
}
</script>
<body>

<button onclick="test()">test</button>

	<form action="../AjaxWrite">
		<input type="submit" value="submit"/>
	</form>
	
</body>
</html>

?

2、server端向前端写数据

?

package hb.com.servlet;

import java.io.IOException;
import java.io.PrintWriter;

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

public class AjaxWrite extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    public AjaxWrite() {
        super();
    }

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		this.doPost(request, response);
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		System.out.println("1");
		System.out.println(request.getParameter("name"));
		System.out.println(request.getParameter("time"));
		
		PrintWriter pw = response.getWriter();
		pw.write("huangbiao");
		//server端向前端发送的数据一定 要flush()方法才能被前端接受
		pw.flush();
		
	}

}
?

3、前端就会显示server端写的数据"huangbiao"弹出

?

利用JSP页面或者是HTML页面下载文件的原理

?

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
<script type="text/javascript" src="../js/jquery-1.4.4.js"></script>
</head>
<script type="text/javascript">

function test(){

	var date = new Date();
	var timestamp = date.getTime();
	alert(timestamp);
	$.get("../FileDownload",{"timestamp":timestamp},function(data){
		alert(data);
	});	
}

</script>
<body>

	<button onclick="test()">test</button>

	<form action="../FileDownload">
		<input type="submit" value="submit"/>
	</form>
</body>
</html>
?

前端触发请求,server端向前端写数据

package hb.com.servlet;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;

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

public class FileDownload extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    public FileDownload() {
        super();
    }

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
			this.doPost(request, response);
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		//写明要下载的文件的大小
		int fileLength = 0;
		File file = new File("c:\\aaa.xml");
		
		FileInputStream fis = new FileInputStream(file);
		
		fileLength = fis.available();
		
		BufferedInputStream buff = new BufferedInputStream(fis);
		
		response.setContentLength(fileLength);
		
		response.setContentType("application/x-msdownload");
		
		response.setHeader("Content-disposition", "attachment;filename=" + "黄彪.xml");
		
		byte[] b = new byte[1024];	// 相当于我们的缓存
		long k = 0;	// 该值用于计算当前实际下载了多少字节
		// 从response对象中得到输出流,准备下载
		OutputStream myout = response.getOutputStream();  
		// 开始循环下载
		while (k < fileLength) {
			int j = buff.read(b, 0, 1024);
			k += j;
			// 将b中的数据写到客户端的内存
			myout.write(b, 0, j);
		}
		// 将写入到客户端的内存的数据,刷新到磁盘
		myout.flush();
		
	}

}

?

总结:实际上下载问价也是属于server端向前端写数据,只是以文件的方式展现出来,并保存到磁盘上了

  相关解决方案