如何在javaservlet和jsp间互传数据

大哥们,请问一下,如何在javaservlet和jsp间互传数据
最新回答
微浅笑°

2024-07-01 02:18:46

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页面间共享数据。

对天空说爱你

2024-07-01 03:03:43

一、jsp往 servlet 传  : 
     1.jsp页面添加标签 : <input type="text" name="test" /> 
     2 . servlet doPost方法中取:request.getParameter("test");
     
二、servlet 往jsp 传
1.servlet doPost方法中往session传值:request.getAttrebute().getSession().put(key , value)
2.jsp java代码取:<% request.getAttrebute().getSeesion().getString(key) %>
雨季盛开的花

2024-07-01 02:13:31

把传的数据放到request里就可以
我不会写诗

2024-07-01 01:47:33

request response ,jsp 地址栏传参
佐佐木惠理

2024-07-01 02:00:26

有方法实现,通过get或者post