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()方法。