[英]Get Textarea Line breaks in a Java String
我有以下表格:
<form method="post" action="./Contact">
<input type="text" name="Problem"><br>
<textarea name="message"></textarea>
<input type="submit" value="Send Problem">
</form>
進入聯系Servlet代碼:
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
request.setCharacterEncoding("UTF-8");
String b = request.getParameter("Problem");
String a = request.getParameter("message");
request.setAttribute("message", a);
request.setAttribute("problem", b);
request.getRequestDispatcher("./index.jsp").forward(request, response);
}
只是測試消息,將其轉換為index.jsp代碼:
<p> Problem: ${problem}</p>
<p> Message: ${message}</p>
它正在運行,但是如果書面消息為:“這是一條消息;
這是一條新的訊息。
我在打破界限。”
打印的消息將是:“這是一條消息;這是一條新的消息行,我正在換行。”
如何使用字符串從textarea消息中獲得書面的換行符?
這樣使用StringBuffer
,
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
request.setCharacterEncoding("UTF-8");
String b = request.getParameter("Problem");
// String a = request.getParameter("message");
StringBuffer text = new StringBuffer(request.getParameter("message"));
int loc = (new String(text)).indexOf('\n');
while(loc > 0){
text.replace(loc, loc+1, "<BR>");
loc = (new String(text)).indexOf('\n');
}
request.setAttribute("message", text);
request.setAttribute("problem", b);
request.getRequestDispatcher("./index.jsp").forward(request, response);
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.