Reputation: 6707
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);
Upvotes: 1
Views: 18661
Reputation: 10082
String[] values = getParameterValues(“Input Parameter”);
try this. Read more about this method
Upvotes: 0
Reputation: 597116
request.getRequestDispatcher("welcome_user.jsp").forward()
) - just add another request.setAttribute("attrName", value);
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.)
Upvotes: 3
Reputation: 240898
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);
Upvotes: 3
Reputation: 39907
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
Upvotes: 3