Several JSP introduction knowledge summary

Source: Internet
Author: User
Tags define count db2 empty getmessage integer sql mysql

From last September, I began to learn JSP, before only a little program of consciousness, groping along the way over, after a lot of hardships, finally one day, I like a dry duck learned to swim, the heart is extremely happy, endure a few days, finally wrote this pure JSP article release program.
I believe that the following few small knowledge of the level of my rookie has a certain help!
==============================================================================
1. Pass form parameters:
String name = new String (Request.getparameter ("name"));

2. Database connection:
~~mysql
Set the URL of the database
String url = "Jdbc:mysql://localhost:3306/jspsky";
Try
Load 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 establish the result set
Java.sql.ResultSet RSI = Statement.executequery (Sqlstringi);
Output the contents of the library on the screen
while (Rss.next ())
{
String a_name = rss.getstring (1);
Out.println (A_name);
{}
Close connection
Connection.close ();
}

Catching exceptions
catch (Java.sql.SQLException e)

Out.println (E.getmessage ());
{}
catch (ClassNotFoundException e)

Out.println (E.getmessage ());
{}


~~db2
Define the URL of the database
String url = "Jdbc:db2:portal";
Try

Load 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 SQL statement
Java.sql.ResultSet rs = statement.executequery (SqlString);
Display the contents of the linked table on the screen
while (Rs.next ())
{
String name = rs.getstring (1);
OUT.PRINTLN (name);
{}
Close connection
Connection.close ();
}
Catching exceptions
catch (Java.sql.SQLException e)

Out.println (E.getmessage ());
{}
catch (ClassNotFoundException e)

Out.println (E.getmessage ());
{}


3. File operation

~ Writes a string to a specified file, creates a new file if it does not exist, completes writing, and overwrites all contents of the original file with this string if it exists
Import java.io.*;
String str = "Print me Xuefeng!";
Define a good print target file name

Get the absolute path of the current host hosting a Web page
String Hostdir = System.getproperty ("User.dir");
Gets the path separator used by the current host
String Filebar = System.getproperty ("File.separator");
Write the complete destination file storage path
String nameoffile=hostdir+filebar+ "test.html";

Try
Instantiate a file output stream object
FileOutputStream afile = new FileOutputStream (nameoffile);
Creates a print output stream object by streaming the file output
PrintWriter pw = new PrintWriter (afile);
Pw.println (str);
Clean up
Pw.close ();
{}
catch (IOException E)
Out.println (E.getmessage ());
{}

~ ~ Lists the list of files in the 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. Counter
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 a cosmetic object and initialize it to: NULL,
Retrieve the value of the attribute D of the application, and force it into a cosmetic object, assigned to count
To determine whether count is empty or empty, assign O to the Count object,
Otherwise, count is passed. Intvalue () method, implements the Count object plus 1, and assigns the value to count
Finally, the Count object is saved in the D variable of the Application object.


+++++++++++++++++++

Next point of study
Deletion of files
Modification of the contents of the file
Upload of pictures
Mailing list
JAVABEANS
Ejb
Xml
Javascript
Operation and maintenance of the database
Understand the Business Project development process

Example Practice
Walls
Chat room
Send mail
News system
Intercepting Web page content
Shopping Cart


Do more practice, in practice, and constantly familiar with the use of Java package!



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.