JSP Practical article

Source: Internet
Author: User
Tags object control characters end error handling functions string throwable domain
Js

One, redirect page

1,response.sendredirect ("url");
2,response.setstatus (httpservletresponse.sc_moved_permanently);
Response.setheader ("Location", newlocation);

Second, HTML encoder and URL encoder

1,html Encoder Custom, principle: ' No output, ' & '-' & ', ' < '-' < ', ' > '-' gt; ', ' ' ' '
2,urlencoder is defined in the java.net package.
Prototype: public static string Encode (string s)
For example: Urlencoder.encode ("Http://wwww.aaa.com/sss.jsp?name= imp")

Third, read and write files in JSP

1, initialize the PrintWriter with FileOutputStream, then write the file in print or Println method
PrintWriter pw=new PrintWriter (New FileOutputStream ("file1.txt"));
Pw.println ("Hello world!");
Pw.close ()//If there is a mistake thow IOException

Initialize the PrintWriter with FileWriter, and then write the file using the print or Println method
File F=new file ("file1.txt");
PrintWriter pw=new PrintWriter (new FileWriter (f));
Pw.print ("Hello world!\n");
Pw.close ();
2, initialize the BufferedReader with InputStreamReader or FileReader, and then read the file with the ReadLine () method
BufferedReader br=new BufferedReader (New FileReader ("file1.txt"));
String rt=br.readline ();//END NULL
Br.close ();
3, initialize PrintWriter with FileWriter, and add files using pint or println method
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");//virtual directory mapped to actual directory
Request.getrealpath ("./");//The directory where the page is located
Request.getrealpath (".. /");//The previous level of the directory in which the page 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 there is no more data
You can read one row of data at a time with BufferedReader
Fr.skip (int i);//Skip I characters


When referencing functions such as parseint, errors are numberformatexception and so on.
Random get random numbers,
Random rd=new Random ((New Date ()). GetTime ());
int P=math.abs (Rd.nextint ())%s;//s to range 0

Four, URL reorganization, form hidden domain cookie

1, these are part of the technology (sessions technology) that makes up for the stateless features of the HTTP protocol
2,url reorganization is the information sent to the server using the Get method "? Param1=value1&param2=value2&...&paramn=valuen"
If the server has already made a session mark on the hyperlink, the client will include this tag when it is sent through the hyperlink.
The <input type=hidden name= "Key1" value= "value1" in 3,form can also be used as a URL reorganization.
4,cookie objects
Cookie C=new Cookie ("Key", "value");
Response.addcookie (c);

Cookie[] C=request.getcookies ();
C.setmaxage (int k);//k in seconds
A general browser can put 20 cookies

Five, Session object

The 1,session object can not only put string data, but also can put complex objects.
2,session.putvalue ("Key1", Object1);
Object o=session.getvalue ("Key1");

Vi. handling Chinese problems in JSP

1,ascii Code
8bit storage, 0~31 and 127 are control characters, 32~126 are visible characters.
2,gb2312
Two 8bit representations. The previous 127~255 to differentiate the ASCII code.
3,unicode
You can unify dozens of kinds of text encoding in the world under the same coding mechanism. stored in 16bit units. 0x0000~0xffff
4,iso-8859-1 or called latin-1,8859-1. The domain of Unicode is 0~255, the low is extended to 0~255 in ASCII, then the 0x00 is made up in the high position, which makes up 16bit (not quite understood here).
5, byte and Unicode Java kernel are unicode,class files as well. But the stream is in the byte way. Char is Unicode, byte is the byte method. Conversion function: Sun.io inside:
public static Bytetocharconverter Getdefault ();//Gets the encoding used by the system.
public static Bytetocharconverter Getconverter (String encoding);
Bytetocharconverter c=new Bytetocharconverter (["Encoding"]);
Byte[] S=c.convertall (char[] D);
You can also char[] D=c.converterall (byte[] s);
6, some functions:
integer.tohexstring (int i);
String s;s.getbytes ();
String (byte[]); String (byte[],encoding);//constructors
About Unicode encoding going to write a separate article

Seven, get the JVM property value

Properties props=system.getproperties ();
Enumeration Enum=props.propertynames (); Key enumeration
key= (String) enum.nextelement ();
String s= (String) props.getproperty (key);

Eight, JSP error handling

1, all exception objects that can be throw and catch are inherited from Throwable. Exception should be caught in the catch; The Error object is also inherited from the throwable, but should not catch, while the end of the program.
The 2,catch sequence should be exception from low to high.
3, translation error and client side request error. JSP source program errors, incorrect import paths, and so on generate a translation error (500) when the Servlet class document is generated. Client request errors are caught while the servlet class is executing.
4, when the error arises, can be jsp:forward to control, but better to use ErrorPage to deal with. You can also throw the new Exception ("ErrMsg").



Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.