package com.mweb.user.servlet;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.Random;
import javax.imageio.ImageIO;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* 生成图形验证码的Servlet
*/
public class ImageCodeMakerServlet extends HttpServlet
{
@Override
public void init(ServletConfig config) throws ServletException
{
// TODO Auto-generated method stub
super.init(config);
}
/**
*
*/
private static final long serialVersionUID = -8924942016876830055L;
public void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException
{
doPost(req, resp);
}
/**
* @see javax.servlet.http.HttpServlet#void
* (javax.servlet.http.HttpServletRequest,
* javax.servlet.http.HttpServletResponse)
*/
public void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException
{
// 首先设置页面不缓存
resp.setHeader("Pragma", "No-cache");
resp.setHeader("Cache-Control", "no-cache");
resp.setDateHeader("Expires", 0);
// 定义图片的宽度和高度
int width = 90, height = 40;
// 创建一个图像对象
BufferedImage image = new BufferedImage(width, height,
BufferedImage.TYPE_INT_RGB);
// 得到图像的环境对象
Graphics g = image.createGraphics();
Random random = new Random();
// 用随机颜色填充图像背景
g.setColor(getRandColor(180, 250));
g.fillRect(0, 0, width, height);
for (int i = 0; i < 5; i++)
{
g.setColor(getRandColor(50, 100));
int x = random.nextInt(width);
int y = random.nextInt(height);
g.drawOval(x, y, 4, 4);
}
// 设置字体,下面准备画随机数
g.setFont(new Font("", Font.PLAIN, 40));
// 随机数字符串
String sRand = "";
for (int i = 0; i < 4; i++)
{
// 生成四个数字字符
String rand = String.valueOf(random.nextInt(10));
sRand += rand;
// 生成随机颜色
g.setColor(new Color(20 + random.nextInt(80), 20 + random
.nextInt(100), 20 + random.nextInt(90)));
// 将随机数字画在图像上
g.drawString(rand, (17 + random.nextInt(3)) * i + 8, 34);
// 生成干扰线
for (int k = 0; k < 12; k++)
{
int x = random.nextInt(width);
int y = random.nextInt(height);
int xl = random.nextInt(9);
int yl = random.nextInt(9);
g.drawLine(x, y, x + xl, y + yl);
}
}
// 将生成的随机数字字符串写入Session
req.getSession().setAttribute("randcode", sRand);
// 使图像生效
g.dispose();
// 输出图像到页面
ImageIO.write(image, "JPEG", resp.getOutputStream());
}
/**
* 产生一个随机的颜色
*
* @param fc
* 颜色分量最小值
* @param bc
* 颜色分量最大值
* @return
*/
public Color getRandColor(int fc, int bc)
{
Random random = new Random();
if (fc > 255)
{
fc = 255;
}
if (bc > 255)
{
bc = 255;
}
int r = fc + random.nextInt(bc - fc);
int g = fc + random.nextInt(bc - fc);
int b = fc + random.nextInt(bc - fc);
return new Color(r, g, b);
}
}
|
wsdl2java:参数解释
wsdl2java 用于根据WSDL生成相应的服务端和客户端代码的生成工具。
命令行格式为:WSDL2Java [options] -uri <url or path> : A url or path to a WSDL
其中常用的options具体如下:
-o <path> : 指定生成代码的输出路径
-a : 生成异步模式的代码
-s : 生成同步模式的代码
-p <pkg> : 指定代码的package名称
-l <languange> : 使用的语言(Java/C) 默认是java
-t : 为代码生成测试用例
-ss : 生成服务端代码 默认不生成
-sd : 生成服务描述文件 services.xml,仅与-ss一同使用
-d <databinding> : 指定databingding,例如,adb,xmlbean,jibx,jaxme and jaxbri
-g : 生成服务端和客户端的代码
-pn <port_name> : 当WSDL中有多个port时,指定其中一个port
-sn <serv_name> : 选择WSDL中的一个service
-u : 展开data-binding的类
-r <path> : 为代码生成指定一个repository
-ssi : 为服务端实现代码生成接口类
-S : 为生成的源码指定存储路径
-R : 为生成的resources指定存储路径
--noBuildXML : 输出中不生成build.xml文件
--noWSDL : 在resources目录中不生成WSDL文件
--noMessageReceiver : 不生成MessageReceiver类
-scn : 用已有的代码代替skeletons
|
/**
* @param args
*/
public static void main(String[] args) {
// try {
// HelloStub stub = new HelloStub();
// GetUser user = new GetUser();
// user.setId("22");
// stub.getUser(user);
// } catch (AxisFault e) {
// // TODO Auto-generated catch block
// e.printStackTrace();
// } catch (RemoteException e) {
// // TODO Auto-generated catch block
// e.printStackTrace();
// }
String xml = "<?xml version='1.0' encoding='utf-8'?>"
+ "<soapenv:Envelope xmlns:soapenv="http://www.w3.org/2003/05/soap-envelope">"
+ "<soapenv:Body>"
+ "<ns:getUserResponse xmlns:ns="http://test.lvxh.com">"
+ " <ns:return xmlns:ax21="http://test.lvxh.com/xsd""
+ " xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="ax21:Result">"
+ " <ax21:code>0</ax21:code>"
+ " <ax21:list>hello</ax21:list>" + " </ns:return>"
+ "</ns:getUserResponse>" + "</soapenv:Body>"
+ "</soapenv:Envelope>";
Map<String, List<Node>> map = new HashMap<String, List<Node>>();
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
try {
DocumentBuilder db = dbf.newDocumentBuilder();
ByteArrayInputStream is = new ByteArrayInputStream(xml.getBytes());
Document document = db.parse(is);
NodeList node = document.getChildNodes();
Test test = new Test();
for (int i = 0; i < node.getLength(); i++) {
Node a = node.item(i);
if(a instanceof DeferredElementImpl)
{
test.pars(a, map);
}
}
} catch (ParserConfigurationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SAXException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//System.out.println(map.get("code").get(0).getTextContent());
}
public void pars(Node node, Map<String, List<Node>> map)
{
NodeList nodeList = node.getChildNodes();
if(node instanceof DeferredElementImpl)
{
String name = node.getNodeName().split(":")[1];
if (map.get(name) != null) {
map.get(name).add(node);
} else {
List<Node> l = new ArrayList<Node>();
l.add(node);
map.put(name, l);
}
}
if (nodeList == null || nodeList.getLength() == 0) {
return;
} else {
for (int i = 0; i < nodeList.getLength(); i++) {
Node n = nodeList.item(i);
if(n instanceof DeferredElementImpl)
{
pars(n, map);
}
}
}
}
|
发生http请求
URLConnection urlCon = null;
OutputStreamWriter out = null;
BufferedReader reader = null;
InputStreamReader ir = null;
InputStream input = null;
OutputStream raw = null;
OutputStream buf = null;
String xmlString = "";
try
{
urlCon = new URL(urlStr).openConnection();
urlCon.setRequestProperty("Content-Type", "text/xml; charset=UTF-8");
urlCon.setRequestProperty("Accept",
"application/x-www-form-urlencoded");
urlCon.setRequestProperty("version", "100");
urlCon.setRequestProperty("Keep-Alive", "300");
urlCon.setRequestProperty("Content-Type",
"application/x-www-form-urlencoded");
urlCon.setDoOutput(true);
urlCon.setDoInput(true);
urlCon.setReadTimeout(NUM_SIXTY_THOUSAND);
raw = urlCon.getOutputStream();
buf = new BufferedOutputStream(raw);
out = new OutputStreamWriter(buf, "UTF-8");
out.write(xmlMsg);
out.flush();
input = urlCon.getInputStream();
ir = new InputStreamReader(input, "UTF-8");
reader = new BufferedReader(ir);
String line = null;
StringBuffer xmlStr = new StringBuffer();
while ((line = reader.readLine()) != null)
{
xmlStr.append(line);
}
xmlString = xmlStr.toString().trim();
}
return xmlString;
|
// 1.单例分两种:
// 饿汉式 和 懒汉式
// 饿汉式:
package com.chess.test;
/**
*
* 饿汉模式的单例
* @author chess
*
*/
public final class SignleObjectHungry
{
private static SignleObjectHungry signleObjHun = new SignleObjectHungry();
private SignleObjectHungry()
{
}
public static SignleObjectHungry getInstance()
{
return signleObjHun;
}
}
// 懒汉式
package com.chess.test;
/**
* 懒汉式单例模式
* @author chess
*
*/
public final class SingleObjectLazy
{
private static SingleObjectLazy signleObjLazy = null;
private SingleObjectLazy()
{
}
public static SingleObjectLazy getInstance()
{
if(null == signleObjLazy)
{
signleObjLazy = new SingleObjectLazy();
}
return signleObjLazy;
}
}
// 由于单例存在线程安全问题,所以要解决的话
// 对于
// 饿汉式线程安全
package com.chess.test;
/**
* 饿汉式单例线程安全
* @author chess
*
*/
public final class SingleObjectHungryAndThreadSafe
{
private static SingleObjectHungryAndThreadSafe signleObj = new SingleObjectHungryAndThreadSafe();
private SingleObjectHungryAndThreadSafe()
{
}
/**
* 加同步锁方法实现线程安全
*
* 此方法存在 性能开销 大
*
* @return
*/
public synchronized static SingleObjectHungryAndThreadSafe getInstance()
{
return signleObj;
}
}
//懒汉式线程安全
package com.chess.test;
/**
* 懒汉式单例 和 线程安全
*
* @author chess
*
*/
public final class SingleObjectLazyAndThreadSafe
{
private static SingleObjectLazyAndThreadSafe singleObj = null;
private SingleObjectLazyAndThreadSafe()
{
}
// public synchronized static SingleObjectLazyAndThreadSafe getInstance()
// {
// if(null == singleObj)
// {
// singleObj = new SingleObjectLazyAndThreadSafe();
// }
//
// return singleObj;
//
// }
// 细化粒度,提高性能 提高线程并发度
//双重检查锁机制
public static SingleObjectLazyAndThreadSafe getInstance()
{
if (null == singleObj)
{
synchronized (SingleObjectHungryAndThreadSafe.class)
{
if (null == singleObj)
{
singleObj = new SingleObjectLazyAndThreadSafe();
}
}
}
return singleObj;
}
//至此 线程安全 已经感觉狠完美了。
//多线程中,如果 线程A 执行到第 30 行,此时对象为空,则进入初始化对象,
// 然而初始化对象需要时间,
// 此时 线程B 也执行到第 30 行 , 此时对象已经初始化未完成, 但是地址已经存在。
// 则 线程B 将获取一个未完成的 对象。
}
//完美的解决方案
package com.chess.test;
/**
* 单例模式线程安全的完美状态
* 新的设计模式
* 使用内部类 JLS会保证这个类的线程安全
* (完全使用 JAVA 虚拟机的机制进行同步保证,且 没有同步的关键字)
* @author chess
*
*/
public class SingleObjectThreadSafeNoSync
{
/**
* 以内部类实现懒加载(懒汉)
*
* 初始化这个内部类的时候 JLS 保证这个类的线程安全
* @author cWX181783
*
*/
private static class SingletonHolder
{
public final static SingleObjectThreadSafeNoSync singleObject =
new SingleObjectThreadSafeNoSync();
}
public static SingleObjectThreadSafeNoSync getInstance()
{
return SingletonHolder.singleObject;
}
}
|