在doGet函数以request作参数调用自定义的setBean函数,在setBean函数里,将传递进来的request,用setAttribute增加了一个数据,如request.setAttribute("str", str),setBean函数完成回到doGet函数转发请求到jsp页面,jsp页面拿不出"str"这个共享数据了servlet-doGet: public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String action = request.getParameter("act"); //判断获取页面还是删除记录
if (action.equals("1")) {
setBean(request);
RequestDispatcher view = request.getRequestDispatcher("/fdbuser.jsp");
view.forward(request, response);
servlet-setBean: String s = "hello world";
request.getSession().setAttribute("record", record);
request.getSession().setAttribute("str", s); } catch (SQLException e) {}
jsp:<%@ page contentType="text/html; charset=utf-8" import="bean.RecordBean,java.util.*"%><html><body>
<%
RecordBean [] rb = (RecordBean [])session.getAttribute("record");
String s = (String)request.getSession().getAttribute("str"); if (rb == null) {
out.print("none");
} else
out.print("ok"); if (s == null) {
out.print("none");
} else
out.print("ok");
%>
</body></html>
throws ServletException, IOException {
String action = request.getParameter("act"); //判断获取页面还是删除记录
if (action.equals("1")) {
setBean(request);
RequestDispatcher view = request.getRequestDispatcher("/fdbuser.jsp");
view.forward(request, response);
servlet-setBean: String s = "hello world";
request.getSession().setAttribute("record", record);
request.getSession().setAttribute("str", s); } catch (SQLException e) {}
jsp:<%@ page contentType="text/html; charset=utf-8" import="bean.RecordBean,java.util.*"%><html><body>
<%
RecordBean [] rb = (RecordBean [])session.getAttribute("record");
String s = (String)request.getSession().getAttribute("str"); if (rb == null) {
out.print("none");
} else
out.print("ok"); if (s == null) {
out.print("none");
} else
out.print("ok");
%>
</body></html>
view.forward(request, response);这段代码加上return;语句没?
request.getSession().setAttribute("str", s);请问这两句中的record 和 s 是从哪里来的
应该是record没有赋值吧。
现在的问题是,在jsp获取String s = (String)request.getAttribute("str");s是空值。
同样获取record的rb都是空值。
request.setAttribute(...
request.getAttribute(...
我还试过,将request.setAttribute用在doGet函数是可以的。