1. redirect page
1, response. sendredirect ("url ");
2, response. setstatus (httpservletresponse. SC _moved_permanently );
Response. setheader ("location", newlocation );
2. HTML encoder and URL Encoder
1. Custom HTML encoder. Principle: ''no output, '&'-" & amp; ", '<'-" & lt ;", '>'-"gt;", '"'-" & quot ;"
2. urlencoder is defined in the java.net package.
Prototype: public static string encode (string S)
Example: urlencoder. encode ("http://wwww.aaa.com/sss.jsp? Name = kiddie ")
3. Read and Write files in JSP
1. Use fileoutputstream to initialize printwriter, and then use print or println to write the file.
Printwriter PW = new printwriter (New fileoutputstream ("file1.txt "));
PW. println ("Hello world! ");
PW. Close (); // if an error occurs, thow ioexception
Use filewriter to initialize printwriter, and then use print or println to write the file.
File F = new file ("file1.txt ");
Printwriter PW = new printwriter (New filewriter (f ));
PW. Print ("Hello world! /N ");
PW. Close ();
2. Use inputstreamreader or filereader to initialize bufferedreader and read the file using Readline ().
Bufferedreader BR = new bufferedreader (New filereader ("file1.txt "));
String RT = Br. Readline (); // The end is null.
BR. Close ();
3. Use filewriter to initialize printwriter, and then use the pint or println method to add files.
Printwriter PW = new printwriter (New filewriter ("file1.txt"), true );
4. Import java. Io .*;
File F = new file (request. getrealpath (""), "file1.txt ");
Boolean F. exists ();
F. Delete (); F. createnewfile ();
File d = new file (request. getrealpath (""));
Boolean D. exists ();
D. Delete (); D. mkdir ();
Request. getrealpath ("url"); // The virtual directory is mapped to the actual directory.
Request. getrealpath ("./"); // directory of the webpage
Request. getrealpath (".../"); // The upper directory of the directory where the webpage is located
File F = new file ("path", "file1.txt ");
F. getname ();
F. isfile ();
F. isdirectory ();
F. Canread ();
F. canwrite ();
F. ishidden ();
F. lastmodified;
F. createnewfile ();
F. Length ();
File d = new file ("path ");
File list [] = D. listfiles (); // list is a file Array
For (INT I = 0; I <list. length; I ++) Out. println (list [I]. getname ());
Filereader Fr = new filereader ("path" + "// file1.txt ");
If (Fr. Read () =-1) // empty file
Fr. Close ();
Fr. Read (int I) // read I characters,-1 if no data exists
File: // you can use bufferedreader to read a row of data at a time.
Fr. Skip (int I); // skipped I characters
When referencing functions such as parseint, the error is numberformatexception.
Random number obtained by random,
Random RD = new random (new date (). gettime ());
Int P = math. Abs (RD. nextint () % s; // The value ranges from 0
Iv. url reorganization and form hiding domain cookies
1. These are part of the sessions technology used to make up for the stateless features of HTTP.
2. url reorganization refers to the message "? Param1 = value1 & param2 = value2 &... & paramn = valuen"
If the server has already marked the session on the hyperlink, the client will include this flag when sending the hyperlink.
3. <input type = hidden name = "key1" value = "value1"/> in form can also be used like URL reorganization.
4. Cookie object
Cookie c = new cookie ("key", "value ");
Response. addcookie (C );
Cookie [] C = request. getcookies ();
C. setmaxage (int K); // K in seconds
Generally, a browser can store 20 cookies.
5. Session Object
1. The session object can not only store string data, but also complex objects.
2, session. putvalue ("key1", object1 );
Object o = session. getvalue ("key1 ");
6. Handling Chinese problems in JSP
1, ASCII code
8-bit storage, 0 ~ 31 and 127 are control characters, 32 ~ 126 is a visible character.
2, gb2312
Two 8bit representation. Previous 127 ~ 255 to distinguish between ASCII codes.
3, Unicode
Dozens of types of Text codes in the world can be unified under the same encoding mechanism. It is stored in 16 bits. 0x0000 ~ 0 xFFFF
4, ISO-8859-1 or known as Latin-1, 8859-1. The value range in Unicode is 0 ~ 255, the low part is ASCII extended to 0 ~ 255, and then add 0x00 in the high position to form a 16-bit (not familiar here ).
5. The bytes and Unicode Java kernels are Unicode and the class files are also. However, the stream uses the byte method. Char is Unicode, and byte is byte. Conversion Function: Sun. IO:
Public static bytetocharconverter getdefault (); // gets the encoding method used by the system.
Public static bytetocharconverter getconverter (string encoding );
Bytetocharconverter c = new bytetocharconverter (["encoding"]);
Byte [] S = C. convertall (char [] D );
It can also be char [] d = C. converterall (byte [] S );
6. Some functions:
Integer. tohexstring (int I );
String s; S. getbytes ();
String (byte []); string (byte [], encoding); // Constructors
File: // write an article about unicode encoding
7. Obtain JVM attribute values
Properties props = system. getproperties ();
Enumeration Enum = props. propertynames (); file: // key Enumeration
Key = (string) enum. nextelement ();
String S = (string) props. getproperty (key );
8. jsp error handling
1. All exception objects that can be throw and catch are inherited from throwable. Exception should be caught. The error object also inherits from throwable, but should not catch, but end the program.
2. The exception of the catch sequence should be from low to advanced.
3. Translation Errors and client-side request errors. If the JSP source code or import path is incorrect, a conversion error (500) is generated when the servlet class document is generated ). When the servlet class is executed, the client request error will be caught.
4. When an error is generated, JSP: forward can be used for control, but errorpage is better used for handling. You can also throw new exception ("errmsg ").