Servlet filters special HTML characters of a string

Source: Internet
Author: User

Servlet filters special HTML characters of a string

(1) In some cases, when users enter data, we need to determine whether the data is valid, that is, to filter out whether the user input information contains special characters;

(2) directly add the code for your reference:

ServletUtilities class:

package com.lc.ch04Biaodanshuju;import javax.servlet.http.HttpServletRequest;public class ServletUtilities {   public static String filter(String input) {    if (!hasSpecialChars(input)) {      return(input);    }    StringBuffer filtered = new StringBuffer(input.length());    char c;    for(int i=0; i
 
  ': filtered.append(">"); break;        case '"': filtered.append("""); break;        case '&': filtered.append("&"); break;        default: filtered.append(c);      }    }    return(filtered.toString());  }  private static boolean hasSpecialChars(String input) {    boolean flag = false;    if ((input != null) && (input.length() > 0)) {      char c;      for(int i=0; i
  
   ': flag = true; break;          case '"': flag = true; break;          case '&': flag = true; break;        }      }    }    return(flag);  }}
  
 


 Submit Code Sample
 
  Submit Code Sample
 

package com.lc.ch04Biaodanshuju;import java.io.IOException;import java.io.PrintWriter;import javax.servlet.ServletException;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;public class BadCodeServlet extends HttpServlet {  public void doGet(HttpServletRequest request,                    HttpServletResponse response)      throws ServletException, IOException {    response.setContentType("text/html");    PrintWriter out = response.getWriter();    String title = "Code Sample";    String docType =      "\n";    out.println(docType +                "\n" +                "" + title + "\n" +                "\n" +                "" + title + "\n" +                "
\n" +                getCode(request) +                "
\n" + "Now, wasn't that an interesting sample\n" + "of code?\n" + ""); } protected String getCode(HttpServletRequest request) { return(request.getParameter("code")); }}



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.