日期:2014-05-19  浏览次数:20793 次

jsp textarea的换行字符如何转换
<form name="myform" method="post" action="reply.jsp">

<table width="100%" border="0" align="center" cellpadding="0" cellspacing="1">
  <tr height="20" valign="middle">
  <td>&nbsp;快速回复主题
  </td>
  <td>
<input type="text" name="username" maxlength="500" height="100" size="100"/>
</td>
  </tr>
  <tr>
  <td width="20%" align="center" valign="middle">
  </td>
  <td width="80%">
  <textarea name="rtext" style="width:98%" rows="3"></textarea>
  </td>
  </tr>
  <tr>
  <td>&nbsp;</td>
  <td><input name="Submit" type="submit" value="回复主题 &gt;&gt;&gt;">  
  &nbsp;&nbsp;
  <input name="Submit2" type="reset" value="重 置"></td></tr>
</table>

</form>

<%



if(session.getAttribute("user name")!=null)
{
String userid=session.getAttribute("user name").toString();
String reply_content=request.getParameter("rtext");

String recont=re.toHTMLString(reply_content);
//String reply_content=workFC.UnitoGB(request.getParameter("rtext"));


String replycontent=workFC.UnitoGB(recont);


re.setUserID(userid);
re.setTopicid(topicid);


re.setReplycontent(replycontent);


re.doReply();

}




%>
<%=re.getReplycontent()%>


</body>
</html>


java代码如下

package com.ice.test;
import java.io.*;
import java.sql.*;
import java.util.*;
import java.util.Date;

public class Reply {

/**
* @param args
*/
private String topicid,replycontent,userID;
Connection con=null;
Statement stmt=null;
ResultSet rs=null;
public Reply(){

}

public static String toHTMLString(String in) {  
  StringBuffer out = new StringBuffer();  
  for (int i = 0; in != null && i < in.length(); i++) {  
  char c = in.charAt(i);  
  if (c == '\'')  
  out.append("&#039;");  
  else if (c == '\"')  
  out.append("&#034;");  
  else if (c == '<')  
  out.append("&lt;");  
  else if (c == '>')  
  out.append("&gt;");  
  else if (c == '&')  
  out.append("&amp;");  
  else if (c == ' ')  
  out.append("&nbsp;");  
  else if (c == '\n')  
  out.append("<br/>");  
  else  
  out.append(c);