The following program is the test. jsp file:
<% @ Page contenttype = "text/html; charset = gb2312" Language = "Java" Import = "Java. SQL. *" errorpage = "" %>
<! Doctype HTML public "-// W3C // dtd html 4.01 transitional // en" "http://www.w3.org/TR/html4/loose.dtd">
<HTML>
<Head>
<Meta http-equiv = "Content-Type" content = "text/html; charset = gb2312">
<Title> test page </title>
<Script language = "JavaScript">
<! --
Function test_onsubmit (){
If (document. Test. Name. value = "")
{
Alert ("sorry, name not entered! ")
Document. Test. Name. Focus ()
Return false
}
Else if (document. Test. Password. value = "" | document. Test. Password. value. touppercase () = "http ://")
{
Alert ("sorry, no password entered! ")
Document. Test. Password. Focus ()
Return false
}
}
// -->
</SCRIPT>
<LINK rel = "stylesheet" href = "CSS/style.css"> _ fcksavedurl = "" CSS/style.css ">"
</Head>
<Body>
<Div align = "center">
<P> </P>
<P> test page </P>
<Form name = "test" method = "Post" Language = "JavaScript" onsubmit = "Return test_onsubmit ()" Action = "doadd. jsp">
<P> Name:
<Input name = "name" type = "text" size = "16" maxlength = "16">
</P>
<P> password:
<Input name = "password" type = "text" size = "16" maxlength = "16">
</P>
<P>
<Input type = "Submit" name = "Submit" value = "Submit">
</P>
</Form>
<P> </P>
</Div>
</Body>
</Html>
The following is the doadd. jsp file:
<% @ Page contenttype = "text/html; charset = gb2312" %>
<% @ Page import = "Java. SQL. *" %>
<%! String name, password; %>
<% Class. forname ("com. Microsoft. JDBC. sqlserver. sqlserverdriver"). newinstance ();
String url = "JDBC: Microsoft: sqlserver: // localhost: 1433; databasename = test ";
// Test is the database
String user = "sa ";
String Password = "";
Connection conn = drivermanager. getconnection (URL, user, password );
Statement stmt = conn. createstatement (resultset. type_scroll_sensitive, resultset. concur_updatable );
Request. setcharacterencoding ("gb2312 ");
Name = request. getparameter ("name") [0]. Trim ();
Password = request. getparameter ("password") [0]. Trim ();
Stmt.exe cuteupdate ("insert into test1 (name, password) values (" + name + "," + password + ")");
%>
<%
Stmt. Close ();
Conn. Close ();
%>
I use response in servlet to redirect the client
Parent form: AA. jsp
The targeted code in servlet is: Response. rendredirect ("BB. jsp ");
Class. forname ("com. Microsoft. JDBC. sqlserver. sqlserverdriver ");
Connection conn = drivermanager. getconnection "JDBC: Microsoft: sqlserver: // 127.0.0.1: 1433; databasename = mydata", "sa", "123 ");
3.
<% Class. forname ("oracle. JDBC. Driver. oracledriver"). newinstance ();
String url = "JDBC: oracle: @ myoracle ";
String uid = "system ";
String Pwd = "manager ";
Connection con = drivermanager. getconnection (URL, uid, PWD );
Statement stmt = con. createstatement ();
Resultset rst1_stmt.exe cutequery ("select * from book ");
While (RST. Next ())
{
Out. println ("<tr> ");
Out. println ("<TD>" + RST. getstring ("bookid") + "</TD> ");
Out. println ("<TD>" + RST. getstring ("bookname") + "</TD> ");
Out. println ("<TD>" + RST. getstring ("publisher") + "</TD> ");
Out. println ("<TD>" + RST. getfloat ("price") + "</TD> ");
Out. println ("</tr> ");
}
RST. Close ();
Stmt. Close ();
Con. Close ();
%>
</Table>
</Body>
</Html>
<% @ Page contenttype = "text/html; charset = gb2312" Language = "Java" Import = "Java. SQL. *" errorpage = "" %>
<HTML>
<Head>
<Title>
Read Database
</Title>
</Head>
<Body>
<%
Try {class. forname ("Sun. JDBC. ODBC. jdbcodbcdriver ");
} Catch (classnotfoundexception e ){
%>
<% = E %>
<%
}
Try {
Connection con = drivermanager. getconnection ("JDBC: ODBC: Test2 ");
Statement SQL = con. createstatement ();
Resultset rs= SQL .exe cutequery ("select * From Table1 ");
While (Rs. Next ())
{
%>
<% = Rs. getstring ("name") %>
<% = Rs. getstring ("password") %>
<%
}
Rs. Close ();
SQL. Close ();
Con. Close ();
} Catch (sqlexception ee ){
%>
<% = Ee %>
<%
}
%>
Nihao!
</Body>
</Html>