学习了,留个痕迹吧。利用Dwr实现登陆的小例子
一、首先新建一个web项目,准备好common-logging.jar和dwr.jar导入项目中,在web.xml中加入: <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>false</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>dwr-invoker</servlet-name>
<url-pattern>/dwr/*</url-pattern>
</servlet-mapping>
二、新建一个User.java
public class User {
private String name;
private String psd;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPsd() {
return psd;
}
public void setPsd(String psd) {
this.psd = psd;
}
}
和Login.java
public class Login {
public User login(User user) {
User u = new User();
user.setName("xu");
user.setPsd("xu");
System.out.println("name=========="+user.getName());
System.out.println("psd=========="+user.getPsd());
if ("xu".equals(user.getName()) && "xu".equals(user.getPsd())) {
return user;
}
return null;
}
三、在web.xml同级目录下新建配置文件dwr.xml:
<!DOCTYPE dwr PUBLIC "-//GetAhead Limited//DTD Direct Web Remoting 2.0//EN" "http://getahead.org/dwr//dwr20.dtd">
<dwr>
<allow>
<convert match="com.pojo.User" converter="bean">
<param name="include" value="name,pass"></param>
这里对应页面js部分的2个参数
</convert>
<create creator="new" javascript="Login">
<param name="class" value="com.login.Login" />
<include method="login" />
</create>
</allow>
</dwr>
这些都是必要的配置文件
在login.jsp上加入
<script type='text/javascript' src='/项目名/dwr/engine.js'></script>
<script type='text/javascript' src='/项目名/dwr/util.js'></script>
这2个是不变的
<script type='text/javascript' src='/项目名/dwr/interface/Login.js'></script>这里的Login与dwr.xml中的Login对应
务必加上项目名
<SCRIPT LANGUAGE="JavaScript">
function CallBack(data) {
var txt = document.getElementById("GetNumber");
if (data == null) {
txt.innerHTML ="参数传递出现错误!";
}else{
txt.innerHTML =data.name ;
}
}
function login(){
var name1=document.getElementById("name").value;
var psd1=document.getElementById("psd").value;
us={name:name1,pass:psd1}
Login.login(us,CallBack);
}
</SCRIPT>
在body里加入
<form name="Form1">
用户名:
<input type="text" name="name" id="name" >
<br>
密码:
<input type="password" id="psd" >
<br>
<input type="button" value="登陆" οnclick="login();" >
<br>
<span id="GetNumber" style="color: red;"></span>
</form>
运行看看结果吧 正确的应该为:输入xu/xu 点击登陆后会在按钮下方出现xu控制台上也会有打印信息
一、首先新建一个web项目,准备好common-logging.jar和dwr.jar导入项目中,在web.xml中加入: <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>false</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>dwr-invoker</servlet-name>
<url-pattern>/dwr/*</url-pattern>
</servlet-mapping>
二、新建一个User.java
public class User {
private String name;
private String psd;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPsd() {
return psd;
}
public void setPsd(String psd) {
this.psd = psd;
}
}
和Login.java
public class Login {
public User login(User user) {
User u = new User();
user.setName("xu");
user.setPsd("xu");
System.out.println("name=========="+user.getName());
System.out.println("psd=========="+user.getPsd());
if ("xu".equals(user.getName()) && "xu".equals(user.getPsd())) {
return user;
}
return null;
}
三、在web.xml同级目录下新建配置文件dwr.xml:
<!DOCTYPE dwr PUBLIC "-//GetAhead Limited//DTD Direct Web Remoting 2.0//EN" "http://getahead.org/dwr//dwr20.dtd">
<dwr>
<allow>
<convert match="com.pojo.User" converter="bean">
<param name="include" value="name,pass"></param>
这里对应页面js部分的2个参数
</convert>
<create creator="new" javascript="Login">
<param name="class" value="com.login.Login" />
<include method="login" />
</create>
</allow>
</dwr>
这些都是必要的配置文件
在login.jsp上加入
<script type='text/javascript' src='/项目名/dwr/engine.js'></script>
<script type='text/javascript' src='/项目名/dwr/util.js'></script>
这2个是不变的
<script type='text/javascript' src='/项目名/dwr/interface/Login.js'></script>这里的Login与dwr.xml中的Login对应
务必加上项目名
<SCRIPT LANGUAGE="JavaScript">
function CallBack(data) {
var txt = document.getElementById("GetNumber");
if (data == null) {
txt.innerHTML ="参数传递出现错误!";
}else{
txt.innerHTML =data.name ;
}
}
function login(){
var name1=document.getElementById("name").value;
var psd1=document.getElementById("psd").value;
us={name:name1,pass:psd1}
Login.login(us,CallBack);
}
</SCRIPT>
在body里加入
<form name="Form1">
用户名:
<input type="text" name="name" id="name" >
<br>
密码:
<input type="password" id="psd" >
<br>
<input type="button" value="登陆" οnclick="login();" >
<br>
<span id="GetNumber" style="color: red;"></span>
</form>
运行看看结果吧 正确的应该为:输入xu/xu 点击登陆后会在按钮下方出现xu控制台上也会有打印信息