1. Java is case sensitive and has been used Programming Language People are most likely to make this mistake, especially when getting started. When I first started debugging JSP, more than 50% of the compilation errors were caused by this.
2. Java calls must be enclosed in parentheses, which are easy to ignore at the beginning, such as title = request. getparameter ("title"). Trim ();
3. JSP corresponds to ASP in the request. Form () and request. querystring () solution.
The form and querystring parameters obtained in JSP are obtained through request. getparameter ("XXXX. Although JSP also has the request. getquerystring () method, the test result is test. jsp? Id = 1 & page = 20 to get id = 1 & page = 20.
What if the URL and form have the same parameter name? Below is a testCode:
<Form method = "Post" Action = "query. jsp? Id = 2 ">
<Input type = "text" name = "ID" value = "1" size = "60">
</Form>
All the names are IDs, and the result is that URL parameters are given priority. jsp processing methods have advantages over ASP.
4. Chinese Character Handling Problems with headaches.
In otherArticleI once said that in the Chinese NT environment, the following statement output will get garbled characters,
<% = "Hello" %> and out. Print ("hello. The solution is to encode the string variable to get the correct result. 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 %>
For Chinese Characters in SQL statements, select * from test where title = 'who is dump'
If you connect to DB2 with the JDBC-ODBC driver, you will not be able to pass the coding of the original or SQL statement.
With the direct JDBC driver of IBMProgramYes.
This problem is probably caused by Chinese nt. In other environments, there may be no Chinese character processing problems. It is said that IBM's Web sphere has good support for Chinese characters, this also brings some general problems to JSP development. It is said that string encoding is a common solution, but there are not so many environments to test.
5. In ASP, string judgment statements are often used, such as if State = "really dummies" then .....
In Java, the string variable is not a simple variable but a class instance. Different methods get different results.
A.
String str1 = "I am a fool ";
String str2 = "I am a fool"; (or string str2 = "I am" + "fool ";)
If (str1 = str2)
Out. Print ("yes ");
Else
Out. Print ("no ");
The result is "yes ".
It is probably compilation optimization. str1 and str2 point to the same class instance;
B.
String str1, str2, str3;
Str1 = "I am a fool ";
Str2 = "I am ";
Str3 = str2 + "Dummies ";
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 ");
The 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, the compareto method is used to judge strings in JSP. It is really difficult to use traditional languages at once. This problem should not be solved if you are familiar with Java.
6. How can I determine whether the database is empty?
Result = stmt.exe cutequery (SQL );
If (result. Next ())
......
After the result is executed, the cursor is in an unknown state and cannot be judged or taken. You must click Next () to use the cursor.
7. Implement paging in JSP.
Page is a keyword and cannot be a variable.
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 ){
Out. Print (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 ("pages"). intvalue ();
If (pages> 0)
{
String SQL = "state = 'silly me '";
Int COUNT = 0;
Try {
Rcount = stmt.exe cutequery ("select count (ID) as ID from user where" + SQL );
Catch (sqlexception ex ){
Out. Print ("aq.exe cutequery:" + ex. getmessage ());
}
If (rcount. Next ())
Count = rcount. getint ("ID ");
Rcount. Close ();
If (count> 0)
{
SQL = "select * from user where" + SQL;
Try {
Result = stmt.exe cutequery (SQL );
}
Catch (sqlexception ex ){
Out. Print ("aq.exe cutequery:" + ex. getmessage ());
}
int I;
string name;
// result. first ();
// result. absolute (pages-1) * pagesize);
// This method is supported by jdbc2.0. Compilation passed, but the execution failed. I don't know if it is related to the driver, so I have to use the following stupid method.
for (I = 1; I <= (pages-1) * pagesize; I ++)
result. next ();
for (I = 1; I <= pagesize; I ++) {
If (result. next () {
name = result. getstring ("name");
out. print (name);
}< br> result. close ();
int n = (INT) (count/pagesize);
If (N * pagesize If (n> 1)
{< br> for (I = 1; I <= N; I ++)
out. print (" "+ I +" & nbsp; ");
}< BR >%>