I started to learn JSP since September, and I had only one point in the past. Program After a lot of hardships and finally one day, I was as happy as a dry duck learning to swim, finally, I wrote this pure JSP Article Publish a program.
I believe the following tips will be helpful to cainiao of my level!
========================================================== ==============================================
1. Pass form parameters:
String name = new string (request. getparameter ("name "));
2. database connection:
~~ MySQL
// Set the database URL
String url = "JDBC: mysql: // localhost: 3306/jspsky ";
Try
// Load the driver
Class. forname ("org. gjt. Mm. MySQL. Driver"). newinstance ();
// Establish a connection
Java. SQL. Connection connection = java. SQL. drivermanager. getconnection (URL );
Java. SQL. Statement statement = connection. createstatement ();
// SQL statement
String sqlstringi = "insert into Commu (name, tel, mobile, OICQ, email) values ('" + name + "', '" + Tel + "', '"+ mobile +"', '"+ OICQ +"', '"+ email + "')";
// Run the SQL statement and create a result set
Java. SQL. resultset RSI = statement.exe cutequery (sqlstringi );
// Output the library content on the screen
While (RSS. Next ())
{
String a_name = RSS. getstring (1 );
Out. println (a_name );
{}
// Close the connection
Connection. Close ();
}
// Catch exceptions
Catch (Java. SQL. sqlexception E)
Out. println (E. getmessage ());
{}
Catch (classnotfoundexception E)
Out. println (E. getmessage ());
{}
~~ DB2
// Define the database URL
String url = "JDBC: DB2: Portal ";
Try
// Load the driver
Class. forname ("com. IBM. db2.jdbc. App. db2driver ");
// Establish a connection,
Java. SQL. Connection connection = java. SQL. drivermanager. getconnection (URL, "user", "password ");
Java. SQL. Statement statement = connection. createstatement ();
// SQL statement
String sqlstring = "select * from client ";
// Execute the SQL statement
Java. SQL. resultset rs = statement.exe cutequery (sqlstring );
// Display the contents of the connected table on the screen
While (Rs. Next ())
{
String name = Rs. getstring (1 );
Out. println (name );
{}
// Close the connection
Connection. Close ();
}
// Catch exceptions
Catch (Java. SQL. sqlexception E)
Out. println (E. getmessage ());
{}
Catch (classnotfoundexception E)
Out. println (E. getmessage ());
{}
3. File Operations
~~ Write a string to a specified file. If the file does not exist, create a new file and complete the writing. If yes, use this string to overwrite all the content of the original file.
Import java. Io .*;
String STR = "Print me Snow Peak! ";
// Define the printed target file name
// Obtain the absolute path for the current host to store web pages
String hostdir = system. getproperty ("user. dir ");
// Obtain the path Separator Used by the current host
String filebar = system. getproperty ("file. separator ");
// Complete target file storage path
String nameoffile = hostdir + filebar + "test.html ";
Try
// Instantiate a file output stream object
Fileoutputstream afile = new fileoutputstream (nameoffile );
// Output the file stream and create a print output stream object
Printwriter PW = new printwriter (afile );
PW. println (STR );
// Clean up
PW. Close ();
{}
Catch (ioexception E)
Out. println (E. getmessage ());
{}
~~ List objects in a specified directory
Import java. Io .*;
String cdur = system. getproperty ("user. dir ");
String filebar = system. getproperty ("file. separator ");
String mydir = cdur + filebar + "Doc" + filebar + "jspsky ";
File My = new file (mydir );
String d [] = My. List ();
Int I;
Int L = D. length;
For (I = 0; I out. Print (d [I]);
{}
4. counters
Integer COUNT = NULL;
Synchronized (Application)
Count = (integer) application. getattribute ("D ");
If (COUNT = NULL)
Count = new INTEGER ("0 ");
Count = new INTEGER (count. intvalue () + 1 );
Application. setattribute ("D", count );
{}
Out. println (count );
// First define an integer object and initialize it to null,
// Retrieves the value of attribute D of the Application object, forcibly converts it to an integer object, and assigns it to count.
// Determines whether the Count value is null. If it is null, O is assigned to the Count object,
// Otherwise, use count. Intvalue () method to add 1 to the Count object and assign the value to count
// Finally, save the Count object in the D variable of the Application object.
++
Next step
File Deletion
File Content Modification
Image Upload
Email list
JavaBeans
EJB
XML
Javascript
Database Operations and Maintenance
Understand the business project development process
Instance exercises
Message Board
Chat Room
Send email
News System
Intercepting webpage content
Shopping Cart
Do more exercises and keep familiar with the use of Java packages in practice!