java 操作cookie
----------------------------------------------------------------------------------------------------------------------------------------------------
写入cookie
<%
String cookieName="Sender";
Cookie cookie=new Cookie(cookieName, "Test_Content");
cookie.setMaxAge(10);
response.addCookie(cookie);
%>
读取cookie
<%
Cookie cookies[]=request.getCookies();
Cookie sCookie=null;
String svalue=null;
String sname=null;
for(int i=0;i{
sCookie=cookies[i];
svalue=sCookie.getValue();
sname=sCookie.getName();
%> ......
一、什么是IOC
IoC就是Inversion of Control,控制反转。在Java开发中,IoC意味着将你设计好的类交给系统去控制,而不是在你的类内部控制。这称为控制反转。
下面我们以几个例子来说明什么是IoC
假设我们要设计一个Girl和一个Boy类,其中Girl有kiss方法,即Girl想要Kiss一个Boy。那么,我们的问题是,Girl如何能够认识这个Boy?
在我们中国,常见的MM与GG的认识方式有以下几种
1 青梅竹马; 2 亲友介绍; 3 父母包办
那么哪一种才是最好呢?
青梅竹马:Girl从小就知道自己的Boy。
public class Girl {
void kiss(){
Boy boy = new Boy();
}
}
然而从开始就创建的Boy缺点就是无法在更换。并且要负责Boy的整个生命周期。如果我们的Girl想要换一个怎么办?(笔者严重不支持Girl经常更换Boy)
亲友介绍:由中间人负责提供Boy来见面
public class Girl {
&nbs ......
JAVA之IO流(超详细的Java.io包的介绍!)
一.Input和Output
1.stream代表的是任何有能力产出数据的数据源,或是任何有能力接收数据的接收源。
在Java的IO中,所有的stream(包括Input和Out stream)都包括两种类型:
1.1 以字节为导向的stream
以字节为导向的stream,表示以字节为单位从stream中读取或往stream中写入信息。以字节为导向的stream包括下面几种类型:
1) input stream:
1) ByteArrayInputStream:把内存中的一个缓冲区作为InputStream使用
2) StringBufferInputStream:把一个String对象作为InputStream
3) FileInputStream:把一个文件作为InputStream,实现对文件的读取操作
4) PipedInputStream:实现了pipe的概念,主要在线程中使用
5) SequenceInputStream:把多个InputStream合并为一个InputStream
2) Out stream
1) ByteArrayOutputStream:把信息存入内存中的一个缓冲区中
2) FileOutputStream:把信息存入文件中
3) PipedOutputStream:实现了pipe的概念,主要在线程中使用
4) SequenceOutputStream:把多个OutStream合并为一个OutStream
1.2 以Unicode字符为导向的stream
以Unicode字符为导向的stream,表示以Unicode字符为单位从s ......
package com.test;
import java.text.DateFormat;
import java.text.ParsePosition;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
public class TimeTest {
//用来全局控制 上一周,本周,下一周的周数变化
private int weeks = 0 ;
private int MaxDate; //一月最大天数
private int MaxYear; //一年最大天数
/**
* @param args
*/
public static void main(String[] args) {
TimeTest tt = new TimeTest();
System.out.println( "获取当天日期:" +tt.getNowTime( "yyyy-MM-dd" ));
System.out.println( "获取本周一日期:" +tt.getMondayOFWeek());
System.out.println( "获取本周日的日期~:" +tt.getCurrentWeekday());
System.out.println( "获取上周一日期:" +tt.getPreviousWeekday());
System.out.println( "获取 ......
public class Base64 {
//码表
static private char[] alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/="
.toCharArray();
//codes里存放的是值表
//可以这么理解根据码表中的值可以得到编码前的值
//如codes['A']=0,即为其索引值
static private byte[] codes = new byte[256];
static {
for (int i = 0; i < 256; i++)
codes[i] = -1;
for (int i = 'A'; i <= 'Z'; i++)
codes[i] = (byte) (i - 'A');
for (int i = 'a'; i <= 'z'; i++)
  ......
下面是用Java实现今天、昨天、前天的日期小例子,大家可以试试,有什么问题我们互相讨论。
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
public class TestStr {
public static void main(String[] args) {
getDates();
}
public static void getDates() {
int currYear,currMonth,currDate;
Calendar calendar=Calendar.getInstance();
currYear=calendar.get(Calendar.YEAR);
currMonth=calendar.get(Calendar.MONTH)+1;
currDate=calendar.get(Calendar.DATE);
System.out.println("今天:"+currYear+"-"+currMonth+"-"+currDate);
//判断昨天是不是上一年。
if (currMonth==1&&currDate==1) {//是一月一号
System.out.println("--------1111---------");
System.out.println("昨天是:"+(currYear-1)+"-"+12+"-"+31);
System.out.println("前天是:"+(currYear-1)+"-"+12+"-"+(31-1));
&n ......