web前后台数据交互的四种方式

1.利用cookie对象

Cookie是服务器保存在客户端中的一小段数据信息。使用Cookie有一个前提,就是客户端浏览器允许使用Cookie并对此做出相应的设置。一般不赞成使用Cookie。

(1)后台代码

Cookie cookie=new Cookie("name", "hello"); 
response.addCookie(cookie);

(2)前台代码

Cookie[] cookies=request.getCookies(); 
for(int i=0;i<cookies.length;i++){ 
        if(cookies[i].getName().toString().equals("name")){ 
            out.print(cookies[i].getValue()); 
        } 
}

2.利用session对象

session对象表示特定会话session的用户数据。客户第一次访问支持session的JSP网页,服务器会创建一个session对象记录客户的信息。当客户访问同一网站的不同网页时,仍处于同一个session中。
(1)后台代码

request.getSession().setAttribute("name", name); 
request.getSession().setMaxInactiveInterval(2); 
response.sendRedirect("welcome.jsp"); 

(2)前台代码(jsp页面)

Object user=request.getSession().getAttribute("name");

3.利用request重定向,设置setAttribute

(1)后台代码

request.setAttribute("name", "cute"); 
request.getRequestDispatcher("welcome.jsp").forward(request, response);  //网址不会改变
PS:如果后台使用的转发代码为 response.sendRedirect("welcome.jsp");  //网址变为welcome.jsp
则request设置的参数无效,因为已经切换到另一个请求了,request参数的有效期为本次请求。

(2)前台代码

String name=request.getAttribute("name").toString(); 

4.利用Ajax进行异步数据请求(得到的数据可以以json或xml格式返回,便于处理)

(1)后台代码案例(运用servlet传输数据)

public class TestServlet extends HttpServlet { 
   
    /**
     * Constructor of the object.
     */ 
    public TestServlet() { 
        super(); 
    } 
   
    public void doGet(HttpServletRequest request, HttpServletResponse response) 
            throws ServletException, IOException { 
        doPost(request, response); 
    } 
   
    public void doPost(HttpServletRequest request, HttpServletResponse response) 
            throws ServletException, IOException { 
   
        response.setContentType("text/html"); 
        PrintWriter out = response.getWriter(); 
        String data="[{\"name\":\"apple\",\"price\":23},{\"name\":\"banana\",\"price\":12},{\"name\":\"orange\",\"price\":8}]"; 
        out.write(data); 
        out.flush(); 
        out.close(); 
    } 
   
    /**
     * Initialization of the servlet. <br>
     *
     * @throws ServletException if an error occurs
     */ 
    public void init() throws ServletException { 
        // Put your code here 
    } 
} 

2.前台js请求处理数据代码

function createXMLHttpRequest(){ 
    var xmlrequest; 
    if(window.XMLHttpRequest){ 
        xmlrequest=new XMLHttpRequest(); 
    }else if(window.ActiveXObject){ 
        try{ 
            xmlrequest=new ActiveXObject("Msxm12.XMLHTTP"); 
        }catch(e){ 
            try{ 
                xmlrequest=new ActiveXObject("Microsoft.XMLHTTP"); 
            }catch(e){ 
                xmlrequest=""; 
            } 
        } 
    } 
    return xmlrequest; 
} 
//获取数据的函数 
function change(){ 
    var xmlrequest=createXMLHttpRequest(); 
    xmlrequest.open("POST","TestServlet",true); 
    xmlrequest.onreadystatechange=function(){ 
        if(xmlrequest.readyState==4&&xmlrequest.status==200){ 
            var data=JSON.parse(xmlrequest.responseText); 
            var content="<table border=1>"; 
            for(var i=0;i<data.length;i++){ 
                content+="<tr>"; 
                for(o in data[i]){ 
                    content+="<td>"+data[i][o]+"</td>"; 
                } 
                content+="</tr>"; 
            } 
            content+="</table>"; 
            document.getElementById("test").innerHTML=content; 
        } 
    }; 
    xmlrequest.send(); 
} 
  
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

友情链接更多精彩内容