web开发中的一些技术杂项整理文章 web,开发,整理

web开发中的一些技术杂项整理文章 web,开发,整理

在web开发中的一些技术杂项比较乱,现在整理一下大家参考一下

关键词:

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文件中书写的方式是:

Js代码

1. $.get(“jsp/get2.jsp”,{

2. “username”:encodeURI($(“#username”).val()),

3. “content”:encodeURI($(“#content”).val())

4. },function(data, textStatus){

5. var username = $(data).find(“comment”).attr(“username”);

6. var content = $(data).find(“comment content”).text();

7. username = decodeURI(username);

8. content = decodeURI(content);

9. var txtHtml = “<div class=’comment’><h6>”+username+”:</h6><p class=’para’>”+content+”</p></div>”;

10. $(“#resText”).html($(“#resText”).html()+txtHtml);

11. },”xml”);

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

Html代码

1. <%@page contentType=”text/xml” language=”java” pageEncoding=”UTF-8″

2. import=”java.util.Date,java.text.SimpleDateFormat”%>

3. <%

4. response.setContentType(“text/xml”);

5. String username = request.getParameter(“username”);

6. String content = request.getParameter(“content”);

7. //需要注意的是下面的标记不需要了,带上的话就会报错了

8. //out.println(“<?xml version=”1.0″ encoding=”UTF-8″?>”);

9. if(content!=null && !content.trim().equals(“”)){

10. if(username==null || username.trim().equals(“”)){

11. username = “匿名人士”;

12. }

13. Date now = new Date();

14. SimpleDateFormat sdf = new SimpleDateFormat(“yyyy年MM月dd日 HH:mm:ss”);

15. out.println(“<comments>”);

16. out.println(“<comment username=””+username+” 发表于 “+sdf.format(now)+””>”);

17. out.println(“<content>”+content+”</content>”);

18. out.println(“</comment>”);

19. out.println(“</comments>”);

20. }

21. %>

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

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

Js代码

1. //重要!!!在新版本的jQuery中,采用了更为严格的json解析方式,所以所有内容都必须要有双引号。

2. //必须形如:{“key” : “28CATEGORY”,”status” : “0”}

3. $(“#send3”).click(function(){

4. $.get(“jsp/get3.jsp”,{

5. “username”:encodeURI($(“#username”).val()),

6. “content”:encodeURI($(“#content”).val())

7. },function(data, textStatus){

8. var username = data.username;

9. var content = data.content;

10. username = decodeURI(username);

11. content = decodeURI(content);

12. var txtHtml = “<div class=’comment’><h6>”+username+”:</h6><p class=’para’>”+content+”</p></div>”;

13. $(“#resText”).html($(“#resText”).html()+txtHtml);

14. },”json”);

15. });

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

Html代码

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

2. <%

3. String username = request.getParameter(“username”);

4. String content = request.getParameter(“content”);

5. out.println(“{ “username” : “”+username+”” , “content” : “”+content+””}”);

6. %>

摘自 yiyiboy2010

web开发中的一些技术杂项整理文章 web,开发,整理

相关文章:

你感兴趣的文章:

标签云: