java 将多个值从 servlet 发送到 jsp
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4437575/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Sending multiple values from servlet to jsp
提问by Sumithra
I have a servlet code where I check for username and password in a helper class and after checking it comes back to servlet and lists users. The username along with the list of users must be displayed in the jsp. How to send both the values? Here is the code which I have tried. It displays nothing String outp=t.Welcome(name, pwd);
我有一个 servlet 代码,我在帮助类中检查用户名和密码,检查后返回 servlet 并列出用户。用户名和用户列表必须显示在 jsp 中。如何发送这两个值?这是我尝试过的代码。它不显示任何内容 String outp=t.Welcome(name, pwd);
String Users=t.List_Of_Users();
String User[]=Users.trim().split(" ");
request.setAttribute("name", User);
response.sendRedirect("Welcome_User.jsp?Users="+User+"&outp="+outp);
回答by Bozho
- If you use forwarding (
request.getRequestDispatcher("welcome_user.jsp").forward()
) - just add anotherrequest.setAttribute("attrName", value);
- if you retain the redirect - add another get parameter.
Welcome_User.jsp?Users="+User+"&outp="+outp + "&another=" + another;
(and remove therequest.setAttribute(..)
)
- 如果您使用转发 (
request.getRequestDispatcher("welcome_user.jsp").forward()
) - 只需添加另一个request.setAttribute("attrName", value);
- 如果您保留重定向 - 添加另一个 get 参数。
Welcome_User.jsp?Users="+User+"&outp="+outp + "&another=" + another;
(并删除request.setAttribute(..)
)
In order to represent an array as string you have multiple options. One of which is Arrays.toString(array)
为了将数组表示为字符串,您有多种选择。其中之一是Arrays.toString(array)
(Note that sending a password as a get parameter is a security problem.)
(请注意,将密码作为 get 参数发送是一个安全问题。)
回答by Jigar Joshi
You can set as many attributes as you want , also optimization should be taken care of. ,
您可以根据需要设置任意数量的属性,还应注意优化。,
request.setAttribute("key1", Object1);
request.setAttribute("key2", Object2);
request.setAttribute("key3", Object3);
.
.
.
request.setAttribute("keyn", Objectn);
then
然后
String destination = "/WEB-INF/pages/result.jsp";
RequestDispatcher rd = getServletContext().getRequestDispatcher(destination);
rd.forward(request, response);
回答by Adeel Ansari
response.sendRedirect()
will clear the buffer, which apparently means any request attributes previously set will not be retained.
response.sendRedirect()
将清除缓冲区,这显然意味着之前设置的任何请求属性都不会保留。
In your case, I believe, its better to use RequestDispatcher.forward()
, after setting your desired attributes in request object.
在您的情况下,我相信,RequestDispatcher.forward()
在请求对象中设置所需的属性后,最好使用。
NB:By convention you must define your variable names starting with a small letter. For example, String user;
, instead of String User;
. Second, the method names should not use underscores. Further, I would suggest self-explanatory names. Below is your snippet with a little renaming.
注意:按照惯例,您必须定义以小写字母开头的变量名。例如,String user;
, 而不是String User;
。其次,方法名称不应使用下划线。此外,我建议使用不言自明的名称。下面是您的代码片段,稍作重命名。
String userNamesStr =t.userNamesSpaceDelimited();
String[] userNameArr = userNamesStr.trim().split(" "); // Or userNames, but we usually follow this for List
回答by VdeX
String[] values = getParameterValues(“Input Parameter”);
try this. Read more about this method
试试这个。阅读有关此方法的更多信息