JSP learning experience summary (transfer)
First, the JSP work principle is first requested in a JSP file, and the JSP engine converts the JSP file into a servlet. And this engine itself is also a servlet, in JSWDK or WebLogic, it is JSPServlet. The JSP engine first converts the JSP file into a Java source file. If the JSP file is found when the JSP file is discovered, the conversion process will be interrupted, and an error message is output to the server and the client; if the conversion is successful, the JSP engine uses Javac Compile the Java source file into the corresponding Class file. Then create an instance of the servlet, which is executed, and the jspinit () method is only executed once in the life cycle of the servlet. Then the JspService () method is called to process the client's request. For each request, the JSP engine creates a new thread to process the request. If there are multiple clients to request the JSP file at the same time, the JSP engine creates multiple threads. Each client requests a thread. Performing a multi-threaded manner can greatly reduce resource requirements for the system, improve the system's concurrency and response time. But you should pay attention to multi-thread programming limitations, because the servlet is always in memory, so the response is very fast. If the .jsp file is modified, the server will decide whether to recompile the file according to the settings. If you need to recompile, you need to replace the result in the memory, and continue the above processing. Although the JSP efficiency is high, there is a slight delay in the first call. Also, if at any time, if the system is insufficient, the JSP engine will remove the servlet from the memory in a certain uncertain manner. When this occurs, the jspdestroy () method is first called, and the servlet instance is marked to join the "garbage collection" process. The jspinit () and jspdestory () format are as follows: Some initialization works in JSPinit (), such as establishing a connection with the database, or establishing a network connection, taking some parameters from the configuration file, release the corresponding Resource. <%! public void jspinit () {system.out.println ("jspinit");}%> <%! public void jspdestory () {system.out.println ("jspdestory");}%> Second, server Output buffer default: The server is output to the client's content, not directly written to the client, but write to an output buffer. Only in the case of the following three, the buffer will be The content is output to the client: the output output buffer of the JSP page has completed information is full in the JSP to call the output buffer size of the output buffer () output buffer () settings: or response.setbuffersize () setting As follows: Set the size of the output buffer to 1kb. Or Response.setBuffersize (1); setting the size of the output buffer is 0, that is, it is not buffered.
Or Response.setBuffersize (0); the size of the output buffer with Response.getBuffersize () or out.getBuffersize () is byte. With Response.isCommitted () to check if the server has output to Client. If the return value is True, the data has been output to the client, and it has not been. Third, the server output redirection has the following three ways to do output redirection: Response.setRedERECT ("URL") This method is to redirect the command to the browser down to redirect the command by modifying the HTTP protocol, so that the browser displays the contents of the web page. Response.sendRedirect ("http: // localhost: 7001 / index.html"); below The method can also change the HTTP header attribute, its principle and 1 is the same. <% Response.setstatus (httpservletResponse.sc_moved_permanently); string newlocn = "/ index.html"; response.setHeader ("location", newlocn);% > Using
String add = "./ index.html";
If it is developed with JBuilder, it can debug JSP directly. But more important is the cause and solution to the error. Some JSP programming common errors are analyzed. (1) .java.lang.NullPointered is generally caused by a variable of a NULL value. Java.lang.NullPointerexception string a = null; a.substring (0, 1) as follows To avoid such an exception, it is best to check if it is NULL value before the variable operation is performed. Such as: <% string ss = session.getattribute ("name") if isnull (ss) {} else {}%> (2 ) .Jsp is written in Java, so it is sensitive, and people who have used other programming languages are most likely to make this error. In addition, the address of the access JSP entered in the address bar of the browser is also case sensitive. Such as http: // localhost: 7001 / demo / t.jsp and http: // localhost: 7001 / demo / t.jsp is not The same (3). Judging the string in JSP To use the CompareTo method, do not use ==, because String variables in Java are not a simple variable but a class instance, different methods have different results, as follows Scheduled: string str1 = "abcd"; string str2 = "abcd"; (or string str2 = "ab" "cd"; if (str1 = Str2) Out.print ("yes"); else out.print "no"); result is "yes". String str1, str2, str3; str3; str3 = "abcd"; str2 = "ab"; str3 = str2 "cd"; if (str1 == str3) Out.print ("YES "); Else out.print (" no "); result is" no ". String str1 = new string (" abcd "); string str2 = new string (" abcd "); if (str1 == str2) OUT. Print ("yes"); else out.print ("no"); result is "no". String str1 = new string ("abcd"); string str2 = new string ("abcd"); if (str1.compareto (str2) == 0) Out.print ("YES"); else out.print ("no"); result is "yes".
(4) Preventing the output from the JSP or Servlet from being saved in the buffer: The browser will save the browsed webpage in the buffer by default, when debugging, usually do not want this. Put the following script In the joining program, it is possible to prevent the output from the JSP or Servlet from being saved in the buffer in the buffer <% response.sethead ("cache-control", "no-store"); // http 1.1 response.setheader ("Pragma "," no-cache "); // http 1.0 response.setdateHeader (" expiRES ", 0); // prevents Caching At the Proxy Server%> can also be implemented in IE: Put / Tool / Internet Options / The newer version of the regular / set / setup page is set to check each time you access this page. Sixth, the cookie http cookie is essentially the normal HTTP header that is transmitted between the server and can save or save. On the customer's hard drive. If saved, each file size does not exceed 4K text files. Multiple cookies can be saved in the same file. If you look from a programming point, Cookie in JSP is a class provided by Java. Commonly used The method is shown below because the client may not accept cookies, so it is recommended to use it to change other ways such as SESSION.
Public class cookie {public string getDomain () // Returns the valid domain of the cookie Public INT getMaxage () // Returns the validity period of the cookie, the unit is second public string getName () // Returns the name of the cookie Public String getPath () / / Returns the valid path of the cookie Public Boolean getSecure () // Returns the security setting of the cookie PUBLIC STRING GETVALUE () // Returns the value of the cookie PUBLIC VOID SETDOMAIN (Java.lang.String Pattern) // Setting the cookie Valid domain public void setMaxage (int expiry) // Sets the validity period of the cookie, unit is a second public void setpath (java.lang.string URI) // set the valid path of the cookie PUBLIC VOID SETSecure (Boolean Flag) // Set this Cookie's Security Set Public Void SetValue (Java.lang.String NewValue) // Settings This cookie value} A cookie contains the following five sections: Name / Value pair, setting the name of the cookie and the values it saved Cookie usually related to the server If the domain is set to java.sun.com, then the cookie is related to this domain, only works on the URL, when browsing the URL, the browser will send the cookie's content to the server, cookie is used as Part of the HTTP Header is sent, and if there is no setting domain, cookies are only related to the server created the cookie. The path is used to specify the path where the Cookie can be used on the server, which onlys the path under this URL. The application works. "/" Indicates that all directories on the server can use this cookie. Cookie has a validity period, the validity default value is -1, which means that the cookie is not saved, when the browser exits, the cookie immediately Failure. Security Option True / false, if set to TRUE, then use the HTTPS protocol when the server is transmitted between the Cookie between the client. How to check if a client is Support cookie method: write a cookie to the client with the following method, and confirm the success try {cookie c = new cookie ("MyCookie", "cookie test"); response.addcookie (c);} catch (Exception E) {System.out.println (e);} then in a new JSP file: Take the client's cookie to cookies with the following method, if cookies.length == 0, explains that the client's browser does not support cookie Try {Cookie [] cookies = request.getCookies (); if (cookies.length == 0) {system.out.println ("not support cookie");}}}} catch (exception e) {system.out.println (e } Seven, JSP and Servlet difference: Sun first develops servlet, its function is relatively strong, the system design is also very advanced, but it outputs the HTML statement or the old CGI mode, is a sentence, so write It is very inconvenient to modify HTML. Later, Sun launched a JSP similar to the ASP, nested Java code into the HTML statement so that it is greatly simplified and convenient for web design and modification.