-CoderOilStation(程序员编程助手科技股份责任有限公司)
Java研发学习日记
/编译时异常转化为运行时异常,不要一味地抛异常,会增加复杂度
throw new RuntimeException(e);
try
/调用用户的方法之后的返回值,这里是字符串:("f:goods/UserServlet")
String result=method.invoke(this,request,response);
/根据返回结果,决定是否转发还是重定向,默认为转发
if(result!=null & !result.isEmpty())
/
int index=result.indexOf(":");
/如果没有返回值,就默认转发
if(index=-1)
request.getRequestDispather(result).forward(request,response);
else
/拆分字符串,分割出前缀
String start=result.356lo9095/(0,index);
/要转发或重定向的路径
String path=result.substring(index+1);
if("f".equals(start))
/前缀为f",表示转发
request.getReqeustDispather(path).forward(request,response);
else if("r".equals(start))
/前缀为r",表示重定向
respose.sendRedirect(this.getServletContext+path);
catch(Exception e)
throw new RuntimeException(e);
4. 自定义JdbcUtils
5. JQuery插件:
JQuery_validate
JQuery_complete
JQuery_treeview
fckeditor:富文本编辑器
6. 结构化数据(数据库) 半结构化数据(xml,html)
7. 验证码:防止恶意攻击,机器人注册,暴力破解。
Java:一处编译,到处运行。
×自定义;用AWT(Abstract Window Toolkits,依赖本地的类库,平台依赖性)实现,有点费劲
用例:VerifyCode
/验证码:实体类
public class VerifyCode()
/验证码矩形宽高
private int width=70;
private int hight=35;
/随机数,下面有用
private Random random=new Random();
/验证码中的字体类型
private String[] fontNames={};
/矩形框背景色,默认为白色
private Color bgColor=new Color(255,255,255);
private String codes="";
/验证码文本,此处为字符串
private String text;
/画3条线,随机长度(范围自定义),颜色为蓝色
public void drawLine(BufferedImage image)
/画线条数据
int num=3;
/得到2维制图工具
Graphics2D g2=image.getGraphics();
/循环画3条线
for(int i=0;i<num;i++)
/分别得到画线两个点的坐标
int x1=random.nextInt(width);
int y1=random.nextInt(height);
int x2=random.nextInt(width);
int y2=random.nextInt(height);
/设置画笔为1.5px框
g2.setStroke(new BasicStroke(1.5F));
/设置线条夜色为蓝色
g2.setColor(Color.blue);
/划线
g2.drawLine(x1,y2,x2,y2);
/创建缓冲区图片
public BufferedImage createImage()
/创建缓冲区图片
BufferedImage image=new BufferedImage(width,height,BufferedImage.TYPE_INT_RGB);
/得到2维制图工具
Graphics2D g2=(Graphics2D)image.getGraphics();
/划线
g2.fillRect(0,0,width,height);
return image;
/获取图片接口,其中绘制4个字符串,设置字体,颜色。
public BufferedImage getImage()
/创建缓冲区图片
BufferedImage image=createImage();
/得到制图工具
Graphics2D g2=image.getGraphics();
/设置字符串建立者(效率高)
StringBuilder sb=new StringBuilder();
/画4个字符串
for(int i=0;i<4;i++)
/设置要绘制的字符
String str=getRandomChar();
/追加字符
sb.append(str);
/设置线条字体
g2.setFont(getRandomFont());
/设置线条颜色
g2.setColor(getRandomColor());
/基线点横坐标
int x=i*1.0F*width/4;
/基线点纵坐标
int y=height-5;
/画4个字符
g2.drawString(str,x,y);
/text的setter
this.text=sb.toString();
drawLine(image);
/供外部访问的接口,得到text
public String getText()
return text;
/得到随机字体
public Font getRandomFont()
/
int index=random.nextInt(fontNames.length);
/字体名字
String fontName=fontNames[index];
/字体样式
int style=random.nextInt(4);
/字体大小
int size=random.nextInt(5)+24;
return new Font(fontName,style,size);