package com.servlet;
import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.io.StringReader;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.input.SAXBuilder;
import com.bean.EbusBean;
import com.bean.Ebus_logBean;
import com.dao.JsonDateValueProcessor;
import com.entity.Test;
import com.entity.TestDate;
import com.service.TestDateService;
import com.service.TestService;
import com.test.test;
import net.sf.json.JSON;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import net.sf.json.JsonConfig;
import net.sf.json.xml.XMLSerializer;
public class TestServlet extends HttpServlet
{
TestService ts=new TestService();
TestDateService td=new TestDateService();
public TestServlet()
{
super();
}
@Override
public void destroy() {
super.destroy();
}
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException
{
doPost(req, resp);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException
{
req.setCharacterEncoding("UTF-8");
resp.setCharacterEncoding("UTF-8");
resp.setContentType("charset=utf-8");
resp.setHeader("text/html", "charset=utf-8");
String str=req.getParameter("type");
if(str.equals("test1"))
{
test1(req,resp);
}else if(str.equals("QueryList"))
{
QueryList(req,resp);
}else if(str.equals("QueryList2"))
{
QueryList2(req,resp);
}else if(str.equals("DateQuery"))
{
QueryDate(req,resp);
}else if(str.equals("testDate"))
{
testDate(req,resp);
}else if(str.equals("json"))
{
json(req,resp);
}else if(str.equals("toxml"))
{
toxml(req,resp);
}else if(str.equals("tostring"))
{
tostring(req,resp);
}else
{
resp.sendRedirect("error.jsp");
}
}
public void testDate(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException
{
String firstTime="2018-05-03 10:20:24";
String sceondTime="2018-05-03 11:35:30";
SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date firstDate=null;
Date secondDate=null;
Long days=0L;
try
{
firstDate=sdf.parse(firstTime);
secondDate=sdf.parse(sceondTime);
days=((firstDate.getTime()-secondDate.getTime())/(60*1000));
} catch (ParseException e)
{
e.printStackTrace();
}
System.out.println(days);
}
public void test1(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException
{
List<Test> lt=new ArrayList<Test>();
Test tt1=new Test("18872210499",270,"wuhan188","500","188","aaaa","INSTORE","0");
Test tt2=new Test("18872210538",170,"wuhan189","1500","388","ssss","INSTORE","0");
Test tt3=new Test("19468457231",370,"wuhan185","2500","588","cccc","INSTORE","0");
lt.add(tt1);
lt.add(tt2);
lt.add(tt3);
String tsjson=JSONArray.fromObject(lt).toString();
PrintWriter pw=resp.getWriter();
pw.print(tsjson);
}
public void QueryList(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException
{
List<Test> lt=ts.getInfo();
String tsjson=JSONArray.fromObject(lt).toString();
PrintWriter pw=resp.getWriter();
pw.print(tsjson);
}
public void QueryList2(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException
{
List<TestDate> ltd=td.getInfo();
String tsjson=JSONArray.fromObject(ltd).toString();
PrintWriter pw=resp.getWriter();
pw.print(tsjson);
}
public void QueryDate(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException
{
List<TestDate> ltd=td.getInfo();
JsonConfig config=new JsonConfig();
JsonDateValueProcessor jdvp=new JsonDateValueProcessor();
config.registerJsonValueProcessor(Date.class, jdvp);
JSONArray arry=new JSONArray();
String tsjson=JSONArray.fromObject(ltd,config).toString();
PrintWriter pw=resp.getWriter();
pw.print(tsjson);
}
public void json(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException
{
/*String str="{"+
"\"accessType\":\"bsacNB\","+
" \"formnum\":\"\","+
" \"interfaceID\":\"PTQryWbandInfoByTelNew\","+
" \"menuID\":\"\","+
" \"operatorID\":\"1301818\","+
" \"region\":\"270\","+
" \"reqSeq\":\"20170905112929\","+
" \"reqTime\":\"20170905112929\","+
" \"routeType\":\"0\","+
" \"token\":\"\","+
" \"uniContact\":\"\","+
" \"unitID\":\"\","+
" \"param\": "+
"{"+
" \"telnum\":\"13986415162\","+
" \"region\":\"711\","+
"\"qrytype\":\"2\","+
"\"certtype\":\"\" "+
"}"+
"}" ;
System.out.println(str);
String json=JSONArray.fromObject(str).toString();
PrintWriter pw=resp.getWriter();
pw.print(json);*/
//JSON respJson = readFromStream(new FileInputStream("test.xml"));
//System.out.println(respJson);
/*JSONObject jsonObject = JSONObject.fromObject(respJson);
String json=JSONArray.fromObject(jsonObject).toString();
PrintWriter pw=resp.getWriter();
pw.print(json);*/
}
public static JSON readFromStream(InputStream stream) throws Exception
{
StringBuffer xml = new StringBuffer();
BufferedReader in = new BufferedReader(new InputStreamReader(stream,"UTF-8"));
String line = null;
while ((line = in.readLine()) != null) {
xml.append(line);
}
XMLSerializer xmlSerializer = new XMLSerializer();
return xmlSerializer.read(xml.toString());
}
public void toxml(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException
{
EbusBean ebus=new EbusBean("bsacNB","245","PTQryWbandInfoByTelNew","1301818","270","20170905112929","20170905112929","0","ds","fgf","8868","12344563214","44");
Ebus_logBean els=new Ebus_logBean("PTQryWbandInfoByTelNew","aaaaa","bbbb",ebus);
try {
JAXBContext context = JAXBContext.newInstance(Ebus_logBean.class);
Marshaller marshaller = context.createMarshaller();
marshaller.marshal(els, System.out);
//marshaller.marshal(jaxbElement, writer);
PrintWriter pw=resp.getWriter();
marshaller.marshal(els, pw);
//pw.print(marshaller.marshal(els));
} catch (JAXBException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void tostring(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException
{
String xml="<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>" +
"<ebus_logBean>" +
"<interfaceID>PTQryWbandInfoByTelNew</interfaceID>" +
"<request_param>null</request_param>" +
"<response_param>null</response_param>" +
"<ebusBean>" +
"<accessType>bsacNB</accessType>" +
"<formnum>0</formnum>" +
"<interfaceID>PTQryWbandInfoByTelNew<interfaceID>" +
"<menuID>0</menuID>" +
"<operatorID>0</operatorID>" +
"<region>null</region>" +
"<reqSeq>null</reqSeq>" +
"<reqTime>null</reqTime>" +
"<routeType>1</routeType>" +
"<unitID>小红</unitID>" +
"<ebusBean>" +
"</ebus_logBean>";
/* try {
JAXBContext context = JAXBContext.newInstance(Ebus_logBean.class);
Unmarshaller unmarshaller = context.createUnmarshaller();
Ebus_logBean ebus = (Ebus_logBean)unmarshaller.unmarshal(new StringReader(xml));
PrintWriter pw=resp.getWriter();
//pw.print(ebus.getEbusBean());
pw.print(xml);
//System.out.println(ebus.getName());
//System.out.println(obj.getPerson().getAddress());
} catch (JAXBException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} */
String xml2="<ROOT>"+
"<Status>00</Status>"+
"<ErrorMsg></ErrorMsg>"+
"<Data>"+
"<Row>"+
"<PERSONID>35020500200610000000000701355116</PERSONID>"+
"<XM>吴聪楠</XM><SFZH>350624198908052530</SFZH>"+
"</Row>"+
"<Row>"+
"<PERSONID>35020500200610000000000701355117</PERSONID>"+
"<XM>吴聪楠2</XM><SFZH>350624198908052531</SFZH>"+
"</Row>"+
"</Data>"+
"</ROOT>";
String str=xmlToJSON(xml2,1);
//System.out.println(xml);
PrintWriter pw=resp.getWriter();
pw.print(str);
}
/**把XML转为JSON格式
* **/
@SuppressWarnings("rawtypes")
public static String xmlToJSON(String xml,int jsonType) {
JSONObject obj = new JSONObject();
try {
InputStream is = new ByteArrayInputStream(xml.getBytes("utf-8"));
SAXBuilder sb = new SAXBuilder();
Document doc = sb.build(is);
Element root = doc.getRootElement();
Map map=iterateElement(root);
obj.put(root.getName(),map);
return obj.toString();
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
@SuppressWarnings({ "unchecked", "rawtypes" })
private static Map iterateElement(Element root) {
List childrenList = root.getChildren();
Element element = null;
Map map = new HashMap();
List list = null;
for (int i = 0; i < childrenList.size(); i++) {
list = new ArrayList();
element = (Element) childrenList.get(i);
if(element.getChildren().size()>0){
if(root.getChildren(element.getName()).size()>1){
if (map.containsKey(element.getName())) {
list = (List) map.get(element.getName());
}
list.add(iterateElement(element));
map.put(element.getName(), list);
}else{
map.put(element.getName(), iterateElement(element));
}
}else {
if(root.getChildren(element.getName()).size()>1){
if (map.containsKey(element.getName())) {
list = (List) map.get(element.getName());
}
list.add(element.getTextTrim());
map.put(element.getName(), list);
}else{
map.put(element.getName(), element.getTextTrim());
}
}
}
return map;
}
}
import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.io.StringReader;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.input.SAXBuilder;
import com.bean.EbusBean;
import com.bean.Ebus_logBean;
import com.dao.JsonDateValueProcessor;
import com.entity.Test;
import com.entity.TestDate;
import com.service.TestDateService;
import com.service.TestService;
import com.test.test;
import net.sf.json.JSON;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import net.sf.json.JsonConfig;
import net.sf.json.xml.XMLSerializer;
public class TestServlet extends HttpServlet
{
TestService ts=new TestService();
TestDateService td=new TestDateService();
public TestServlet()
{
super();
}
@Override
public void destroy() {
super.destroy();
}
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException
{
doPost(req, resp);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException
{
req.setCharacterEncoding("UTF-8");
resp.setCharacterEncoding("UTF-8");
resp.setContentType("charset=utf-8");
resp.setHeader("text/html", "charset=utf-8");
String str=req.getParameter("type");
if(str.equals("test1"))
{
test1(req,resp);
}else if(str.equals("QueryList"))
{
QueryList(req,resp);
}else if(str.equals("QueryList2"))
{
QueryList2(req,resp);
}else if(str.equals("DateQuery"))
{
QueryDate(req,resp);
}else if(str.equals("testDate"))
{
testDate(req,resp);
}else if(str.equals("json"))
{
json(req,resp);
}else if(str.equals("toxml"))
{
toxml(req,resp);
}else if(str.equals("tostring"))
{
tostring(req,resp);
}else
{
resp.sendRedirect("error.jsp");
}
}
public void testDate(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException
{
String firstTime="2018-05-03 10:20:24";
String sceondTime="2018-05-03 11:35:30";
SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
Date firstDate=null;
Date secondDate=null;
Long days=0L;
try
{
firstDate=sdf.parse(firstTime);
secondDate=sdf.parse(sceondTime);
days=((firstDate.getTime()-secondDate.getTime())/(60*1000));
} catch (ParseException e)
{
e.printStackTrace();
}
System.out.println(days);
}
public void test1(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException
{
List<Test> lt=new ArrayList<Test>();
Test tt1=new Test("18872210499",270,"wuhan188","500","188","aaaa","INSTORE","0");
Test tt2=new Test("18872210538",170,"wuhan189","1500","388","ssss","INSTORE","0");
Test tt3=new Test("19468457231",370,"wuhan185","2500","588","cccc","INSTORE","0");
lt.add(tt1);
lt.add(tt2);
lt.add(tt3);
String tsjson=JSONArray.fromObject(lt).toString();
PrintWriter pw=resp.getWriter();
pw.print(tsjson);
}
public void QueryList(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException
{
List<Test> lt=ts.getInfo();
String tsjson=JSONArray.fromObject(lt).toString();
PrintWriter pw=resp.getWriter();
pw.print(tsjson);
}
public void QueryList2(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException
{
List<TestDate> ltd=td.getInfo();
String tsjson=JSONArray.fromObject(ltd).toString();
PrintWriter pw=resp.getWriter();
pw.print(tsjson);
}
public void QueryDate(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException
{
List<TestDate> ltd=td.getInfo();
JsonConfig config=new JsonConfig();
JsonDateValueProcessor jdvp=new JsonDateValueProcessor();
config.registerJsonValueProcessor(Date.class, jdvp);
JSONArray arry=new JSONArray();
String tsjson=JSONArray.fromObject(ltd,config).toString();
PrintWriter pw=resp.getWriter();
pw.print(tsjson);
}
public void json(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException
{
/*String str="{"+
"\"accessType\":\"bsacNB\","+
" \"formnum\":\"\","+
" \"interfaceID\":\"PTQryWbandInfoByTelNew\","+
" \"menuID\":\"\","+
" \"operatorID\":\"1301818\","+
" \"region\":\"270\","+
" \"reqSeq\":\"20170905112929\","+
" \"reqTime\":\"20170905112929\","+
" \"routeType\":\"0\","+
" \"token\":\"\","+
" \"uniContact\":\"\","+
" \"unitID\":\"\","+
" \"param\": "+
"{"+
" \"telnum\":\"13986415162\","+
" \"region\":\"711\","+
"\"qrytype\":\"2\","+
"\"certtype\":\"\" "+
"}"+
"}" ;
System.out.println(str);
String json=JSONArray.fromObject(str).toString();
PrintWriter pw=resp.getWriter();
pw.print(json);*/
//JSON respJson = readFromStream(new FileInputStream("test.xml"));
//System.out.println(respJson);
/*JSONObject jsonObject = JSONObject.fromObject(respJson);
String json=JSONArray.fromObject(jsonObject).toString();
PrintWriter pw=resp.getWriter();
pw.print(json);*/
}
public static JSON readFromStream(InputStream stream) throws Exception
{
StringBuffer xml = new StringBuffer();
BufferedReader in = new BufferedReader(new InputStreamReader(stream,"UTF-8"));
String line = null;
while ((line = in.readLine()) != null) {
xml.append(line);
}
XMLSerializer xmlSerializer = new XMLSerializer();
return xmlSerializer.read(xml.toString());
}
public void toxml(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException
{
EbusBean ebus=new EbusBean("bsacNB","245","PTQryWbandInfoByTelNew","1301818","270","20170905112929","20170905112929","0","ds","fgf","8868","12344563214","44");
Ebus_logBean els=new Ebus_logBean("PTQryWbandInfoByTelNew","aaaaa","bbbb",ebus);
try {
JAXBContext context = JAXBContext.newInstance(Ebus_logBean.class);
Marshaller marshaller = context.createMarshaller();
marshaller.marshal(els, System.out);
//marshaller.marshal(jaxbElement, writer);
PrintWriter pw=resp.getWriter();
marshaller.marshal(els, pw);
//pw.print(marshaller.marshal(els));
} catch (JAXBException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void tostring(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException
{
String xml="<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>" +
"<ebus_logBean>" +
"<interfaceID>PTQryWbandInfoByTelNew</interfaceID>" +
"<request_param>null</request_param>" +
"<response_param>null</response_param>" +
"<ebusBean>" +
"<accessType>bsacNB</accessType>" +
"<formnum>0</formnum>" +
"<interfaceID>PTQryWbandInfoByTelNew<interfaceID>" +
"<menuID>0</menuID>" +
"<operatorID>0</operatorID>" +
"<region>null</region>" +
"<reqSeq>null</reqSeq>" +
"<reqTime>null</reqTime>" +
"<routeType>1</routeType>" +
"<unitID>小红</unitID>" +
"<ebusBean>" +
"</ebus_logBean>";
/* try {
JAXBContext context = JAXBContext.newInstance(Ebus_logBean.class);
Unmarshaller unmarshaller = context.createUnmarshaller();
Ebus_logBean ebus = (Ebus_logBean)unmarshaller.unmarshal(new StringReader(xml));
PrintWriter pw=resp.getWriter();
//pw.print(ebus.getEbusBean());
pw.print(xml);
//System.out.println(ebus.getName());
//System.out.println(obj.getPerson().getAddress());
} catch (JAXBException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} */
String xml2="<ROOT>"+
"<Status>00</Status>"+
"<ErrorMsg></ErrorMsg>"+
"<Data>"+
"<Row>"+
"<PERSONID>35020500200610000000000701355116</PERSONID>"+
"<XM>吴聪楠</XM><SFZH>350624198908052530</SFZH>"+
"</Row>"+
"<Row>"+
"<PERSONID>35020500200610000000000701355117</PERSONID>"+
"<XM>吴聪楠2</XM><SFZH>350624198908052531</SFZH>"+
"</Row>"+
"</Data>"+
"</ROOT>";
String str=xmlToJSON(xml2,1);
//System.out.println(xml);
PrintWriter pw=resp.getWriter();
pw.print(str);
}
/**把XML转为JSON格式
* **/
@SuppressWarnings("rawtypes")
public static String xmlToJSON(String xml,int jsonType) {
JSONObject obj = new JSONObject();
try {
InputStream is = new ByteArrayInputStream(xml.getBytes("utf-8"));
SAXBuilder sb = new SAXBuilder();
Document doc = sb.build(is);
Element root = doc.getRootElement();
Map map=iterateElement(root);
obj.put(root.getName(),map);
return obj.toString();
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
@SuppressWarnings({ "unchecked", "rawtypes" })
private static Map iterateElement(Element root) {
List childrenList = root.getChildren();
Element element = null;
Map map = new HashMap();
List list = null;
for (int i = 0; i < childrenList.size(); i++) {
list = new ArrayList();
element = (Element) childrenList.get(i);
if(element.getChildren().size()>0){
if(root.getChildren(element.getName()).size()>1){
if (map.containsKey(element.getName())) {
list = (List) map.get(element.getName());
}
list.add(iterateElement(element));
map.put(element.getName(), list);
}else{
map.put(element.getName(), iterateElement(element));
}
}else {
if(root.getChildren(element.getName()).size()>1){
if (map.containsKey(element.getName())) {
list = (List) map.get(element.getName());
}
list.add(element.getTextTrim());
map.put(element.getName(), list);
}else{
map.put(element.getName(), element.getTextTrim());
}
}
}
return map;
}
}