锘??xml version="1.0" encoding="utf-8" standalone="yes"?> Class綾伙細 鎴戜滑涔熷彲浠ヤ嬌鐢ㄤ竴涓壒孌婄殑Java綾繪潵鑾峰緱榪欎簺淇℃伅銆傝繖涓被灝辨槸Class綾匯傚涓涓璞′嬌鐢╣etClass()鏂規硶鍙互鑾峰緱Class綾誨瀷鐨勪竴涓疄渚嬨備緥濡傦細 姝e涓涓狤mployee瀵硅薄鎻忚堪浜嗕竴涓壒瀹氱殑闆囧憳鐨勪俊鎭竴鏍鳳紝涓涓狢lass瀵硅薄鎻忚堪浜嗕竴涓壒瀹氱殑Class鐨勪俊鎭侰lass綾繪渶鏈夌敤鐨勬柟娉曞ぇ姒傛槸getName()錛屾鏂規硶榪斿洖浜嗗璞$殑綾誨悕銆?br />渚嬪錛屽鏋渆鏄竴涓狤mployee綾誨璞★紝璇彞錛?br />System.out.println(e.getClass().getName() + " " + e.getName()); 鎵撳嵃鍑猴細 榪樺彲浠ヤ嬌鐢ㄩ潤鎬佹柟娉昮orName()錛岄氳繃涓涓瓧絎︿覆鏉ヨ幏寰楃被瀵硅薄銆?br />String className = "java.util.Date"; 濡傛灉綾誨悕淇濆瓨鍦ㄤ竴涓瓧絎︿覆涓彲浠ヤ嬌鐢╢orName鏂規硶鏉ヨ幏寰桟lass瀵硅薄錛岃繖鍦╟lassName鏄竴涓被鎴栬呮帴鍙g殑鍚嶇О鏃訛紝浼氳搗浣滅敤銆傚惁鍒欙紝浼氭姏鍑哄紓甯搞?/p>
TIP
Class瀵硅薄鎻忚堪浜嗕竴涓被鍨嬶紝榪欎釜綾誨瀷涔熷彲鑳戒笉鏄竴涓被銆備緥濡傦紝int涓嶆槸涓涓被錛宨nt.class浠嶇劧鏄竴涓狢lass綾誨瀷鐨勫璞°?/p>
浣跨敤Class瀵硅薄鐨刵ewInstance()鏂規硶錛屽彲浠ヨ幏寰椾竴涓璞$殑瀹炰緥銆備緥濡傦細 Using Reflection to Analyze the Capabilities of Classes API static Class forName(String className) Object newInstance()
Object newInstance(Object[] args) Parameters:
void printStackTrace() 聽 浣跨敤鍙嶅皠鏉ュ垎鏋愮被 榪欏効鏄畝瑕佺殑浠嬬粛鍙嶅皠鏈哄埗鏈閲嶈鐨勯儴鍒嗭紝浣跨敤瀹冧滑鍙互鑰冩煡涓涓被鐨勭粨鏋勩?/p>
All you have to do is have the appropriate method in the Modifier class work on the integer that getModifiers returns. You can also use the Modifier.toString method to print the modifiers. java.lang.reflect鍖呬腑Field錛孧ethod鍜孋onstructor榪?涓被錛屽垎鍒敤鏉ユ弿榪頒竴涓被鐨凢ield錛孧ethod鍜孋onstructor銆傝繖3涓被閮芥湁涓涓悕涓篻etName鐨勬柟娉曪紝鐢ㄦ潵鑾峰緱欏圭洰鐨勫悕瀛椼侳ield綾繪湁涓猤etType()鏂規硶錛屽張榪斿洖涓涓狢lass瀵硅薄錛屾弿榪拌繖涓瓧孌電殑綾誨瀷銆侻ethod鍜孋onstructor綾婚兘鏈夎幏寰楀弬鏁扮被鍨嬬殑鏂規硶錛孧ethod綾誨緱鍒頒簡榪斿洖綾誨瀷銆?涓被閮芥湁榪斿洖綾誨瀷涓篒nteger鐨刧etModifiers鏂規硶錛岃幏寰椾慨楗幫紝渚嬪public鎴栬卻tatic絳夈傝繕鍙互浣跨敤java.lang.reflect鍖呬腑鐨凪odifier綾繪潵鍒嗘瀽getModifiers鐨処nteger綾誨瀷榪斿洖鍊箋備嬌鐢∕odifier綾葷殑isPublic錛宨sPrivate錛宨sFinal鏂規硶鏉ヨ幏鐭ヤ竴涓柟娉曟垨鑰呮瀯閫犲嚱鏁版槸鍚︽槸public, private, or final銆備綘鎵闇瑕佸仛鐨勬槸浣跨敤鍚堥傜殑鏂規硶瀵筭etModifiers榪斿洖鐨勬暣鍨嬪艱繘琛屾搷浣溿備篃鍙互浣跨敤Modifier.toString()鏂規硶鏉ユ墦鍗頒慨楗扮銆?/p>
渚嬪瓙5-5鏄劇ず浜嗗浣曟墦鍗板嚭涓涓被鐨勬墍鏈変俊鎭傝繖涓▼搴忔彁紺轟綘杈撳叆涓涓被鍚嶏紝鎵撳嵃鍑烘墍鏈夌殑鏁版嵁鎴愬憳錛屾柟娉曞拰鏋勯犲嚱鏁般備緥濡傦紝浣犺緭鍏ワ細 紼嬪簭鎵撳嵃鍑猴細 聽聽 public static final double POSITIVE_INFINITY; 榪欎釜紼嬪簭鍙互鍒嗘瀽浠諱綍java瑙i噴鍣ㄥ彲浠ュ姞杞界殑綾伙紝鑰屼笉浠呬粎鏄▼搴忕紪璇戞椂鐨勭被銆?br />Example 5-5. ReflectionTest.java
Field[] getFields() 1.1 Method[] getMethods() 1.1 Constructor[] getConstructors() 1.1
Class getDeclaringClass() Class[] getExceptionTypes() (in Constructor and Method classes) returns an array of Class objects that represent the types of the exceptions thrown by the method. int getModifiers() returns an integer that describes the modifiers of this constructor, method, or field. Use the methods in the Modifier class to analyze the return value. String getName() returns a string that is the name of the constructor, method, or field. Class[] getParameterTypes() (in Constructor and Method classes) returns an array of Class objects that represent the types of the parameters. Class getReturnType() (in Method classes) returns a Class object that represents the return type.
static String toString(int modifiers) static boolean isAbstract(int modifiers)
紼嬪簭榪愯鏃訛紝Java榪愯鏃剁郴緇熺淮鎶や簡涓浠芥墍鏈夊璞$殑榪愯鏃剁被鍨嬭瘑鍒備繚鎸佷簡姣忎釜瀵硅薄鐨勭被鐨勪俊鎭傝櫄鎷熸満鏍規嵁榪愯鏃剁被鍒俊鎭紝閫夋嫨姝g‘鐨勬柟娉曟潵鎵ц銆?br />For example, the statement
Employee e;
Class cl = e.getClass();
Employee Harry Hacker
Class cl = Class.forName(className);
褰撳惎鍔ㄦ椂錛屽寘鍚玬ain鏂規硶鐨勭被琚姞杞姐傚茍鍔犺澆瀹冩墍闇瑕佺殑鎵鏈夌殑綾伙紝榪欎簺綾誨張鍔犺澆瀹冧滑鐢ㄥ埌鐨勭被錛屼緷姝ょ被鎺ㄣ傝繖鏍蜂竴涓ぇ搴旂敤紼嬪簭鍙兘浼氳姳璐硅緝闀跨殑鏃墮棿錛屼嬌鐢ㄦ埛鎰熷埌涓嶅揩銆傚彲浠ラ噰鐢ㄤ笅闈㈢殑鏂規硶錛岀粰鐢ㄦ埛紼嬪簭鏇村揩鐨勫惎鍔ㄧ殑閿欒銆傜‘淇漨ain鏂規硶娌℃湁鏄懼紡鐨勫紩鐢ㄥ叾瀹冪被銆傜劧鍚庨氳繃璋冪敤Class.forName鏉ュ己鍒跺姞杞藉叾浠栫被銆?/p>
絎笁縐嶈幏寰楃被瀵硅薄鐨勬柟娉曪紝濡傛灉T鏄竴涓猨ava綾伙紝T.class榪斿洖鍖歸厤鐨勭被瀵硅薄銆備緥濡傦細
Class cl1 = Date.class; // if you import java.util.*;
Class cl2 = int.class;
Class cl3 = Double[].class;
String s = "java.util.Date";
Object m = Class.forName(s).newInstance();
newInstance浣跨敤榛樿鐨勬瀯閫犲嚱鏁幫紝濡傛灉娌℃湁榛樿鏋勯犲嚱鏁幫紝浼氭姏鍑哄紓甯搞?/p>
Here is a brief overview of the most important parts of the reflection mechanism for letting you examine the structure of a class.
java.lang.Class 1.0
returns the Class object representing the class with name className.
returns a new instance of this class.
java.lang.reflect.Constructor 1.1
constructs a new instance of the constructor's declaring class.
聽args
聽the parameters supplied to the constructor. See the section on reflection for more information on how to supply parameters.
java.lang.Throwable 1.0
prints the Throwable object and the stack trace to the standard error stream.
Here is a brief overview of the most important parts of the reflection mechanism for letting you examine the structure of a class.
Class綾葷殑getFields, getMethods, getConstructors鏂規硶鑾峰緱鍏叡fields錛宮ethods鍜宑onstructors鐨勬暟緇勩備篃鍖呭惈浜嗚秴綾葷殑鍏叡鎴愬憳銆俫etdeclaredFields錛実eTDeclaredMethods鍜実eTDeclaredConstructors鏂規硶鍙互鑾峰緱Class綾諱腑澹版槑鐨勬墍鏈夌殑fields錛屾柟娉曞拰鏋勯犲嚱鏁般傚寘鍚簡private鍜宲rotected鎴愬憳錛屼絾鏄笉鍖呭惈瓚呯被涓殑鎴愬憳銆?/p>
java.lang.Double
class java.lang.Double extends java.lang.Number
{
聽聽 public java.lang.Double(java.lang.String);
聽聽 public java.lang.Double(double);
聽聽 public int hashCode();
聽聽 public int compareTo(java.lang.Object);
聽聽 public int compareTo(java.lang.Double);
聽聽 public boolean equals(java.lang.Object);
聽聽 public java.lang.String toString();
聽聽 public static java.lang.String toString(double);
聽聽 public static java.lang.Double valueOf(java.lang.String);
聽聽 public static boolean isNaN(double);
聽聽 public boolean isNaN();
聽聽 public static boolean isInfinite(double);
聽聽 public boolean isInfinite();
聽聽 public byte byteValue();
聽聽 public short shortValue();
聽聽 public int intValue();
聽聽 public long longValue();
聽聽 public float floatValue();
聽聽 public double doubleValue();
聽聽 public static double parseDouble(java.lang.String);
聽聽 public static native long doubleToLongBits(double);
聽聽 public static native long doubleToRawLongBits(double);
聽聽 public static native double longBitsToDouble(long);
聽聽 public static final double NEGATIVE_INFINITY;
聽聽 public static final double NaN;
聽聽 public static final double MAX_VALUE;
聽聽 public static final double MIN_VALUE;
聽聽 public static final java.lang.Class TYPE;
聽聽 private double value;
聽聽 private static final long serialVersionUID;
}
import
聽java.lang.reflect.
*
;
public
聽
class
聽ReflectionTest聽{
聽聽聽聽
public
聽
static
聽
void
聽main(String[]聽args)聽{
聽聽聽聽聽聽聽聽
//
聽read聽class聽name聽from聽command-line聽args聽or聽user聽input
聽聽聽聽聽聽聽聽String聽name;
聽聽聽聽聽聽聽聽
if
聽(args.length聽
>
聽
0
)
聽聽聽聽聽聽聽聽聽聽聽聽name聽
=
聽args[
0
];
聽聽聽聽聽聽聽聽
else
聽{
聽聽聽聽聽聽聽聽聽聽聽聽Scanner聽in聽
=
聽
new
聽Scanner(System.in);
聽聽聽聽聽聽聽聽聽聽聽聽System.out.println(
"
Enter聽class聽name聽(e.g.聽java.util.Date):聽
"
);
聽聽聽聽聽聽聽聽聽聽聽聽name聽
=
聽in.next();
聽聽聽聽聽聽聽聽}
聽聽聽聽聽聽聽聽
try
聽{
聽聽聽聽聽聽聽聽聽聽聽聽
//
聽print聽class聽name聽and聽superclass聽name聽(if聽!=聽Object)
聽聽聽聽聽聽聽聽聽聽聽聽Class聽cl聽
=
聽Class.forName(name);
聽聽聽聽聽聽聽聽聽聽聽聽Class聽supercl聽
=
聽cl.getSuperclass();
聽聽聽聽聽聽聽聽聽聽聽聽System.out.print(
"
class聽
"
聽
+
聽name);
聽聽聽聽聽聽聽聽聽聽聽聽
if
聽(supercl聽
!=
聽
null
聽
&&
聽supercl聽
!=
聽Object.
class
)
聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽System.out.print(
"
聽extends聽
"
聽
+
聽supercl.getName());
聽聽聽聽聽聽聽聽聽聽聽聽System.out.print(
"
\n{\n
"
);
聽聽聽聽聽聽聽聽聽聽聽聽printConstructors(cl);
聽聽聽聽聽聽聽聽聽聽聽聽System.out.println();
聽聽聽聽聽聽聽聽聽聽聽聽printMethods(cl);
聽聽聽聽聽聽聽聽聽聽聽聽System.out.println();
聽聽聽聽聽聽聽聽聽聽聽聽printFields(cl);
聽聽聽聽聽聽聽聽聽聽聽聽System.out.println(
"
}
"
);
聽聽聽聽聽聽聽聽}聽
catch
聽(ClassNotFoundException聽e)聽{
聽聽聽聽聽聽聽聽聽聽聽聽e.printStackTrace();
聽聽聽聽聽聽聽聽}
聽聽聽聽聽聽聽聽System.exit(
0
);
聽聽聽聽}
聽聽聽聽
/**
聽聽聽聽聽*聽Prints聽all聽constructors聽of聽a聽class
聽聽聽聽聽*聽
聽聽聽聽聽*聽
@param
聽cl
聽聽聽聽聽*聽聽聽聽聽聽聽聽聽聽聽聽a聽class
聽聽聽聽聽
*/
聽聽聽聽
public
聽
static
聽
void
聽printConstructors(Class聽cl)
聽聽聽聽聽{
聽聽聽聽聽聽聽聽Constructor[]聽constructors聽
=
聽cl.getDeclaredConstructors();
聽
聽聽聽聽聽聽聽聽
for
聽(Constructor聽c聽:聽constructors)
聽聽聽聽聽聽聽聽{
聽聽聽聽聽聽聽聽聽聽聽String聽name聽
=
聽c.getName();
聽聽聽聽聽聽聽聽聽聽聽System.out.print(
"
聽聽聽
"
聽
+
聽Modifier.toString(c.getModifiers()));
聽聽聽聽聽聽聽聽聽聽聽System.out.print(
"
聽
"
聽
+
聽name聽
+
聽
"
(
"
);
聽
聽聽聽聽聽聽聽聽聽聽聽
//
聽print聽parameter聽types
聽聽聽聽聽聽聽聽聽聽聽Class[]聽paramTypes聽
=
聽c.getParameterTypes();
聽聽聽聽聽聽聽聽聽聽聽
for
聽(
int
聽j聽
=
聽
0
;聽j聽
<
聽paramTypes.length;聽j
++
)
聽聽聽聽聽聽聽聽聽聽聽{
聽聽聽聽聽聽聽聽聽聽聽聽聽聽
if
聽(j聽
>
聽
0
)聽System.out.print(
"
,聽
"
);
聽聽聽聽聽聽聽聽聽聽聽聽聽聽System.out.print(paramTypes[j].getName());
聽聽聽聽聽聽聽聽聽聽聽}
聽聽聽聽聽聽聽聽聽聽聽System.out.println(
"
);
"
);
聽聽聽聽聽聽聽聽}
聽聽聽聽聽}
聽聽聽聽
/**
聽聽聽聽聽*聽Prints聽all聽methods聽of聽a聽class
聽聽聽聽聽*聽
聽聽聽聽聽*聽
@param
聽cl
聽聽聽聽聽*聽聽聽聽聽聽聽聽聽聽聽聽a聽class
聽聽聽聽聽
*/
聽聽聽聽
public
聽
static
聽
void
聽printMethods(Class聽cl)
聽聽聽聽聽{
聽聽聽聽聽聽聽聽Method[]聽methods聽
=
聽cl.getDeclaredMethods();
聽
聽聽聽聽聽聽聽聽
for
聽(Method聽m聽:聽methods)
聽聽聽聽聽聽聽聽{
聽聽聽聽聽聽聽聽聽聽聽Class聽retType聽
=
聽m.getReturnType();
聽聽聽聽聽聽聽聽聽聽聽String聽name聽
=
聽m.getName();
聽
聽聽聽聽聽聽聽聽聽聽聽
//
聽print聽modifiers,聽return聽type聽and聽method聽name
聽聽聽聽聽聽聽聽聽聽聽System.out.print(
"
聽聽聽
"
聽
+
聽Modifier.toString(m.getModifiers()));
聽聽聽聽聽聽聽聽聽聽聽System.out.print(
"
聽
"
聽
+
聽retType.getName()聽
+
聽
"
聽
"
聽
+
聽name聽
+
聽
"
(
"
);
聽
聽聽聽聽聽聽聽聽聽聽聽
//
聽print聽parameter聽types
聽聽聽聽聽聽聽聽聽聽聽Class[]聽paramTypes聽
=
聽m.getParameterTypes();
聽聽聽聽聽聽聽聽聽聽聽
for
聽(
int
聽j聽
=
聽
0
;聽j聽
<
聽paramTypes.length;聽j
++
)
聽聽聽聽聽聽聽聽聽聽聽{
聽聽聽聽聽聽聽聽聽聽聽聽聽聽
if
聽(j聽
>
聽
0
)聽System.out.print(
"
,聽
"
);
聽聽聽聽聽聽聽聽聽聽聽聽聽聽System.out.print(paramTypes[j].getName());
聽聽聽聽聽聽聽聽聽聽聽}
聽聽聽聽聽聽聽聽聽聽聽System.out.println(
"
);
"
);
聽聽聽聽聽聽聽聽}
聽聽聽聽聽}
聽聽聽聽
/**
聽聽聽聽聽*聽Prints聽all聽fields聽of聽a聽class
聽聽聽聽聽*聽
聽聽聽聽聽*聽
@param
聽cl
聽聽聽聽聽*聽聽聽聽聽聽聽聽聽聽聽聽a聽class
聽聽聽聽聽
*/
聽聽聽聽
public
聽
static
聽
void
聽printFields(Class聽cl)
聽聽聽聽聽{
聽聽聽聽聽聽聽聽Field[]聽fields聽
=
聽cl.getDeclaredFields();
聽聽聽聽聽聽聽
for
聽(Field聽f聽:聽fields)
聽聽聽聽聽聽聽{
聽聽聽聽聽聽聽聽聽聽Class聽type聽
=
聽f.getType();
聽聽聽聽聽聽聽聽聽聽String聽name聽
=
聽f.getName();
聽聽聽聽聽聽聽聽聽聽System.out.print(
"
聽聽聽
"
聽
+
聽Modifier.toString(f.getModifiers()));
聽聽聽聽聽聽聽聽聽聽System.out.println(
"
聽
"
聽
+
聽type.getName()聽
+
聽
"
聽
"
聽
+
聽name聽
+
聽
"
;
"
);
聽聽聽聽聽聽聽}
聽聽聽聽}
}
API
java.lang.Class 1.0
Field[] getDeclaredFields() 1.1
The getFields method returns an array containing Field objects for the public fields of this class or its superclasses. The geTDeclaredField method returns an array of Field objects for all fields of this class. The methods return an array of length 0 if there are no such fields or if the Class object represents a primitive or array type.
Method[] getDeclaredMethods() 1.1
return an array containing Method objects: getMethods returns public methods and includes inherited methods; getdeclaredMethods returns all methods of this class or interface but does not include inherited methods.
Constructor[] getDeclaredConstructors() 1.1
return an array containing Constructor objects that give you all the public constructors (for getConstructors) or all constructors (for getdeclaredConstructors) of the class represented by this Class object.
java.lang.reflect.Field 1.1
java.lang.reflect.Method 1.1
java.lang.reflect.Constructor 1.1
returns the Class object for the class that defines this constructor, method, or field.
java.lang.reflect.Modifier 1.1
returns a string with the modifiers that correspond to the bits set in modifiers.
static boolean isFinal(int modifiers)
static boolean isInterface(int modifiers)
static boolean isNative(int modifiers)
static boolean isPrivate(int modifiers)
static boolean isProtected(int modifiers)
static boolean isPublic(int modifiers)
static boolean isStatic(int modifiers)
static boolean isStrict(int modifiers)
static boolean isSynchronized(int modifiers)
static boolean isVolatile(int modifiers)
These methods test the bit in the modifiers value that corresponds to the modifier in the method name.
浣跨敤Reflection鍦ㄨ繍琛屾椂鍒嗘瀽瀵硅薄
]]>
1
璺ㄦ祻瑙堝櫒甯冨眬鐨勯棶棰橈細
鐢變簬嫻忚鍣ㄤ箣闂寸殑寰皬宸窛錛屽湪甯冨眬澶嶆潅鐨勯〉闈㈡椂灝嗕細紕板埌楹葷儲
2
鏃ユ湡鎺т歡
濡備綍瀹炵幇璺ㄦ祻瑙堝櫒鐨勬棩鏈熸帶浠訛紝濡備綍璁懼畾緙虹渷鍊鹼紝濡備綍瀹炵幇澶ц寖鍥寸殑鏃ユ湡鐩存帴瀹氫綅錛熷浣曡嚜閫傚簲澶氬尯鍩熸牸寮忛棶棰橈紵
3
銆佸垎杈ㄧ巼
濡備綍鏀寔甯歌鍒嗚鯨鐜囷紝渚嬪
800*600
錛?/span>
1024*768
錛?/span>
1280*1024
錛熻〃鏍煎拰灞傚浣曡嚜閫傚簲錛?/span>
4
銆侀槻姝?/span>
SQL
娉ㄥ叆寮忔敾鍑?/span>
5
錛庨槻姝㈣剼鏈敾鍑匯?/span>
鏈夋椂鍊欓渶瑕佸湪欏甸潰鍏佽閮ㄥ垎
html
鎴?/span>
javascript
浠g爜
,
濡傛樉紺哄琛屾枃鏈?/span>
6
銆佺敤鎴烽潪鎺堟潈欏甸潰璁塊棶
濡備綍瀹炵幇姣忎釜欏甸潰鐨勫畨鍏ㄩ獙璇侊紝濡備綍闃叉鐢ㄦ埛閫氳繃鐩存帴鎷瘋礉鍏蜂綋欏甸潰鍦板潃絳夋柟寮忥紝璁塊棶緋葷粺錛?/span>
鍖?br />鎷病鏈夐摼鎺ョ殑鏌愪簺欏甸潰鍜?/span>jsp 欏甸潰銆?/span>濡備綍鎺у埗閮ㄥ垎鏉冮檺錛屾渶灝忔帶鍒跺崟鍏冩槸浠涔堬紵
7銆侀噸澶嶆彁浜ら棶棰?/span>
鍔熻兘鎿嶄綔瀹屾垚鍚庯紝榧犳爣鍙抽敭鐐瑰嚮鎵鍦ㄩ〉闈紝閫夋嫨寮瑰嚭鑿滃崟鐨勫埛鏂板姛鑳斤紝瀹規槗鍑虹幇閲嶅鎻愪氦闂銆?/span>
鍔熻兘鎿嶄綔瀹屾垚鍚庯紝閫氳繃嫻忚鍣ㄧ殑鍚庨閿繘琛岄噸澶嶆搷浣滐紝瀹規槗鍑虹幇閲嶅鎻愪氦闂銆?/span>
URL娌℃湁閲嶅畾鍚戯紝鍒鋒柊欏甸潰鍑虹幇閲嶅鎻愪氦闂銆?/span>
鏌愬姛鑳介敭鍙嶅簲鏃墮棿寤惰繜鏃訛紝鍦ㄧ煭鏃墮棿鍐呴噸澶嶇偣鍑昏鍔熻兘閿紝瀹規槗鍑虹幇閲嶅鎻愪氦闂錛?/span>
鏌愪簺鐢ㄦ埛涔犳儻鍙屽嚮鎸夐挳錛屾煇浜涚敤鎴烽敊璇湴鐐逛簡涓ゆ鎸夐挳錛屾煇浜涢紶鏍囧嚭鐜版晠闅滐紝瀵艱嚧鍗曞嚮鍙樻垚鍙屽嚮錛岀粨鏋滃彂鐢熼噸澶嶆彁浜ゃ?/span>
濡傛灉涓嶅姞澶勭悊錛岃繖浜涜姹傞兘浼氳鏈嶅姟鍣ㄥ鐞嗭紝浠庤屽鑷撮敊璇殑緇撴灉銆?/span>
8錛庡浣曢攣瀹氳祫婧愬璁板綍錛氳繖鏄竴涓佸ぇ闅鵑棶棰樹簡錛屼負姝よ繕鎼炰簡涓嶅皯涓滆タ鍑烘潵銆?/span>
9錛屽浣曢槻姝㈡祻瑙堝櫒璁頒綇鏁版嵁錛屾煇浜涙祻瑙堝櫒鑳藉涓烘瘡涓涓?/span>URL璁頒綇鐢ㄦ埛杈撳叆鐨勬暟鎹紝鍦ㄦ煇浜涙儏鍐典笅鏄嵄闄╃殑銆傛瘮濡傛垜淇敼浜嗘煇浜涙暟鎹紝浣嗙敱浜庢煇浜涘師鍥犳垜鎯沖埛鏂幫紝姝ゆ椂鏁版嵁宸茬粡琚叾浠栫敤鎴蜂慨鏀癸紝浣嗗湪嫻忚鍣ㄩ噷闈綘鐪嬪埌鐨勬槸浣犱慨鏀圭殑鏁版嵁錛堜綘鐢ㄥ埛鏂版寜閽篃涓嶈兘鐪嬪埌鏂版暟鎹級銆傚鏋滀綘鍐嶆淇濆瓨錛屼綘璁や負浣犳病鏈変慨鏀規暟鎹紝浣嗘暟鎹凡緇忚淇敼浜嗐傚綋欏甸潰鏄剼鏈姩鎬佺敓鎴愮殑鏃跺欙紝榪欎釜闂鏇翠弗閲嶃?/span>
10錛屽浣曢槻姝㈡垨渚︽祴瀹㈡埛淇敼嫻忚鍣ㄨ緗紝姣斿瀹㈡埛鍦ㄦ墦寮欏甸潰涔嬪悗鍏抽棴鑴氭湰鍔熻兘銆?/span>
11錛屽洖閫闂錛屽浜庡伐浣滄祦鐔熸倝鐨勪漢瀵硅繖涓棶棰樺緢娓呮錛屾煇涓搷浣滆妭鐐瑰浣曞洖鍒板惎鍔ㄧ偣錛屽浣曞洖鍒頒笂涓鑺傜偣錛屽浣曢伩鍏嶇‖緙栫爜錛?/span>
12錛屾牎楠岄棶棰橈紝濡備綍鍚屾椂瀹炵幇瀹㈡埛绔拰鏈嶅姟鍣ㄧ鏍¢獙錛屽鎴風璋冪敤鏈嶅姟鍣ㄧ鐨勬牎楠屽姛鑳姐?/span>
13錛屽浣曢槻姝㈡暟鎹吉閫狅紝濡備綍闃叉瀹㈡埛浣跨敤鐗規畩宸ュ叿鍜屾妧鏈吉閫犳暟鎹彁浜ゅ埌鏈嶅姟鍣ㄣ?/span>
14錛?/span>濡備綍榪借釜瀹氫綅閿欒錛屽浣曞鐞嗗紓甯革紵
15錛屽浣曞湪涓や釜鎿嶄綔涔嬮棿浼犻掑ぇ閲忔暟鎹紝灝ゅ叾鏄潪瀛樺偍鏁版嵁銆?/span>
16錛?/span>甯﹀畨鍏ㄦ帶鍒剁殑鏂偣緇紶鐨勪笂杞?/span>鍜屼笅杞?/span>
17錛屽垎甯冨紡鐜鐨勭壒孌婂鐞嗭紝
18錛屽浣曟彁楂樺紑鍙戞晥鐜囷紵
銆銆
鈥?/span>
浠ヤ笅鏄簲鐢ㄥ疄渚?/span>
銆銆錛?/span>
%=CHK(Username)%
錛?/span>
銆銆
Username=CHK(replace(request(鈥渦sername鈥?,鈥濃欌?鈥濃?)
銆銆浣跨敤
Include
鎶婂嚱鏁板啓鍦ㄥ叕鏈夐〉闈笂錛岃繖鏍鋒晥鐜囨槸鏈濂界殑銆?/span>
銆銆紼嬪簭浣擄紙
1
錛?/span>
銆銆鍙﹀錛屽煎緱鎴戜滑娉ㄦ剰鐨勬槸錛屽緢澶氱珯鐐瑰湪鐢ㄦ埛娉ㄥ唽錛屾垨鑰呮槸鐢ㄦ埛璧勬枡淇敼鐨勯〉闈笂涔熺己灝戣剼鏈殑榪囨護錛屾垨鑰呮槸鍙湪鍏朵腑涔嬩竴榪涜榪囨護錛屾敞鍐岃繘鍏ュ悗淇敼璧勬枡浠嶇劧鍙互榪涜鑴氭湰鏀誨嚮銆傚鐢ㄦ埛鎻愪氦鐨勬暟鎹繘琛屾嫻嬪拰榪囨護錛岀▼搴忎綋錛?/span>
2
錛?/span>
濡備笅錛?/span>
銆銆
鈥?/span>
浠ヤ笅鏄繃婊ゅ嚱鏁?/span>
銆銆
If Instr(request("username"),"=")
錛?/span>
0 or
銆銆
Instr(request("username"),"%")
錛?/span>
0 or
銆銆
Instr(request("username"),chr(32))
錛?/span>
0 or
銆銆
Instr(request("username"),"?")
錛?/span>
0 or
銆銆
Instr(request("username"),"&")
錛?/span>
0 or
銆銆
Instr(request("username"),";")
錛?/span>
0 or
銆銆
Instr(request("username"),",")
錛?/span>
0 or
銆銆
Instr(request("username"),"'")
錛?/span>
0 or
銆銆
Instr(request("username"),"?")
錛?/span>
0 or
銆銆
Instr(request("username"),chr(34))
錛?/span>
0 or
銆銆
Instr(request("username"),chr(9))
錛?/span>
0 or
銆銆
Instr(request("username"),"顥?)
錛?/span>
0 or
銆銆
Instr(request("username"),"$")
錛?/span>
0 or
銆銆
Instr(request("username"),"
錛?/span>
")
錛?/span>
0 or
銆銆
Instr(request("username"),"
錛?/span>
")
錛?/span>
0 or
銆銆
Instr(request("username"),"""")
錛?/span>
0 then
銆銆
response.write "
鏈嬪弸錛屼綘鐨勬彁浜ょ敤鎴峰悕鍚湁闈炴硶瀛楃錛岃鏇存敼錛岃阿璋㈠悎浣?/span>
錛?/span>
a href='****:window.history.go(-1);'
錛炶繑鍥烇紲
/a
錛?/span>
"
銆銆
response.end
銆銆
end if
銆銆紼嬪簭浣擄紙
2
錛?/span>
銆銆涓轟簡鎻愪緵宸ヤ綔鏁堢巼鎴戜滑鍐嶅皢榪囨護鍐呭紼嬪簭鍖栵紝榪欐牱瀵瑰涓弬鏁扮殑榪囨護鏁堢巼灝嗘湁寰堝ぇ紼嬪害涓婄殑鎻愰珮錛氬
銆銆紼嬪簭浣擄紙
3
錛?/span>
銆銆
鈥?/span>
浠ヤ笅涓虹▼搴忎富浣?/span>
銆銆
dim Bword(18)
銆銆
Bword(0)="?"
銆銆
Bword(1)=";"
銆銆
Bword(2)="
錛?/span>
"
銆銆
Bword(3)="
錛?/span>
"
銆銆
Bword(4)="-"
銆銆
Bword(5)="鈥?
銆銆
Bword(6)="鈥濃?
銆銆
Bword(7)="&"
銆銆
Bword(8)="%"
銆銆
Bword(9)="$"
銆銆
Bword(10)="'"
銆銆
Bword(11)=":"
銆銆
Bword(12)="
銆
"
銆銆
Bword(13)="("
銆銆
Bword(14)=")"
銆銆
Bword(15)="--"
銆銆
Bword(16)=" chr(9)"
銆銆
Bword(17)=" chr(34)"
銆銆
Bword(18)=" chr(32)"
銆銆
errc=false
銆銆
鈥?/span>
浠ヤ笅鏄簲鐢ㄥ疄渚嬮儴鍒?/span>
銆銆
for i= 0 to ubound(Bword)
銆銆
if instr(FQYs,Bword(i))
錛滐紴
0 then
銆銆
errc=true
銆銆
end if
銆銆
next
銆銆
if errc then
銆銆
response.write "
錛?/span>
script language=""****""
錛?/span>
"
銆銆
response.write "parent.alert('
寰堟姳姝?/span>
!
鎮ㄧ殑鎿嶄綔榪濇硶浜?/span>
);"
銆銆
response.write "history,back();"
銆銆
response.write "
錛?/span>
/script
錛?/span>
"
銆銆
response.end
銆銆
end if
銆銆紼嬪簭浣擄紙
3
錛?/span>
銆銆鏈変簡涓婇潰鐨勮繃婊ゅ嚱鏁版偍鍙互鍦ㄤ換浣曢渶瑕佽繃婊ょ殑鍦版柟搴旂敤榪囨護鍑芥暟鐩存帴浣跨敤灝卞彲浠ヤ簡銆傝繖灝變嬌鎴戜滑鐨勪慨澶嶅伐浣滃ぇ澶х殑綆鍖栦簡銆?/span>
銆銆鍙﹀錛屾垜鎯沖湪榪欓噷鍐嶆澶氭彁閱掍竴涓嬶紝涓浜涚珯鐐圭殑
UBB
鍦ㄨ繘琛屽皬鐨勮〃鎯呭浘鏍囪漿鍖栨椂涔熶細鍑虹幇榪囨護闂錛岀敱浜庡緢闅愯斀鎵浠ヤ笉瀹規槗鍙戠幇錛?/span>
銆銆濡傦細
銆銆鎴戜滑鏍囩鍐呯殑鏂囧瓧榪涜淇敼錛?/span>
銆銆涓嶇煡閬撳悇浣嶇湅鎳傛病錛屽墠涓涓崟寮曞彿鐢ㄦ潵涓拰紼嬪簭鎻愪緵鐨勫乏寮曞彿錛岀浜屼釜鍗曞紩鍙風敤鏉ヤ腑鍜岄棴鍚堢殑鍙沖紩鍙鳳紝榪欐牱紼嬪簭杈撳嚭灝變負錛?/span>
銆銆錛?/span>
img src=鈥檌mg/0001.gif鈥?onerror=****:alert(); alt=鈥欌?/span>
錛?/span>
銆銆濡傛灉鍥劇墖涓嶅瓨鍦紝閭d箞灝嗘縺媧?/span>
onerror
鏍囩鎵ц鑴氭湰紼嬪簭銆傚浜庡凡緇忚繃婊や簡鍗曞紩鍙風殑绔欑偣鍦ㄨ繖閲岀敤鍙屽紩鍙蜂竴鏍峰彲浠ュ畬鎴愩傚浜庤繃婊や簡
****
瀛楁鐨勶紝鍙敤
alert()
涔熷畬鍏ㄥ彲浠ャ傛墍浠ヨ瑕佽繃婊ゅ氨瑕佽繃婊ゅ畬鍏紝鍒粰鏀誨嚮鑰呯暀涓嬩竴涓濇満浼氥?/span>
絎竴閮ㄥ垎:聽 jsp聽 ---> java/servlet
瀵逛簬嫻佽鐨?span lang="EN-US">Tomcat鏉ヨ錛屾湁浠ヤ笅涓夌瑙e喅鏂規硶錛?/p>
銆銆1) 鏇存敼 D:\Tomcat\conf\server.xml錛屾寚瀹氭祻瑙堝櫒鐨勭紪鐮佹牸寮忎負鈥?/span>綆浣撲腑鏂?span lang="EN-US">鈥?/span>錛?/p>
銆銆鏂規硶鏄壘鍒?span lang="EN-US"> server.xml
聽聽聽 <Connector port="8080" maxThreads="150" minSpareThreads="25" maxSpareThreads="75"
聽聽聽 enableLookups="false" redirectPort="8443" acceptCount="100"
聽聽聽 connectionTimeout="20000" disableUploadTimeout="true" URIEncoding='GBK' />
銆銆鏍囪錛岀矖浣撳瓧鏄垜娣誨姞鐨勩?/p>
銆銆鍙互榪欐牱楠岃瘉浣犵殑鏇存敼鏄惁鎴愬姛錛氬湪鏇存敼鍓嶏紝鍦ㄤ綘鍑虹幇涔辯爜鐨勯〉闈㈢殑IE嫻忚鍣紝鐐瑰嚮鑿滃崟鈥?/span>鏌ョ湅锝滅紪鐮?span lang="EN-US">鈥?/span>錛屼細鍙戠幇鈥?/span>瑗挎(ISO)鈥?/span>澶勪簬閫変腑鐘舵併傝屾洿鏀瑰悗錛岀偣鍑昏彍鍗?span lang="EN-US">鈥?/span>鏌ョ湅锝滅紪鐮?span lang="EN-US">鈥?/span>錛屼細鍙戠幇鈥?/span>綆浣撲腑鏂?span lang="EN-US">(GB2312)鈥?/span>澶勪簬閫変腑鐘舵併傛敞鎰忎綘鐨凾omcat鐨刢ommon涓嬭鏈?endorsed"鍜?i18n"鏂囦歡澶癸紝鍏朵腑鍖呭惈緙栫爜鎵闇鐨刲ib
聽聽聽
銆銆2)鏇磋 Java 紼嬪簭錛?br />聽public class ThreeParams extends HttpServlet {
聽 public void doGet(HttpServletRequest request, HttpServletResponse response)
聽聽 throws ServletException, IOException {
聽聽聽聽聽 response.setContentType("text/html; charset=GBK");
聽聽聽聽聽 ...
聽 }聽
}
銆銆綺椾綋瀛楁槸蹇呴渶瑕佹湁鐨勶紝瀹冪殑浣滅敤鏄嫻忚鍣ㄦ妸Unicode瀛楃杞崲涓?span lang="EN-US">GBK瀛楃銆傝繖鏍烽〉闈㈢殑鍐呭鍜屾祻瑙堝櫒鐨勬樉紺烘ā寮忛兘璁炬垚浜?span lang="EN-US">GBK錛屽氨涓嶄細涔辯爜浜嗐?/p>
聽聽
絎簩閮ㄥ垎聽聽聽 java/servlet聽聽 ---->聽聽 mysql
mysql鍜孒ibernate涓殑涓枃闂瑙e喅鏂規硶
鍙︿竴縐嶈В鍐蟲柟娉?mysql-4.1.11).浣庣増鏈殑mysql浼間箮娌″嚭鐜拌繃涓枃闂(mysql-4.0.17). :)
1.娣誨姞涓涓繃婊ゅ櫒,灝嗗瓧絎﹂泦緙栫爜璁句負GBK.
淇敼web.xml:
聽<filter>
聽聽<filter-name>SetCharacterEncoding</filter-name>
聽聽<filter-class>hxz.filter.SetEncodingFilter</filter-class>
聽聽<init-param>
聽聽聽<param-name>encoding</param-name>
聽聽聽<param-value>GBK</param-value>
聽聽</init-param>
聽</filter>
聽
聽<filter-mapping>
聽聽<filter-name>SetCharacterEncoding</filter-name>
聽聽<url-pattern>/*</url-pattern>
聽</filter-mapping>
鏂板緩涓涓繃婊ゅ櫒:
package hxz.filter;
import java.io.IOException;
import javax.servlet.*;
public class SetEncodingFilter implements Filter {
聽
聽// default character encoding
聽String defaultEncoding = "GBK";聽
聽
聽public void init(FilterConfig config) throws ServletException {
聽聽String encoding = config.getInitParameter("encoding");
聽聽if (encoding != null) {
聽聽聽defaultEncoding = encoding;
聽聽}
聽}
聽public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
聽聽request.setCharacterEncoding(defaultEncoding);
聽聽chain.doFilter(request, response);
聽}
聽public void destroy() {
聽聽defaultEncoding = null;
聽}
}
2.淇敼hibernate閰嶇疆鏂囦歡:
<property name="url">
聽 <value>jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=GBK</value>
</property>
娉ㄦ剰鍚庨潰鐨?useUnicode=true&characterEncoding=GBK, 鍦▁ml涓?amp;瑕佹敼涓?amp;
3.鍦ㄦ柊鐗堟湰涓璵ysql寤鴻〃鏃?浣犲彲浠ラ夋嫨涓縐嶅瓧絎﹂泦,灝嗗畠璁句負GBK.
jsp鎴杝ervlet涓妸瀛楃闆嗘敼涓篏BK.
聽