标签:ack 方式 index www. logging ue4 count() alt https
package com.filter; import javax.servlet.*; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletRequestWrapper; import javax.servlet.http.HttpServletResponse; import java.io.IOException; import java.io.UnsupportedEncodingException; public class EncodingFilter implements Filter { private FilterConfig config; public void destroy() { } public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) throws ServletException, IOException { String encoding = config.getInitParameter("encoding"); if(encoding == null){ encoding = "utf-8"; } HttpServletRequest request = null; HttpServletResponse response = null; try { request = (HttpServletRequest) req; response = (HttpServletResponse) resp; }catch (Exception e){ throw new RuntimeException("不是Http请求的request和response"); } //解决POST请求和响应信息的字符输出流 request.setCharacterEncoding(encoding); response.setContentType(encoding); response.setContentType("text/html;charset="+encoding); //如果是get请求提交,需要对request进行装饰包装 HttpServletRequestD httpServletRequestD = new HttpServletRequestD(request); chain.doFilter(httpServletRequestD, resp); } public void init(FilterConfig config) throws ServletException { this.config = config; } } class HttpServletRequestD extends HttpServletRequestWrapper{ public HttpServletRequestD(HttpServletRequest request) { super(request); } @Override public String getParameter(String name) { String value = super.getParameter(name); try { //在tomcat8中的编码使用的是utf-8,所以对get请求不需要处理,但如果是tomcat6那么就需要将iso8859-1转变为utf-8 value = new String(value.getBytes(super.getCharacterEncoding()),super.getCharacterEncoding()); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } return value; } @Override public String[] getParameterValues(String name) { String[] values = super.getParameterValues(name); String[] newValue = new String[values.length]; for(int x = 0;x<values.length;x++){//对每一个字符串都进行编码转换 String value = values[x]; try { value = new String(value.getBytes(super.getCharacterEncoding()),super.getCharacterEncoding()); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } newValue[x] = value; } return newValue; } }
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" version="3.1"> <filter> <filter-name>EncodingFilter</filter-name> <filter-class>com.filter.EncodingFilter</filter-class> <init-param> <param-name>encoding</param-name> <param-value>utf-8</param-value> </init-param> </filter> <filter-mapping> <filter-name>EncodingFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <welcome-file-list> <welcome-file>index.html</welcome-file> </welcome-file-list> </web-app>
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>AJAX编码步骤</title> <script type="text/javascript"> window.onload =function () { //GET方式提交 //我们知道GET方式提交的参数是在请求头 document.getElementById("btn").onclick = function () { var input_value = document.getElementById("input_value").value; //1.创建XMLHttpRequest对象 var xmlhttp ; if(window.XMLHttpRequest){ xmlhttp = new XMLHttpRequest(); }else{ xmlhttp = new ActiveXObject("Microsoft.XMLHTTP"); } //注册状态监控回调函数 xmlhttp.onreadystatechange= function () { if(xmlhttp.readyState == 4){ if(xmlhttp.status == 200){ var text = xmlhttp.responseText; alert(text); } } } //建立与服务器的异步连接 xmlhttp.open("GET","/ajax?name="+input_value); //发出异步请求 发出请求体 xmlhttp.send(null); } } </script> </head> <body> <input type="text" id="input_value"/> <input type="button" value="点击" id="btn"/> </body> </html>
package com.controller; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; @WebServlet(name = "AjaxServlet",urlPatterns = "/ajax") public class AjaxServlet extends HttpServlet { protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String name = request.getParameter("name"); System.out.println(name); response.getWriter().println(name); } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { this.doPost(request,response); } }
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>AJAX编码步骤</title> <script type="text/javascript"> window.onload =function () { //POST方式提交 //我们知道POST方式提交的参数是在请求体 document.getElementById("btn").onclick = function () { var input_value = document.getElementById("input_value").value; //1.创建XMLHttpRequest对象 var xmlhttp ; if(window.XMLHttpRequest){ xmlhttp = new XMLHttpRequest(); }else{ xmlhttp = new ActiveXObject("Microsoft.XMLHTTP"); } //注册状态监控回调函数 xmlhttp.onreadystatechange= function () { if(xmlhttp.readyState == 4){ if(xmlhttp.status == 200){ var text = xmlhttp.responseText; alert(text); } } } //建立与服务器的异步连接 xmlhttp.open("POST","/ajax"); //发出异步请求 发出请求体 xmlhttp.setRequestHeader("CONTENT-TYPE","application/x-www-form-urlencoded"); xmlhttp.send("name="+input_value); } } </script> </head> <body> <input type="text" id="input_value"/> <input type="button" value="点击" id="btn"/> </body> </html>
package com.controller; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; @WebServlet(name = "AjaxServlet",urlPatterns = "/ajax") public class AjaxServlet extends HttpServlet { protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String name = request.getParameter("name"); System.out.println(name); response.getWriter().println(name); } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { this.doPost(request,response); } }
{ key1:value1, key2:value2, ... }
[ { key1:value1, key2:value2 }, { key3:value3, key4:value4 } ]
<dependency> <groupId>net.sf.json-lib</groupId> <artifactId>json-lib</artifactId> <version>2.4</version> <classifier>jdk15</classifier> </dependency> <dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-lang3</artifactId> <version>3.1</version> </dependency> <dependency> <groupId>commons-beanutils</groupId> <artifactId>commons-beanutils</artifactId> <version>1.8.3</version> </dependency> <dependency> <groupId>commons-logging</groupId> <artifactId>commons-logging</artifactId> <version>1.1.1</version> </dependency> <dependency> <groupId>commons-collections</groupId> <artifactId>commons-collections</artifactId> <version>3.2.1</version> </dependency> <dependency> <groupId>net.sf.ezmorph</groupId> <artifactId>ezmorph</artifactId> <version>1.0.6</version> </dependency>
package com.domain; import java.io.Serializable; import java.util.Date; public class Account implements Serializable { private Integer id; private String name; private Double money; public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Double getMoney() { return money; } public void setMoney(Double money) { this.money = money; } @Override public String toString() { return "Account{" + "id=" + id + ", name=‘" + name + ‘\‘‘ + ", money=" + money + ‘}‘; } }
@Test public void testArray(){ String[] str = {"aa","bb","cc"}; JSONArray jsonArray = JSONArray.fromObject(str); System.out.println(jsonArray); }
@Test public void testList(){ List<String> list = new ArrayList<>(); list.add("aaa"); list.add("bbb"); list.add("ccc"); JSONArray jsonArray = JSONArray.fromObject(list); System.out.println(jsonArray); }
@Test public void testObject(){ Account account = new Account(); account.setId(1); account.setName("哈哈"); account.setMoney(230.0); JSONObject jsonObject = JSONObject.fromObject(account); System.out.println(jsonObject); }
@Test public void testObjectWithoutName(){ JsonConfig config = new JsonConfig(); config.setExcludes(new String[]{"name"}); Account account = new Account(); account.setId(1); account.setName("哈哈"); account.setMoney(230.0); JSONObject jsonObject = JSONObject.fromObject(account,config); System.out.println(jsonObject); }
@Test public void testSet(){ Set<Account> set = new HashSet<>(); Account a = new Account(); a.setId(1); a.setName("aaa"); a.setMoney(100.0); Account b = new Account(); b.setId(2); b.setName("bbb"); b.setMoney(200.0); set.add(a); set.add(b); JSONArray jsonArray = JSONArray.fromObject(set); System.out.println(jsonArray); }
@Test public void testMap(){ Map<String,Object> map = new HashMap<>(); map.put("aaa","aaaaa"); map.put("bbb","bbbbb"); JSONObject object = JSONObject.fromObject(map); System.out.println(object); }
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>AJAX编码步骤</title> <script type="text/javascript"> window.onload =function () { //POST方式提交 //我们知道POST方式提交的参数是在请求体 document.getElementById("btn").onclick = function () { //1.创建XMLHttpRequest对象 var xmlhttp ; if(window.XMLHttpRequest){ xmlhttp = new XMLHttpRequest(); }else{ xmlhttp = new ActiveXObject("Microsoft.XMLHTTP"); } //注册状态监控回调函数 xmlhttp.onreadystatechange= function () { if(xmlhttp.readyState == 4){ if(xmlhttp.status == 200){ var text = xmlhttp.responseText; var json = eval("("+text+")"); var html = ""; for(var x =0;x<json.length;x++){ var account = json[x]; var account_id = account.id; var account_name = account.name; var account_money = account.money; html +="<tr><tb>"+account_id+"</tb>"; html +="<tb>"+account_name+"</tb>"; html +="<tb>"+account_money+"</tb></tr>"; } document.getElementById("tbody").innerHTML = html; } } } //建立与服务器的异步连接 xmlhttp.open("POST","/ajax"); //发出异步请求 发出请求体 xmlhttp.setRequestHeader("CONTENT-TYPE","application/x-www-form-urlencoded"); xmlhttp.send(null); } } </script> </head> <body> <input type="button" value="点击" id="btn"/> <table bgcolor="#faebd7" > <tbody id="tbody"></tbody> </table> </body> </html>
package com.controller; import com.domain.Account; import net.sf.json.JSONArray; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; import java.util.ArrayList; import java.util.List; @WebServlet(name = "AjaxServlet",urlPatterns = "/ajax") public class AjaxServlet extends HttpServlet { protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { List<Account> accounts = new ArrayList<>(); Account a = new Account(); a.setId(1); a.setName("aaa"); a.setMoney(100.0); Account b = new Account(); b.setId(2); b.setName("bbb"); b.setMoney(200.0); accounts.add(a); accounts.add(b); JSONArray array = JSONArray.fromObject(accounts); response.getWriter().println(array.toString()); } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { this.doPost(request,response); } }
标签:ack 方式 index www. logging ue4 count() alt https
原文地址:http://www.cnblogs.com/xuweiweiwoaini/p/7831285.html