js| Program | programmer
1. Java is case-sensitive, and people who use other programming languages are most likely to make this mistake, especially when they are just getting started. I just started debugging JSP when 50% of the compilation errors are all because of this.
2. Java invocation process is to parentheses, at first it is easy to ignore, such as Title=request.getparameter ("title"). Trim ();
3. A workaround for Request.Form () and Request.QueryString () in the ASP in the JSP.
JSP to get the parameters are not form and querystring, are through the Request.getparameter ("XXXX") to obtain. Although the JSP also has the request.getquerystring () method, but the test result is test.jsp?id=1&page=20 obtains the id=1&page=20.
What if the URL and form have the same parameter name? Here is a section of the test code:
<form method= "POST" action= "query.jsp?id=2" >
<input type= "text" name= "id" value= "1" size= ">"
</form>
Name are IDs, the result is that the parameters of the URL first, JSP this approach and ASP than I feel the strengths.
4. Headache of Chinese character processing problems.
In other articles, said that in the Chinese NT environment, the following statement output will get garbled,
<%= "Hello"%> and Out.print ("Hello"); The solution is to encode the string variable to get the correct result, and 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%>
On the SQL statement Chinese character question, the example is select * from test where title= ' Who is fool '
In the JDBC-ODBC Drive even DB2, whether the original sentence or the SQL statement to encode after the dead and alive pass.
After changing IBM's JDBC Direct drive, the program can be passed after encoding the SQL statement.
This problem is probably the cause of the Chinese NT, in other circumstances may not have the Chinese character processing problem, it is said that IBM's web sphere to Chinese support is very good, this also gives JSP development brings certain universality problem. It is said that string encoding is a common solution, but there is not so much testing in the environment.
5. In ASP, often use to string judgment statements such as if state= "really stupid" then ...
In Java, a string variable is not a simple variable but a class instance, and 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".
Probably compiler optimization, STR1,STR2 point to the same class instance;
B.
String STR1,STR2,STR3;
Str1= "I Am a fool";
str2= "I Am";
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");
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".
So in the JSP to judge the string to use the CompareTo method, accustomed to the traditional language is really a very comfortable, familiar with Java friends should not this problem.
6. How can I tell if the database is empty?
result = Stmt.executequery (SQL);
if (Result.next ())
......
After result execution the cursor is out of an unspecified state and cannot be judged by status or value and must be next () to be used.
7. Implement pagination in the 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= ' I'm not stupid '";
int count=0;
try {
Rcount = Stmt.executequery ("SELECT count (ID) as ID from user where" +sql);
catch (SQLException ex) {
Out.print ("Aq.executequery:" + 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 ());
}
int i;
String name;
Result.first ();
Result.absolute ((pages-1) *pagesize);
This method jdbc2.0 support. The compiler passed, but did not know if it was related to the driver, so I had 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);
}
Result.close ();
int n= (int) (count/pagesize);
if (N*pagesize<count) n++;
if (n>1)
{
for (i=1;i<=n;i++)
Out.print ("<a href=query.jsp?pages=" +i+ ">" +i+ "</a>");
}
}
}
%>