问题
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 <
and >
.
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&b=2">
: & < > " '
.
来源:https://stackoverflow.com/questions/16149347/cant-get-text-area-contents-enclosed-in-in-servlet