I have a servlet code where I check for username and password in a helper class and after checking it comes back to servle开发者_开发百科t 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);
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
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);
- If you use forwarding (
request.getRequestDispatcher("welcome_user.jsp").forward()
) - just add anotherrequest.setAttribute("attrName", value);
- if you retain the redirect - add another get parameter.
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.)
String[] values = getParameterValues(“Input Parameter”);
try this. Read more about this method
精彩评论