1

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);
Sumithra
  • 6,587
  • 19
  • 51
  • 50

4 Answers4

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

Bozho
  • 588,226
  • 146
  • 1,060
  • 1,140
3

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);
jmj
  • 237,923
  • 42
  • 401
  • 438
  • These are not parameters, these are attributes. Both are having different meaning here, in this context. So, you should be careful. ;) – Adeel Ansari Dec 14 '10 at 10:05
  • @Adeel yeah . I think OP wants to pass data from servlet to jsp, So i don't think any issue with this approach. I know attribute and parameter are different things, Thanks btw :) – jmj Dec 14 '10 at 10:07
  • @org.life.java: I edited your post, I hope you didn't mind. Now, I hope, you already got what I meant. – Adeel Ansari Dec 14 '10 at 10:11
  • why is result.jsp in WEB-INF. isn't WEB-INF for the classes, lib, configuration folders and files? – 124697 Dec 14 '10 at 10:12
  • @Adeel Thanks, I didn't re-check my answer after your comment ;) Thanks btw – jmj Dec 14 '10 at 10:13
  • @user521180: No, `WEB-INF` is there, so no resource can access the inside things from outside. Simple as that. – Adeel Ansari Dec 14 '10 at 10:15
  • @user521180 Yeah, it was just to demonstrate – jmj Dec 14 '10 at 10:15
  • @org.life.java: No no buddy, they are not. That was perfectly fine. You can revert the thing if you want to. – Adeel Ansari Dec 14 '10 at 10:16
  • @Adeel how will I access the jsp using GET request directly if its in `WEB-INF` – jmj Dec 14 '10 at 10:17
  • @org.life.java: The question is why on earth you want to access a JSP directly, without going through a servlet? Its this way `request-initiated -> servlet-mapping -> servlet -> jsp -> request-fulfilled` – Adeel Ansari Dec 14 '10 at 10:19
  • @Adeel totally agree with you. but if OP wants directly to display JSP then ?, Its good thing to hide jsp directly from user that is true, – jmj Dec 14 '10 at 10:22
  • @org.life.java: Then we have to tell OP, that its not a good practice and so discouraged, in order to make our life easier. Do you really wanna see those kinda code from your fellow programmers? I doubt it. :) – Adeel Ansari Dec 14 '10 at 10:26
  • @Adeel just FYI http://stackoverflow.com/questions/4430647/jsp-java-code-html-table/4430688#4430688 :) – jmj Dec 14 '10 at 10:27
  • @user521180 follow this discussion you will have great idea how it should be designed, Thanks @Adeel :) – jmj Dec 14 '10 at 10:28
  • @org.life.java: I have visited your referred thread, but I hardly can find any relation between this and that. :( – Adeel Ansari Dec 14 '10 at 10:59
  • @Adeel there is some concern of this link and this discussion, anyways – jmj Dec 14 '10 at 11:00
3

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
Adeel Ansari
  • 39,541
  • 12
  • 93
  • 133
0
String[] values = getParameterValues(“Input Parameter”);

try this. Read more about this method

VedantK
  • 9,728
  • 7
  • 66
  • 71