First school JSP experience

zhaozj2021-02-16  109

First school JSP experience

Author: Raymond [hhzqq@21cn.com] test environment for jdk1.2.2 jswdk-1.0 winnt4.0 Chinese version.

1. Java is sensitive, people who have used other programming languages ​​are the easiest to commit this mistake, especially when I just came. It is because it is because of this.

2. Java's calling process is to be added parentheses, and it is easier to ignore at the beginning, such as title = Request.getParameter (/ "Title /"). Trim ();

3. The solution to the request.form () and Request.QueryString () in the JSP. The parameters in JSP do not have FORM and QueryString points, all through Request.getParameter (/ "xxxx /"). Although JSP also has a request.getQueryString () method, the test result is Test.jsp? Id = 1 & Page = 20 Get ID = 1 & Page = 20. If the URL and FORM have the same parameter name? Below is a test code:

Name is the ID, the result is the parameter of the URL is prioritized, and this processing method of JSP and the ASP have their own strengths.

4. Chinese character treatment problem with headache. In other articles, I have said that the statement output is garbled in the Chinese NT environment, <% = / "Hello /"%> and out.print (/ "Hello /"); The solution is to get the correct result as long as the string variable can be encoded, the following code can get the correct output: <% string title = / "Hello /"; Byte [] TmpByte = Title.getbytes (/ "ISO8859_1 /" ); Title = new string (tmpByte); out.print (title);%> or <% = title%>

Regarding the SQL statement Chinese characters, examples showing the SELECT * from test where title = / 'Who is a fool /' in the JDBC-ODBC driver DB2, whether it is the original sentence or the SQL statement is done, but it is dead. After changing the IBM's JDBC, the program can pass the program to the SQL statement.

The generation of this problem is probably the reason for Chinese NT. In other environments, there may be no Chinese characters, it is said that IBM's Web Sphere is very good for Chinese support, which also brings a generic issue for JSP development. It is said that the string coding is a general solution, but there is not so much environment to test.

5. The string judgment statement is often used in the ASP, such as if state = / "True," THEN .................................................................................................................................................... a. String str1 = / "I am a fool /"; string str2 = / "I am a fool /"; (or string str2 = / "I /" / "fool /";) IF (str1 == str2) Out.print (/ "yes /"); else out.print (/ "no /"); the result is / "yes /". Probably compilation optimization, STR1, STR2 points to the same class instance; b. String str1, str2, str3; str1 = / "I am a fool /"; str2 = / "I /"; str3 = str2 / "fool /" ; If (str1 == str3) Out.print (/ "yes /"); else out.print (/ "no /"); the result is / "no /".

String str1 = new string (/ I am a fool / "); string str2 = new string (/" I am a fool / "); if (str1 == str2) Out.print (/" yes / "); else Out .print (/ "no /"); result is / "no /".

String str1 = new string (/ "I am a fool /"); string str2 = new string (/ "I am a fool /"); if (str1.compareto (str2) == 0) Out.print (/ "YES / "); Else out.print (/" no / "); the result is /" yes / ".

Therefore, in JSP to determine the string to use the Compareto method, it is true that the traditional language is really adapted, but friends who are familiar with Java should have no problem.

6. How to determine the database empty? Result = stmt.executeQuery (SQL); if (result.next ()) ... Result execution, the cursor is out of an unprecedented state, and cannot be judged, and the value cannot be taken, it must be next () Can be used.

7. Implement pagination in JSP. Page is keyword and cannot be variables. Conn.jsp <% string sdbdriver = / "com.ibm.db2.jdbc.app.db2driver/; string sconnstr = / "jdbc: db2: faq /"; connection conn = null; statement stmt = null; ResultSet RS = Null; try {class.forname (sdbdriver);} catch (java.lang.classnotfoundexception e) {out.print (/ "FAQ (): /" E.getMessage ());} Try {conn = drivermanager.getConnection (Sconnstr, / "WSDEMO /", / "wsdemo1 /"); stmt = conn.createstatement ();} catch (sqlexception e) {output (e.tostring ());}%> query.jsp

<% @ page language = / "java /" import = / "java.sql. * /"%> <% @ Page ContentType = / "text / html; charset = GB2312 /"%> <% @ include file = / "Conn.jsp /"%> <% ....... int Pages = 0; int Pagesize = 10; ResultSet Result = null; ResultSet Rcount = NULL;

Pages = new integer (Request.getParameter). INTVALUE ();

IF (Pages> 0) {

String SQL = / "state = / 'I am not stupid /' /"; int count = 0; try {rcount = stmt.executeQuery (/ "Select Count (ID) AS ID from user where /" SQL); Catch ( SQLEXCEPTION EX) {OUT.PRINT (/ "aq.executequence: /" ex.getMessage ());} if (rcount.next ()) count = rcount.getint (/ "id /"); rcount.close );

IF (count> 0) {sql = / "select * from user where /" SQL; try {result = stmt.executeQuery (SQL);} catch (sqlexception ex) {out.print (/ "aq.executeQuery: / " ex.getMessage ());

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

New Post(0)