Sumithra
Sumithra

Reputation: 6707

Sending multiple values from servlet to jsp

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

Answers (4)

Tnadev
Tnadev

Reputation: 10082

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

try this. Read more about this method

Upvotes: 0

Bozho
Bozho

Reputation: 597116

  • 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.)

Upvotes: 3

Jigar Joshi
Jigar Joshi

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

Adeel Ansari
Adeel Ansari

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

Related Questions