A wap1.0-based Web project (text)-implement login verification

Source: Internet
Author: User

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.

 


====
Package 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 //" http://www.wapforum.org/DTD/wml_1.1.xml ">
<WML>

<Head> userlogin

<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 //" http://www.wapforum.org/DTD/wml_1.1.xml ">
<WML>
<Head> display page <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 //" http://www.wapforum.org/DTD/wml_1.1.xml ">
<WML>
<Head> display page <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. Test

Enter the correct admin; 123

Input error 123; 123


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.