Can't get text area contents enclosed in <…> in servlet

浪子不回头ぞ 提交于 2019-12-14 04:18:39

问题


I have a form with a textarea in an HTML page and I'm trying to send its contents to a servlet via POST. The problem is that if I write something like unenclosed <enclosed> in the textarea, when I call request.getParameter("textareaId") (where request is a HttpServletRequest object) in the service() method of the servlet (that extends HttpServlet), I get only "unenclosed" as parameter value. Is there any way to get the complete contents of the textarea?

Html code:

<form action="MyServlet" method="post">
    <textarea name="MyTextarea" rows="5" cols="50"></textarea>
</form>

Servlet code:

public class MyServlet extends HttpServlet {
    public void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String contents = request.getParameter("MyTextarea");
        System.out.println(contents);
    }
}

Thanks!

EDIT: I solved it by escaping the textarea contents in a JavaScript function before sending it to the servlet.


回答1:


This index.jsp works for me on Tomcat 6.0.35. I see the < and > printed to the console.

<%
    String contents = request.getParameter("MyTextarea");
    System.out.println(contents);
%>
<form method="post">
    <textarea name="MyTextarea" rows="5" cols="50"></textarea>
    <input type=submit>
</form>

What servlet container are you using?

Does the container have any parameter escaping turned on by default?

What is the default character encoding (can't really see that being an issue as < and > are regular ASCII)?




回答2:


Inside the <textarea name="MyTextarea">...</textarea> one should use &lt; and &gt;. This can be done with apache common's EscapeUtils.escapeHTML (I think). This also holds for <input value="..."> or in <a href="... .jsp?a=1&amp;b=2">: &amp; &lt; &gt; &quot; &apos;.



来源:https://stackoverflow.com/questions/16149347/cant-get-text-area-contents-enclosed-in-in-servlet

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!