Several summary of JSP entry knowledge

Source: Internet
Author: User

Since July, I started to learn JSP. In the past, I had only the awareness of a program. I had to explore it all the way. After a lot of hardships, I finally got one day, like a dry duck who learns to swim, I am very happy. After a few days and nights, I finally wrote the article publishing program of this pure JSP.
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!

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.