1. Preface: This article mainly describes the practical application of the DWR framework, and describes how to use DWR through real-time verification of login information.
2. Create a web application project in myeclipse. Configure DWR in Web. xml. The configuration is as follows:
<Servlet>
<Servlet-Name> DWR-invoker </servlet-Name>
<Servlet-class> UK. Ltd. getahead. DWR. dwrservlet </servlet-class>
<Init-param>
<Param-Name> debug </param-Name>
<Param-value> true </param-value>
</Init-param>
</Servlet>
<Servlet-mapping>
<Servlet-Name> DWR-invoker </servlet-Name>
<URL-pattern>/DWR/* </url-pattern>
</Servlet-mapping>
3. create a Java class to verify the login information, including the user name, password, and user type. The information corresponds to the users table in database news, here we will create the users tableCodeAre listed as follows:
Create Database news
Go
Use news
Create Table users
(
Username varchar (50) default ('') primary key,
Password varchar (50) default (''),
Type varchar (50) default ('normal ')
)
Java class:
Package DWR. test;
Import java. SQL. connection;
Import java. SQL. preparedstatement;
Import java. SQL. resultset;
Import java. util. arraylist;
Import java. util. List;
Public class validator {
Private Static string username;
Private Static string password;
Private Static string type;
Private connection conn;
Private preparedstatement pS;
Private resultset RS;
Public void Init (){
Try {
Conn = dbconnection. getconnection ();
Debug. println (conn. tostring ());
} Catch (exception e ){
Debug. println ("no connection is obtained! ");
E. printstacktrace ();
}
}
Public list <string> getalltype (){
Init ();
List <string> typelist = new arraylist <string> ();
Try {
PS = conn. preparestatement ("select distinct (type) from loginfo ");
Rs = ps.exe cutequery ();
While (Rs. Next ()){
Typelist. Add (string) Rs. getstring ("type "));
}
} Catch (exception e ){
E. printstacktrace ();
}
Close ();
Return typelist;
}
Public String validate (int I ){
Debug. println ("Num:" + I );
Init ();
String sql1 = "select * From loginfo where type =? ";
String sql2 = "select * From loginfo where type =? And username =? ";
String sql3 = "select * From loginfo where username =? And Password =? And type =? ";
Try {
If (I = 1 ){
PS = conn. preparestatement (sql1 );
PS. setstring (1, type );
Rs = ps.exe cutequery ();
Debug. println (sql1 );
If (! Rs. Next ()){
Return "this department has no users! ";
}
} Else if (I = 2 ){
PS = conn. preparestatement (sql2 );
PS. setstring (1, type );
PS. setstring (2, username );
Rs = ps.exe cutequery ();
Debug. println (sql2 );
If (! Rs. Next ())
Return "username error! ";
} Else if (I = 3 ){
PS = conn. preparestatement (sql3 );
PS. setstring (1, username );
PS. setstring (2, password );
PS. setstring (3, type );
Debug. println (sql3 );
Rs = ps.exe cutequery ();
If (! Rs. Next ())
Return "incorrect user password! ";
}
Return NULL;
} Catch (exception e ){
Debug. println ("an exception occurred during judgment! ");
E. printstacktrace ();
} Finally {
Close ();
}
Return "an exception occurred during judgment ";
}
Public void close (){
Try {
If (RS! = NULL)
Rs. Close ();
If (PS! = NULL)
PS. Close ();
If (Conn! = NULL)
Conn. Close ();
} Catch (exception e ){
E. printstacktrace ();
}
}
Public void setusername (string username ){
Debug. println ("username:" + username );
This. Username = username;
}
Public void setpassword (string password ){
Debug. println ("Password:" + password );
This. Password = password;
}
Public void settype (string type ){
Debug. println ("type:" + type );
This. type = type;
}
Public static void main (string ARGs []) {
Validator v = new validator ();
V. setusername ("Jiangbin ");
V. setpassword ("Jiangbin ");
V. settype ("Administrator ");
If (V. Validate (3) = NULL)
Debug. println ("found the user! ");
}
}
The referenced database pool edge class is:
Package DWR. test;
Import java. SQL. connection;
Import javax. SQL. datasource;
Import org. Apache. tomcat. DBCP. DBCP. basicdatasource;
Public class dbconnection {
Private Static datasource;
Public static connection getconnection () throws exception {
If (datasource = NULL ){
Basicdatasource BDS = new basicdatasource ();
BDS
. Setdriverclassname ("com. Microsoft. JDBC. sqlserver. sqlserverdriver ");
BDS
. Seturl ("JDBC: Microsoft: sqlserver: // localhost: 1433; databasename = News ");
BDS. setusername ("sa ");
BDS. setpassword ("");
BDS. setmaxidle (2 );
BDS. setmaxactive (20 );
BDS. setdefaautoautocommit (true );
Datasource = BDS;
}
Return datasource. getconnection ();
}
}
The referenced debug class is:
/*
* Debug. Java
*/
/*
* Copyright (c) 2001 by Florian bomers <florian@bome.com>
*
*
* This program is free software; you can redistribute it and/or modify
* It under the terms of the GNU Library General Public License as published
* By the Free Software Foundation; either version 2 of the license, or
* (At your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* But without any warranty; without even the implied warranty
* Merchantability or fitness for a special purpose. See
* GNU Library General Public License for more details.
*
* You shoshould have written ed a copy of the GNU Library General Public
* License along with this program; if not, write to the Free Software
* Foundation, inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*
*/
Package DWR. test;
Public class debug {
Public static Boolean error = true;
Public static Boolean DEBUG = true;
Public static Boolean trace = true;
Public static Boolean trace_readwrite = true;
Public static Boolean trace_inout = false;
Public static Boolean show_all_exceptions = true;
// For net. Server
Public static Boolean slow_net_upload = true;
Public static Boolean slow_net_download = true;
// Show the time of a debug message
Private Static final Boolean show_times = true;
Private Static long start_time = system. currenttimemillis ();
Public static synchronized void println (string smessage ){
If (TRACE ){
If (show_times ){
Smessage = "" + (system. currenttimemillis ()-start_time)
+ ":" + Smessage;
}
System. Out. println (smessage );
}
}
Public static void println (Object OBJ, string smessage ){
If (TRACE ){
String Cn = obj. getclass (). getname ();
Int I = cn. lastindexof ('.');
If (I> = 0 & I <CN. Length ()-1 ){
CN = cn. substring (I + 1 );
}
Println (CN + ":" + smessage );
}
}
Public static synchronized void println (throwable t ){
If (TRACE ){
T. printstacktrace ();
}
}
Public static synchronized void printstacktrace (){
If (TRACE ){
Thread. dumpstack ();
}
}
}
4. Create the DWR. xml file to link the Java verification class with the page script object. The DWR. xml file and the web. xml file are in the same directory:
<? XML version = "1.0" encoding = "gb18030"?>
<DWR>
<Allow>
<Create creator = "new" javascript = "validator">
<Param name = "class" value = "DWR. Test. validator"/>
</Create>
</Allow>
</DWR>
5. Create index. jsp on the logon page. The page code is as follows:
<% @ Page Language = "Java" Import = "Java. util. *" pageencoding = "gb18030" %>
<HTML>
<Head>
<Title> log page </title>
<SCRIPT type = 'text/JavaScript 'src = './DWR/interface/validator. js'> </SCRIPT>
<SCRIPT type = 'text/JavaScript 'src = './DWR/engine. js'> </SCRIPT>
<SCRIPT type = 'text/JavaScript 'src = './DWR/util. js'> </SCRIPT>
<SCRIPT type = "text/JavaScript" src = "./JS/log. js"> </SCRIPT>
</Head>
<Body onload = "addusertype ();">
<Font color = "red" size = "4"> <Div id = "isuser"> </div>
</Font>
<HR>
<Form action = "success. jsp">
<Table>
<Tr>
<TD colspan = "3" bgcolor = "# 6699ff">
Login
</TD>
</Tr>
<Tr>
<TD>
User Type
</TD>
<TD>
<Select id = "type" onblur = "reply ('1');">
</SELECT>
</TD>
<TD>
<Div id = 'typetext '> </div>
</TD>
</Tr>
<Tr>
<TD>
User Name
</TD>
<TD>
<Input type = "text" name = "username" onblur = "reply ('2');"/>
</TD>
<TD>
<Div id = 'usernametext '> </div>
</TD>
</Tr>
<Tr>
<TD>
Password
</TD>
<TD>
<Input type = "password" name = "password" onblur = "reply ('3');"/>
</TD>
<TD>
<Div id = 'passwordtext'> </div>
</TD>
</Tr>
<Tr>
<TD colspan = "3">
<Input type = "button" value = "Submit" onclick = "check ();"/>
<Input type = "reset" value = "reset"/>
</TD>
</Tr>
</Table>
</Form>
</Body>
</Html>
6. The referenced log. js file in the page is as follows:
Function callback1 (STR ){
Dwrutil. setvalue ("isuser", STR );
}
Function callback2 (STR ){
Dwrutil. setvalue ("isuser", STR );
}
Function callback3 (STR ){
Dwrutil. setvalue ("isuser", STR );
}
Function callback4 (STR ){
If (STR = NULL ){
Location. href = "success. jsp ";
} Else {
Dwrutil. setvalue ("isuser", "\ error \: \ you \ input \ User \ email \ information \ do \ save \ In! ");
}
}
Function reply (Num ){
VaR type = dwrutil. getvalue ("type ");
If (type = "\ Please \ select "){
Dwrutil. setvalue ("isuser", "\ Please \ select \ User \ class \ type! ");
Return;
}
VaR username = dwrutil. getvalue ("username ");
VaR Password = dwrutil. getvalue ("password ");
Validator. settype (type );
Validator. setusername (username );
Validator. setpassword (password );
If (num = "1 "){
Validator. Validate (1, callback1 );
}
If (num = "2 "){
Validator. Validate (2, callback2 );
}
If (num = "3 "){
Validator. Validate (3, callback3 );
}
If (num = "4 "){
Validator. Validate (3, callback4 );
}
}
Function addusertype (){
Validator. getalltype (alltype );
}
Function alltype (typelist ){
VaR obj_type = Document. getelementbyid ("type ");
Dwrutil. removealloptions (obj_type );
Dwrutil. addoptions (obj_type, ["\ Please \ select"]);
Dwrutil. addoptions (obj_type, typelist );
}
Function check (){
Reply (4 );
}
Deploy the project to Tomcat. Pay attention to reference the DWR and jar packages on the build path.