0

参考:HTML 表单、php 和撇号

我一直在尝试将数据从我的 HTML 表单传递到我的 servlet 进行处理。但是,我注意到我会丢失文本输入中的撇号。我不确定这是客户端还是服务器端处理错误,但是通过上面的参考,我认为我需要在 servlet 端进行一些处理?尝试寻找替代上述方法的 servlet,但找不到。

这是代码片段:

html格式:

<form method="post" action="CreateThreadServlet">
                        <b>Title</b><br>
                        <input type="text" name="title" size="60%" placeholder="Enter your title here"/><br>

                        <br><b>Tags</b><br>
                        <input type="text" name="tags" placeholder="Additional Tags: comma separated, e.g. Gamification, Java" size="60%" /><br>  

                        <br><b>Details</b><br>
                        <textarea name="content" style="width:100%;height:50%"></textarea>
                        <input type="hidden" name="nick" value=<%=nick%>>
                        <input type="hidden" name="userid" value=<%=userid%>>
                        <button type="button" style='float: right;' onClick="closeDimmer()">Cancel</button>
                        <input type="submit" name="Submit" value="Submit" text-align='center' style='float: right;'>

                    </form>

这是处理表单的 servlet 代码:

String userid = req.getParameter("userid");
    String nick = req.getParameter("nick");
    String title = null; //tried using the URLDecoder, doesn't work
    try {
        title = URLDecoder.decode(req.getParameter("title"), "UTF-8");
    } catch (UnsupportedEncodingException ex) {
        Logger.getLogger(CreateThreadServlet.class.getName()).log(Level.SEVERE, null, ex);
    }
    String tags = req.getParameter("tags");
    String[] tagStr = tags.split(",");
    String[] addTags = req.getParameterValues("addTags");

请帮助新手。

4

1 回答 1

0

正如这个链接所解释的,你可以简单地配置一个过滤器

<filter>
    <filter-name>HitCounterFilter </filter-name>
    <filter-class>
        net.my.filters.HitCounterFilter
    </filter-class>
</filter>
<filter-mapping>
    <filter-name>HitCounterFilter</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>

有这个:

public final class HitCounterFilter implements Filter {
   private FilterConfig filterConfig = null;
   public void init(FilterConfig filterConfig) 
      throws ServletException {
      this.filterConfig = filterConfig;
   }
   public void destroy() {
      this.filterConfig = null;
   }
   public void doFilter(ServletRequest request,
      ServletResponse response, FilterChain chain) 
      throws IOException, ServletException {
     if (request.getCharacterEncoding() == null) {
        request.setCharacterEncoding("UTF-8");
     }
     chain.doFilter(request, wrapper);
   }
}

所以你强制使用UTF-8编码。

于 2013-06-28T13:32:49.303 回答