将多个值从 servlet 发送到 jsp

发布于 2024-10-07 15:33:40 字数 342 浏览 2 评论 0原文

我有一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

萌辣 2024-10-14 15:33:40

response.sendRedirect() 将清除缓冲区,这显然意味着之前设置的任何请求属性都不会被保留。

在你的情况下,我相信,最好使用 RequestDispatcher.forward(),在请求对象中设置所需的属性后。

注意:按照惯例,您必须定义以小写字母开头的变量名称。例如,String user;,而不是 String User;。其次,方法名称不应使用下划线。此外,我建议使用不言自明的名称。下面是您的代码片段,并进行了一些重命名。

String userNamesStr =t.userNamesSpaceDelimited();
String[] userNameArr = userNamesStr.trim().split(" "); // Or userNames, but we usually follow this for List

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 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 userNamesStr =t.userNamesSpaceDelimited();
String[] userNameArr = userNamesStr.trim().split(" "); // Or userNames, but we usually follow this for List
岁月打碎记忆 2024-10-14 15:33:40

你可以设置任意数量的属性,也应该注意优化。 ,

request.setAttribute("key1", Object1);
request.setAttribute("key2", Object2);
request.setAttribute("key3", Object3);
.
.
.
request.setAttribute("keyn", Objectn);

然后

String destination = "/WEB-INF/pages/result.jsp";
RequestDispatcher rd = getServletContext().getRequestDispatcher(destination);
rd.forward(request, response);

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);
忆梦 2024-10-14 15:33:40
  • 如果您使用转发 (request.getRequestDispatcher("welcome_user.jsp").forward()) - 只需添加另一个 request.setAttribute("attrName", value);
  • if您保留重定向 - 添加另一个获取参数。 Welcome_User.jsp?Users="+User+"&outp="+outp + "&another=" + another; (并删除 request.setAttribute(..))

为了将数组表示为字符串,您有多种选择,其中之一是 Arrays.toString(array)

(请注意,将密码作为 get 参数发送是一个安全问题。)

  • If you use forwarding (request.getRequestDispatcher("welcome_user.jsp").forward()) - just add another request.setAttribute("attrName", value);
  • if you retain the redirect - add another get parameter. Welcome_User.jsp?Users="+User+"&outp="+outp + "&another=" + another; (and remove the request.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.)

尘世孤行 2024-10-14 15:33:40
String[] values = getParameterValues(“Input Parameter”);

试试这个。阅读有关此方法的更多信息

String[] values = getParameterValues(“Input Parameter”);

try this. Read more about this method

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文