开发者

Printing values one by one in jsp

I use a servlet to get values from database and I want to print that in jsp. My problem is the values are printed like usera userb userc. I want the output to be like

usera 
userb
userc

Please help me to do this. Here is what i have tried

<%
String Users=request.getParameter("Users");
String User[]=Users.trim().split(" 开发者_JS百科");
for(int i=0;i<User.length;i++){
    out.println(User[i]);
}
%>


Since you are outputting html, you should add <br /> after each user:

 out.println(User[i] + "<br />");

Note that it is not advisable to use java code in JSPs. Write your java code in a servlet, place the results as request attributes, and then forward to a JSP, where you can show the result using JSTL.


<%
String Users=request.getParameter("Users");
String User[]=Users.trim().split(" ");
for(int i=0;i<User.length;i++){
    out.println(User[i]+"<br/>");
}
%>

I would suggest you to go for JSTL

Here is how it should be :

Perform java code at servlet and forward request to a jsp file

Servlet :

String Users=request.getParameter("Users");
String User[]=Users.trim().split(" ");
request.setAttribute("name", User);

in that jsp file

<c:forEach var = "userName" items = "${name}">
<tr>
<font color="#000080"><td>${userName}</td></font>
</tr>
</c:forEach>  

See Also

  • How to avoid java code in jsp files
  • Servlet Info
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜