0

Reference: HTML forms, php and apostrophes

I've been trying to pass data from my HTML form to my servlet for processing. However, I noticed that I'd lose the apostrophes in the text inputs. I'm not sure if this is a client or server side processing error, but looking through the reference above, i think i need to do some processing on the servlet side? Tried looking for a servlet alternative to the above but couldn't find any.

Here's the code snippets:

Html form:

<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>

This is the servlet code that processes the form:

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");

PLEASE HELP THE NEWBIE.

Community
  • 1
  • 1
  • can you please give an example of the string that you are sending to the response, and what you are receiving there? – fmodos Jun 28 '13 at 13:04

1 Answers1

0

As this link explains you could simply config a filter

<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>

having this:

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);
   }
}

so you force an UTF-8 encoding.

fGo
  • 1,146
  • 5
  • 11