1.create a new web project,named "wapDemo1".
2.create four packages,po,dao,service and action.
--
3.Create a new class User in packages po,define its attributes uname and passwd.
====
package po;
public class User {
private String uname;
private String passwd;
public String getUname() {
return uname;
}
public void setUname(String uname) {
this.uname = uname;
}
public String getPasswd() {
return passwd;
}
public void setPasswd(String passwd) {
this.passwd = passwd;
}
}
====
4.create a new detabase Db in packages, dao for demo.
====
package dao;
import java.util.ArrayList;
import java.util.List;
import po.User;
public class Db {
private static List<User> listUser = new ArrayList<User>();
static{
User u = new User();
u.setUname("admin");
u.setPasswd("123");
listUser.add(u);
}
public static List<User> getListUser() {
return listUser;
}
public static void setListUser(List<User> listUser) {
Db.listUser = listUser;
}
}
====
5.create a new class UserDao in packages dao .
===
package dao;
import java.util.ArrayList;
import java.util.List;
import po.User;
public class UserDao {
public List<User> queryUserDao(User user){
List<User> list = new ArrayList<User>();
List<User> dblist = Db.getListUser();
for(int i=0 ; i < dblist.size(); i++){
User u = dblist.get(i);
if(u.getUname().trim().equals(user.getUname().trim())
&& u.getPasswd().trim().equals(user.getPasswd().trim())){
list.add(u);
}
}
return list;
}
}
====
6.create a new class UserServcie in packages service .
import java.util.List;
import po.User;
import dao.UserDao;
public class UserService {
public boolean loginUserServcie(User user){
UserDao ud = new UserDao();
List<User> list = ud.queryUserDao(user);
if(list.size() < 1 || list == null){
return false;
}else{
return true;
}
}
}
====
7.create a new class UserAction in packages action .
====
package action;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import po.User;
import service.UserService;
public class UserAction extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
this.doPost(req, resp);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
User user = new User();
String uname = req.getParameter("uname");
String passwd = req.getParameter("passwd");
user.setUname(uname);
user.setPasswd(passwd);
UserService us = new UserService();
boolean flog = us.loginUserServcie(user);
System.out.println("sh22222si");
if (flog==true)
resp.sendRedirect("success.wml");
else
resp.sendRedirect("error.wml");
}
}
====
8.create a web login.jsp
====
<%@ page language="java" contentType="text/vnd.wap.wml; charset=UTF-8" %><?xml version="1.0"?>
<!DOCTYPE wml PUBLIC "-//WAPFORUM//DTD WML 1.1//EN" "http://www.wapforum.org/DTD/wml_1.1.xml">
<wml>
<head>UserLogin</head>
<card id="login" title="userlogin">
<p align="center">
username:<input name="uname" type="text"/> <br />
password:<input name="passwd" type="password"/><br />
<anchor>
<go href="login.action">
<postfield name="uname" value="$(uname)" />
<postfield name="passwd" value="$(passwd)" />
</go>
</anchor>
</p>
</card>
</wml>
====
9.create a web success.wml
====
<?xml version="1.0"?>
<!DOCTYPE wml PUBLIC "-//WAPFORUM//DTD WML 1.1//EN" "http://www.wapforum.org/DTD/wml_1.1.xml">
<wml>
<head>显示页面</head>
<card id="success" title="success">
<p align="center">
success.wml
</p>
</card>
</wml>
====
10.create a web error.wml
====
<?xml version="1.0"?>
<!DOCTYPE wml PUBLIC "-//WAPFORUM//DTD WML 1.1//EN" "http://www.wapforum.org/DTD/wml_1.1.xml">
<wml>
<head>显示页面</head>
<card id="error" title="error">
<p align="center">
error
</p>
</card>
</wml>
====
11.configure the web.xml
==
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>login</servlet-name>
<servlet-class>com.wapDemo.action.UserAction</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>login</servlet-name>
<url-pattern>/login.action</url-pattern>
</servlet-mapping>
</web-app>
====
12.测试
输入正确的admin;123
输入错误的123;123