DWR logon implementation (Getting Started)

Source: Internet
Author: User

If DWR is used, the effect is immediate. Although it may damage the MVC design pattern, its low-coupling programming method is as simple as developing a CS program. In the past, when I used ajax to write a program, the most time spent was the JS part. Now with DWR, I don't have to debug complicated JS and focus on the business logic, this will surely get twice the result! I just learned an entry-level instance. To help you learn, I posted the source code! Don't laugh. I am also a beginner in DWR.

Configure web. XML with the following code:
<? XML version = "1.0" encoding = "UTF-8"?>
<! Doctype web-app
Public "-// Sun Microsystems, Inc. // DTD web application 2.3 // en"
Http://java.sun.com/dtd/web-app_2_3.dtd>
<Web-app>
<Display-Name> SJF </display-Name>
<Description> SJF </description>
<Servlet>
<Servlet-Name> DWR-invoker </servlet-Name>
<Display-Name> DWR servlet </display-Name>
<Description> direct Web remoter servlet </description>
<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>
</Web-app>
DWR. xml file Configuration:
<? XML version = "1.0" encoding = "UTF-8"?>
<! Doctype DWR public "-// getahead limited // DTD direct Web remoting 1.0 //" http://www.getahead.ltd.uk/dwr/dwr10.dtd ">
<DWR>
<! -- Without allow, DWR isn't allowed to do anything -->
<Allow>
<Create scope = "application" creator = "new" javascript = "checklogin">
<Param name = "class" value = "com. Test. Ajax. userlogin"/>
</Create>
</Allow>
</DWR>
Java file (userlogin. Java) code:
Package com. Test. Ajax;

Public class userlogin {
Private string usern;
Private string passwd;
Public userlogin (){
}
Public String checklogin (){
If (usern. Equals ("admin") & passwd. Equals ("admin ")){
Return "Login success! ";
} Else {
Return "Login faild! ";
}
}
Public void setfield (string usern, string passwd ){
This. usern = usern;
This. passwd = passwd;
}
Public static void main (string ARGs []) {
Userlogin ul = new userlogin ();
Ul. setfield ("admin", "admin ");
System. Out. println (UL. checklogin ());
}
}
The code for JSP files (which can be replaced by HTML files) is as follows:
<% @ Page Language = "Java" contenttype = "text/html; charset = UTF-8" pageencoding = "UTF-8" %>
<! Doctype HTML public "-// W3C // dtd xhtml 1.0 transitional // en" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<HTML xmlns = "http://www.w3.org/1999/xhtml">
<Head>
<Meta http-equiv = "Content-Type" content = "text/html; charset = UTF-8"/>
<Title> DWR for user login </title>
<SCRIPT type = "text/JavaScript" src = "<% = request. getcontextpath () %>/DWR/interface/checklogin. js"> </SCRIPT>
<SCRIPT type = "text/JavaScript" src = "<% = request. getcontextpath () %>/DWR/engine. js"> </SCRIPT>
<SCRIPT type = "text/JavaScript" src = "<% = request. getcontextpath () %>/DWR/util. js"> </SCRIPT>
<Script language = "JavaScript">
VaR mycall = function callback (data ){
Alert (data );
}
Function login (){
VaR UN = Document. getelementbyidx ("form_usern ");
VaR Pd = Document. getelementbyidx ("form_passwd ")
Checklogin. setfield (UN. Value, PD. value );
Checklogin. checklogin (mycall );
}
</SCRIPT>
</Head>
<Body>
<H1> User Login <Label for = "form_usern"> User: </label> <input type = "text" id = "form_usern"/> <br/>
<Label for = "form_passwd"> password: </label> <input type = "password" id = "form_passwd"> <br/>
<Input type = "button" value = "login" onclick = "javascript: Login ()">
</Body>
</Html>
Note: Make sure that the DWR. jar file exists in the \ WEB-INF \ Lib folder.

# Java Column

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.