Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/316.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 表单提交后如何显示参数(将字符转换为HTML实体名称)_Java_Html_Jsp - Fatal编程技术网

Java 表单提交后如何显示参数(将字符转换为HTML实体名称)

Java 表单提交后如何显示参数(将字符转换为HTML实体名称),java,html,jsp,Java,Html,Jsp,我试图通过编码HTML响应来编写脚本,以防止跨站点脚本攻击。但是我不知道如何在表单提交之前转换字符 html 编码html响应不是防止CSRF攻击的正确方法。如果不想编写CSRF保护,请使用。这是一个筛选器,可以与jsp项目集成。另一个选项是您可以自己手动控制CSRF。请检查此选项 <% String mobile = common.setNullToString(request.getParameter("mobile")); String converted

我试图通过编码HTML响应来编写脚本,以防止跨站点脚本攻击。但是我不知道如何在表单提交之前转换字符

html
编码html响应不是防止CSRF攻击的正确方法。如果不想编写CSRF保护,请使用。这是一个筛选器,可以与jsp项目集成。另一个选项是您可以自己手动控制CSRF。请检查此选项

<%
    String mobile       = common.setNullToString(request.getParameter("mobile"));
    String converted_param= detect_xss.escapeHtml(mobile); //convert method
%>

<form name="mainform" action="test2.jsp">
<input type="text" name="name" value="" >
<input type="button" onclick="button();">
</form>
public class detect_xss {
    public static final HashMap m = new HashMap();
    static{
     m.put(34, "&quot;"); // < - less-than
     m.put(60, "&lt;"); // < - less-than
     m.put(62, "&gt;"); // > - greater-thanof entities and integer value of a char
    }

     public static String escapeHtml(String html) 
     {
         String str = html;
         try 
         {
             StringWriter writer = new StringWriter((int)(str.length() * 1.5));
             escape(writer, str);
             return writer.toString();
         }
         catch (IOException ioe) 
         {
             ioe.printStackTrace();
             return null;
         }
    }

     public static void escape(Writer writer, String str) throws IOException 
     {
         int len = str.length();
         for (int i = 0; i < len; i++) 
         {
             char c = str.charAt(i);
             int ascii = (int) c;
             String entityName = (String) m.get(ascii);
             if (entityName == null) 
             {
                 if (c > 0x7F) 
                 {
                    writer.write("&#");
                    writer.write(Integer.toString(c, 10));
                    writer.write(';');
                 }
                 else {
                     writer.write(c);
                 }
             } else {
                 writer.write(entityName);
             }
         }
     }

}
http://localhost:9080/home/test2.jsp?mobile=<script>alert%28"test"%29<%2Fscript>
http://localhost:9080/amgeneral/test2.jsp?mobile=&lt;script&gt;alert(&quot;test&quot;)&lt;/script&gt;