我所了解的servlet传值的方式

来源:互联网 发布:js 选择文件路径 编辑:程序博客网 时间:2024/05/17 06:20

1. 定向传值

 

request.setAttribute("key", value);request.getRequestDispatcher("index.jsp").forward(request, response);

2. ajax+servlet


//生成JSON数据JSONArray arr = new JSONArray(valueList);String arrString = arr.toString();//响应到客户端request.setCharacterEncoding("utf-8");response.setContentType("text/plain;charset=utf-8");response.getWriter().print(arrString);

3. 页面响应传值

if(null != ui){request.getSession().setAttribute("key", value);response.sendRedirect("index.jsp");}else {request.setCharacterEncoding("utf-8");response.setContentType("text/html;charset=utf-8");PrintWriter pw = response.getWriter();StringBuffer sb = new StringBuffer();sb.append("<script>");sb.append("alert('用户名或密码错误,请重新登录');location.href='login.jsp';");sb.append("</script>");pw.write(sb.toString());}


0 0
原创粉丝点击