将多个值从 servlet 发送到 jsp
我有一个 servlet 代码,我在助手类中检查用户名和密码,检查后返回到 servlet 并列出用户。用户名以及用户列表必须显示在 jsp 中。如何发送这两个值?这是我尝试过的代码。它什么也不显示 String outp=t.Welcome(姓名,密码);
String Users=t.List_Of_Users();
String User[]=Users.trim().split(" ");
request.setAttribute("name", User);
response.sendRedirect("Welcome_User.jsp?Users="+User+"&outp="+outp);
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);
String Users=t.List_Of_Users();
String User[]=Users.trim().split(" ");
request.setAttribute("name", User);
response.sendRedirect("Welcome_User.jsp?Users="+User+"&outp="+outp);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
response.sendRedirect()
将清除缓冲区,这显然意味着之前设置的任何请求属性都不会被保留。在你的情况下,我相信,最好使用
RequestDispatcher.forward()
,在请求对象中设置所需的属性后。注意:按照惯例,您必须定义以小写字母开头的变量名称。例如,
String user;
,而不是String User;
。其次,方法名称不应使用下划线。此外,我建议使用不言自明的名称。下面是您的代码片段,并进行了一些重命名。response.sendRedirect()
will clear the buffer, which apparently means any request attributes previously set will not be retained.In your case, I believe, its better to use
RequestDispatcher.forward()
, after setting your desired attributes in request object.NB:By convention you must define your variable names starting with a small letter. For example,
String user;
, instead ofString User;
. Second, the method names should not use underscores. Further, I would suggest self-explanatory names. Below is your snippet with a little renaming.你可以设置任意数量的属性,也应该注意优化。 ,
然后
You can set as many attributes as you want , also optimization should be taken care of. ,
then
request.getRequestDispatcher("welcome_user.jsp").forward()
) - 只需添加另一个request.setAttribute("attrName", value);
Welcome_User.jsp?Users="+User+"&outp="+outp + "&another=" + another;
(并删除request.setAttribute(..))
为了将数组表示为字符串,您有多种选择,其中之一是 Arrays.toString(array)
(请注意,将密码作为 get 参数发送是一个安全问题。)
request.getRequestDispatcher("welcome_user.jsp").forward()
) - just add anotherrequest.setAttribute("attrName", value);
Welcome_User.jsp?Users="+User+"&outp="+outp + "&another=" + another;
(and remove therequest.setAttribute(..)
)In order to represent an array as string you have multiple options. One of which is
Arrays.toString(array)
(Note that sending a password as a get parameter is a security problem.)
试试这个。阅读有关此方法的更多信息
try this. Read more about this method