![servlet怎么把数据中的数据传到jsp页面?,第1张 servlet怎么把数据中的数据传到jsp页面?,第1张](/aiimages/servlet%E6%80%8E%E4%B9%88%E6%8A%8A%E6%95%B0%E6%8D%AE%E4%B8%AD%E7%9A%84%E6%95%B0%E6%8D%AE%E4%BC%A0%E5%88%B0jsp%E9%A1%B5%E9%9D%A2%EF%BC%9F.png)
1、利用ServletContext这个web全局上下文来共享
数据\x0d\x0aservlet中getServletContext()可以获得一个ServletContext对象,利用这个对象的getAttribute()/setAttribute()
方法可以在整个WEB应该里共享数据,可以实现servlet和jsp之间的数据互传\x0d\x0a比如:\x0d\x0a\x0d\x0a在servlet中\x0d\x0agetServletContext.setAttribute("title", "hello world")\x0d\x0a\x0d\x0a在servlet上下文中以“hello”为键,保存了“hello world”这一个字符串,如果要在jsp中调用,则用如下jsp脚本\x0d\x0a\x0d\x0a\x0d\x0a2、利用session在同一个会话共享数据\x0d\x0a利用HttpSession共享同一个会话的数据。这也要用到session的getAttribute()/setAttribute()方法,和ServletContext()的使用差不多的。\x0d\x0a3、利用request共享一次请求的数据\x0d\x0a一次请求当中,可以利用request的getAttribute()/setAttribute()方法在servlet和jsp
页面间共享数据。jsp通过servlet传值到另外一个jsp的方法:
jsp通过form表单传给servlet的值通过request对象可以传到另外一个jsp。
Servlet传值给Jsp
具体实现如下:
java代码:String a= "abccdefg "
request.setAttribute( "ValueA ",a)
request.getRequestDispatcher( "网址/jsp页面 ").forward(request,response)
jsp页面:
<%
String s =(String)request.getAttribute( "ValueA ")
%>
jsp页面就可以取出Servlet的值。
评论列表(0条)