Log Summary 6-JSP Learning Experience Summary.

xiaoxiao2021-03-30  192

First, JSP works

When a JSP file is first requested, 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, the output buffer of the server

By default: The server is output to the client's content, not directly written to the client, but first writes to an output buffer. Only in the case of the following three, the content of the buffer is output to On the client:

The Output Output Buffer of the JSP page has completed information is full in JSP to call the out.flush () or response.flushbuffer () output buffer size can be used: or response.setBuffersize () setting, as follows: Set the output buffer The size is 1 kB. 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 is not yet.

Third, the server output redirection

There are three ways to do the output redirection:

Response.SetRedect ("URL") This method is a redirected command to the browser to redirect the browser to the browser to redirect the browser to the browser. Response.sendredirect ("http: // localhost: 7001 / Index.html "); The following 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 This method is to use the server side to output the data to the buffer. Before sending the contents of the buffer to the client, the original does not send, change Send this page, if there is a lot of output before , the previous output has enabled the buffer, automatically outputs to the client, then the statement will not work, this should be paid to it. As here example (1) will output the contents of index.html, index.html 2 content not output, but the output out.println ( "@@@@@@@@@@@@@@@@@" ); Content, and on the server will thrown: java.lang.illegalStateException: Response alleady committed exception, but the client does not have any error output. (1) <% @ page buffer = "1kb"%> <% long i = 0; for (i = 0; i <10; i ) {out.println ( "@@@@@@@@@@@ @@@@@ ";}%> (2) <% @ Page Buffer =" 1kb "%> <% long i = 0; for ( i = 0; i <600; i ) {out.println ( "@@@@@@@@@@@@@@@@@");}%> Description: 1. a method (1), (2 ) You can use variables to indicate a redirected address; method (3) cannot use variables to represent a redirected address. String add = "./ index.html"; Unable to redirect to index.html

String add = http: // localhost: 7001 / index.html response.sendRedirect (add); can be redirected to http: // localhost: 7001 / index.html.

2. Using the method (1), (2) REQUEST (by request.setttribute () is used to save the value in the Request), the method (3) can be adopted in the new page. In summary, we should use (1), (2) The redirection is better.

Fourth, the correct application class in JSP:

The class should be used as a Java bean, do not use it directly in <%>> The following code (1) will change to code after transformation of JSP engine (2): From it can see if a class is in JSP as Java BEAN uses, JSP will save it to the corresponding internal object based on its scope. If the scope is request, save it to the Request object. And only call (NULL of the object). When an object of this class is created directly in <%>>, each time you call JSP, you will recreate the object, which will affect performance. Code (1) <% test.print (" this is use bean "; testdemo td = new testdemo (); td.print (" This is use new ");%>

Code (2) Demo.com.testdemo test.getaTRibute ("test"); if (test == null) {TRY {Test = (Demo.com.testdemo) .beans.beans .instantiate (getClass () getClassLoader (), "demo.com.testdemo".);} catch (Exception _beanException) {throw new weblogic.utils.NestedRuntimeException ( "can not instantiate 'demo.com.testdemo'", _ beanException); } Request.setttribute ("test", test); out.print ("/ r / n");} out.print ("/ r / n / r / n / r / n"); test.print (" Testdemo TD = New TestDemo (); Td.Print ("this is use new");

Five, JSP debugging

JSP debugging is more troublesome, especially when Bean is in a session, it is more difficult. Ghey from a few pages to the inside. It is usually to check the problem with out.println () or system.out.print () to check the problem. 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 an operation of a NULL value. As the following operations will throw .lang.NullPointeredException string a = null; a.substring (0, 1); To avoid this 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 the most likely to make this mistake. 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 Show:

String str1 = "abcd"; string str2 = "abcd"; (or string str2 = "ab" "CD"; if (str1 == str2) Out.print ("YES"); else out.print ("NO "); The result is" yes ".

String str1, str2, str3; str1 = "abcd"; str2 = "ab"; str3 = str2 "cd"; if (str1 == str3) Out.print ("yes"); Else Out.print ("NO" The 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 "); The 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 / setup / inspection page is set to check each time the page is accessed.

Sixth, cookie

HTTP cookie is essentially a normal HTTP header that is transmitted between the client and saves or saved on the customer's hard drive. If saved, each file size does not exceed 4K text files. Multiple cookies can be saved to the same In a file. If you look from a programming point, cookie in JSP is a class provided by Java. The common method is as follows, because the client may not accept cookies, so it is recommended to use it, change the 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 () / / Return to the valid path of this cookie PUBLIC Boolean getSecure () // Returns the security setting of the cookie PUBLIC STRING GETVALUE () // Returns the value of the cookie PUBLIC VOID SETDOM

转载请注明原文地址:https://www.9cbs.com/read-130493.html

New Post(0)