在Java字符串中获取Textarea换行符 [英] Get Textarea Line breaks in a Java String

查看:577
本文介绍了在Java字符串中获取Textarea换行符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的格式如下:

<form method="post" action="./Contact">
    <input type="text" name="Problem"><br>
    <textarea name="message"></textarea>
    <input type="submit" value="Send Problem">
</form>

进入Contact Servlet代码:

Into Contact Servlet code:

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代码:

Just testing the message, into index.jsp code:

<p> Problem: ${problem}</p>
<p> Message: ${message}</p>

它正在工作,但是如果书面消息是,我遇到了问题: 这是一条消息;

It is working, but I got a problem, if the written message be: "It is a message;

这是一条新消息.

我正在打破界限."

打印的消息将是: 这是一条消息;这是一条新消息.我正在打破界限."

The printed message will be: "It is a message;It is a new line of message.I'm breaking lines."

如何使用字符串从textarea消息中获得书面的换行符?

How can I get, with a String, the written line breaks from textarea message?

推荐答案

像这样使用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);
}

这篇关于在Java字符串中获取Textarea换行符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆