当前位置: 代码迷 >> Web前端 >> 技术杂项收拾
  详细解决方案

技术杂项收拾

热度:263   发布时间:2012-06-28 15:20:03.0
技术杂项整理

1.get()和post()区别:
(1).get请求会将参数放在URL之后进行传递,而post方式是作为http消息实体发送给Web服务器,但是,这种区别在ajax中对用户是不可见的。
(2).get请求方式对传输的数据有大小限制,通常不能大于2KB,而是用post的方式一般不受限制。
(3).get请求的数据会被浏览器缓存起来,因此其他人就可以通过浏览器的历史记录读取这些数据,例如帐号密码等,严重的可能会带来安全问题,而post方式相对就可以避免这些问题。
(4).get方式和post方式传递的数据在服务器端获取的方式也可能不相同,如php,$_GET[]获取get请求,$_POST[]获取post请求。但是JSP是相同的。
(5).get请求提交的速度要比post速度快,但不是差异特别大,在对速度要求比较苛刻的条件下,如搜索引擎,对搜索请求就会采用get方式提交。
(6).HTTP/1.1协议规定了八种请求方式,分别是:options,head,get,post,put,delete,trace,connect,但get和post方式是使用最多的方式。
2.jQuery解析xml和json注意事项:
在使用新版本的jQuery(1.7.2,不是很清楚到底从哪个版本开始这样规定的)采用$.get()解析xml数据时,通常js文件中书写的方式是:

$.get("jsp/get2.jsp",{
	"username":encodeURI($("#username").val()),
	"content":encodeURI($("#content").val())
},function(data, textStatus){
	var username = $(data).find("comment").attr("username");
	var content = $(data).find("comment content").text();
	username =  decodeURI(username);
	content =  decodeURI(content);
	var txtHtml = "<div class='comment'><h6>"+username+":</h6><p class='para'>"+content+"</p></div>";
	$("#resText").html($("#resText").html()+txtHtml);
},"xml");

服务器端(JSP)需要返回一个XML文件或者构建出一个XML文件,方式如下:

<%@page contentType="text/xml" language="java" pageEncoding="UTF-8" 
import="java.util.Date,java.text.SimpleDateFormat"%>
<%
	response.setContentType("text/xml");
	String username = request.getParameter("username");
	String content = request.getParameter("content");
	//需要注意的是下面的标记不需要了,带上的话就会报错了
	//out.println("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
	if(content!=null && !content.trim().equals("")){
		if(username==null || username.trim().equals("")){
			username = "匿名人士";
		}
		Date now = new Date();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");
		out.println("<comments>");
		out.println("<comment username=\""+username+"   发表于   "+sdf.format(now)+"\">");
		out.println("<content>"+content+"</content>");
		out.println("</comment>");
		out.println("</comments>");
	}
%>

?需要注意的内容已经在注释中标记出来了,不然的话浏览器会报出XML文件解析失败的错误。

json文件的解析重点注意的是服务端,js代码如下:

//重要!!!在新版本的jQuery中,采用了更为严格的json解析方式,所以所有内容都必须要有双引号。
//必须形如:{"key" : "28CATEGORY","status" : "0"}
$("#send3").click(function(){
	$.get("jsp/get3.jsp",{
		"username":encodeURI($("#username").val()),
		"content":encodeURI($("#content").val())
	},function(data, textStatus){
		var username = data.username;
		var content = data.content;
		username = decodeURI(username);
		content = decodeURI(content);
		var txtHtml = "<div class='comment'><h6>"+username+":</h6><p class='para'>"+content+"</p></div>";
		$("#resText").html($("#resText").html()+txtHtml);
	},"json");
});

?按照上述的注意说明,JSP代码应该为:

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
	String username = request.getParameter("username");
	String content = request.getParameter("content");
	out.println("{ \"username\" : \""+username+"\" , \"content\" : \""+content+"\"}");
%>
?

?

?

?

?

?

?

?

?

?

?

?

?

?

?

?

?

?

?

?

?

?

?

?

?

?

?

?

?

  相关解决方案