index.jsp页面:
<form action="action.jsp" method="post">
<textarea rows="5" cols="30" name="txt"></textarea><br/>
<input type="submit" value="提交"/>
</form>
action.jsp页面:
<body>
<%
request.setCharacterEncoding("GBK");
String sourceStr = request.getParameter("txt").trim();
String lines[] = sourceStr.split(",|,"); //根据中文/英文的逗号分隔字符串为数组
StringBuffer strbuilder = new StringBuffer(); //创建字符串构建器
for(String line:lines){
//把每个数组元素的字符串与回车符相连并添加到字符串构建器中
strbuilder.append(line + "\n");
}
%>
<textarea rows="5" cols="30">
<%=strbuilder.toString() %>
</textarea>
</body>
trim()
------解决方案--------------------
第12行到14行代码这样写成一行,如
<textarea rows="5" cols="30"><%=strbuilder.toString() %></textarea>