如何转换HTML表单textarea的JSP请求参数的实体? [英] How to convert entites of JSP request arguments for html form textarea?

查看:125
本文介绍了如何转换HTML表单textarea的JSP请求参数的实体?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是JSP新手,并生成一个带有文本区域的表单。是否有一个库将文本转换为HTML的FORM TEXTAREA,并将其转换为实体以便URL正确格式化或解析?



例如:



textarea(命名为ta):

 使用&符号&简单测试;在textarea 

url:

  http://.../myapp.jsp?ta = simple + test + with +&符+%26 + in + textarea 
解决方案

1.5.0 / docs / api / java / net / URLEncoder.html#encode%28java.lang.String,%20java.lang.String%29rel =nofollow noreferrer> URLEncoder.encode(String string,String encoding) 将字符串安全地编码以便在URL中使用。它抛出UnsupportedEncodingException,所以一定要记住。以下是一个JSP示例,它将字符串编码并将其显示为文档的主体。

 <%@ page language = java
import =java.net.URLEncoder
contentType =text / html; charset = UTF-8
pageEncoding =UTF-8%>

<!DOCTYPE html PUBLIC - // W3C // DTD HTML 4.01 Transitional // ENhttp://www.w3.org/TR/html4/loose.dtd\">

<%

String encoded = null;
尝试{
encoded = URLEncoder.encode(简单测试,使用&符号在textarea中,UTF-8);
} catch(Exception e){

}

%>
< html>
< head>
< title> MyTitle< / title>
< / head>
< body>
<%=编码的%>
< / body>
< / html>






使用JSTL会更好,这个例子特别是< c:url> 标签,它会自动编码它的内容。例如,要获得您在问题中提到的编码的字符串URL,您可以这样做:

 < c:url var =myEncodedURLvalue =http://.../myapp.jsp> 
< c:param name =tavalue =用&符号&在textarea中简单测试/>
< / c:url>

然后您可以使用表达式 $ {myEncodedURL}访问。如果您目前没有使用JSTL,那么就会涉及一条学习曲线 - 您需要设置taglib,然后导入并使用它。您可以在 developerworks 上查看有关如何使用此JSTL标签的更多信息。 。


I am new to JSP and generating a form with a text area. Is there a library to convert the text from/to an HTML's FORM TEXTAREA that will convert to/from entities for the URL to be properly formatted/parsed?

For example:

textarea (named ta):

simple test with ampersand & in textarea

url:

http://.../myapp.jsp?ta=simple+test+with+ampersand+%26+in+textarea

解决方案

If you are using scriptlets, you can use the URLEncoder.encode(String string, String encoding) to encode Strings in safely for use in URLs. It throws UnsupportedEncodingException, so make sure you catch that. Here's an example JSP that encodes your string and displays it as the body of the document.

<%@ page language="java"
  import="java.net.URLEncoder"
  contentType="text/html; charset=UTF-8"
  pageEncoding="UTF-8"%>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<%

String encoded = null;
try {
    encoded = URLEncoder.encode("simple test with ampersand & in textarea", "UTF-8");
} catch (Exception e) {

}

%>
<html>
  <head>
    <title>MyTitle</title>
  </head>
  <body>
    <%=encoded%>
  </body>
</html>


It would be better practice to use JSTL, in this case specifically the <c:url> tag which will automatically encode its content. For example, to get the encoded String URL you mentioned in your question, you might do this:

<c:url var="myEncodedURL" value="http://.../myapp.jsp">
  <c:param name="ta" value="simple test with ampersand & in textarea"/>
</c:url>

Which you could then access with the expression ${myEncodedURL}. If you're not using JSTL at the moment then there's a learning curve involved - you'll need to set up the taglib, import it at then use it. You can see more on how to use this JSTL tag on developerworks.

这篇关于如何转换HTML表单textarea的JSP请求参数的实体?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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