jsp中利用response.senddirect(str)重定向,传递参数新思路
2015-01-01 11:02
351 查看
用Servlet进行请求重定向,参数传递好办,直接用request.setAttribute(str1,str2);
但是如果不用Servlet 而是直接用jsp进行转发呢?
我们首先要知道 请求的重定向:在最终的Servlet中,request对象和中转的那个request不是同一个对象
所以传递参数,自然就获取不到了
下面我们换思路,另辟蹊径,我们用session session的生命周期长啊 所以完全可以获取
至于session与request的知识,请查阅相关资料
下面贴出登录出现密码或用户名错误 跳转至login页面
userBean对用户检测部分进行了封装。这个自己可以动手写一下。
但是如果不用Servlet 而是直接用jsp进行转发呢?
我们首先要知道 请求的重定向:在最终的Servlet中,request对象和中转的那个request不是同一个对象
所以传递参数,自然就获取不到了
下面我们换思路,另辟蹊径,我们用session session的生命周期长啊 所以完全可以获取
至于session与request的知识,请查阅相关资料
下面贴出登录出现密码或用户名错误 跳转至login页面
<% userBean bean = new userBean(); String name = request.getParameter("userName"); String password = request.getParameter("userPassword"); String str1 = ""; if(bean.isVerifyLoginInfo(name, password)) str1 = "login success"; else { str1 = "fail in login,password or username has error."; %> <%-- 请求转发 <jsp:forward page="login.jsp"> <jsp:param name="error" value="<%=str1%>"/> </jsp:forward> --%> <% //请求重定向 session.setAttribute("error",str1); //request.setAttribute("error",str1); response.sendRedirect("login.jsp"); } %>
<% //利用重定向获取到的参数属性 //String str = (String)session.getAttribute("error"); String str = (String)request.getAttribute("error"); /*利用请求转发获取的参数 String str = request.getParameter("error"); if(str == null) str=""; */ if(str == null) str=""; %> <h2 align="center"><font color="red"><%=str %></font></h2>
userBean对用户检测部分进行了封装。这个自己可以动手写一下。
相关文章推荐
- jsp中使用response.sendRedirect重定向页面传递中文参数
- 【疑问】如何在jsp中response.sendRedirect()方法中传递中文参数
- SpringMVC利用return new ModelAndView(new RedirectView("xxx.do"), Map map)重定向传递多参数
- jsp中response.sendRedirect()中文参数乱码问题
- jsp中利用jquery+ajax传递json格式参数与前后台之间
- response.sendRedirect 传递参数的问题
- response.sendRedirect传递中文参数
- jsp利用URI 传递中文 参数
- jsp中获取当前页的url & response.sendRedirect(url)参数过长及乱码问题
- jsp中利用jquery+ajax传递json格式参数与前后台之间
- jsp servlet框架参数传递及控制器转发,重定向代码
- request.getRequestdispacher("result.jsp“).forward(request,response)与response.sendRedirect()重定向
- Flex中如何利用HTTPService的send(),将参数传递给HTTPService
- 有关jsp中的response.sendRedirect()中文参数的问题
- jsp中获取当前页的url & response.sendRedirect(url)参数过长及乱码问题
- jsp中response.sendRedirect()中文参数乱码问题
- jsp重定向out.print() ; response.sendRedirect()
- jsp重定向out.print() ; response.sendRedirect()
- response.sendRedirect 传递参数可能出现的问题(乱码、地址映射出错)
- 备忘录:jsp重定向。使用out.print(); response.sendRedirect();