1. servlet怎麼把數據中的數據傳到jsp頁面
1、利用ServletContext這個web全局上下文來共享數據
servlet中getServletContext()可以獲得一個ServletContext對象,利用這個對象的getAttribute()/setAttribute()方法可以在整個WEB應該里共享數據,可以實現servlet和jsp之間的數據互傳
比如:
在servlet中
getServletContext.setAttribute("title", "hello world");
在servlet上下文中以「hello」為鍵,保存了「hello world」這一個字元串,如果要在jsp中調用,則用如下jsp腳本
<%=application.getAttribute("hello")%>
2、利用session在同一個會話共享數據
利用HttpSession共享同一個會話的數據。這也要用到session的getAttribute()/setAttribute()方法,和ServletContext()的使用差不多的。
3、利用request共享一次請求的數據
一次請求當中,可以利用request的getAttribute()/setAttribute()方法在servlet和jsp頁面間共享數據。
2. servlet怎麼接收json數據
解釋一下:
1.利用流接收傳過來的數據,這里用的是字元流,你也可以用位元組流。
2.JSONObject.fromObject() 方法的參數是String類型參數,所以用 toString()方法。