First, let's look at the directory structure.
-- + Login
---------- + WEB-INF
----------------------- + Classes
-Beans
-Tags
----------- + TLDs
Login is the JSP file in the main directory. In the example login. jsp, loginfailed. jsp, login_form.jsp, newaccount. jsp, welcome. jsp, accountcreated. jsp
Under web-INF, there is a web. xml configuration file, classes folder class, And TLDs folder with custom labels.
Because I didn't use a database, I didn't use the Lib folder. Instead, I placed the *. jar file.
The classes directory contains the beans and tags folders, which are respectively placed in the user, logindb, and getrequestparametertag categories. The loginservlet and newaccountservlet controller classes are also directly stored in the classes directory.
Let's first look at the two business object classes under beans.
User. Java
Package beans;
Public class user implements java. Io. serializable {
Private final string username, password, hint;
// Final indicates that the hint cannot be modified after this attribute is initialized.
Public user (string username, string password, string hint ){
This. Username = username;
This. Password = password;
This. Hint = hint;
}
Public String GetUserName (){
Return username;
}
Public String GetPassword (){
Return password;
}
Public String gethint (){
Return hint;
}
// Determine whether the user name and password of the current object are equal
Public Boolean equals (string uname, string upwd ){
Return GetUserName (). Equals (uname )&&
GetPassword (). Equals (upwd );
}
}
Logindb. Java
Package beans;
Import java. util. iterator;
Import java. util. vector;
Public class logindb implements java. Io. serializable {
Private vector users = new vector ();
// The Vector class is synchronized, so adduser does not need to be synchronized.
Public void adduser (string name, string PWD, string hint ){
Users. Add (new user (name, PWD, hint ));
}
// The following method is used to determine whether a correct user exists:
Public user getuser (string name, string PWD ){
Iterator it = users. iterator ();
User user;
// Iteration needs to be synchronized
Synchronized (users ){
While (it. hasnext ()){
User = (User) it. Next ();
If (user. Equals (name, PWD ))
Return user; // If the returned result is true, the current user is returned.
}
}
Return NULL;
}
Public String gethint (string name ){
Iterator it = users. iterator ();
User user;
Synchronized (users ){
While (it. hasnext ()){
User = (User) it. Next ();
If (user. GetUserName (). Equals (name ))
Return user. gethint ();
}
}
Return NULL;
}
}
Login. jsp
<HTML>
<Head>
<Meta http-equiv = "Content-Type" content = "text/html; charset = gb2312">
<Title> login page </title>
</Head>
<Body>
<@ Include file = "login_form.jsp">
</Body>
</Html>
The contained login_form.jsp
<% @ Taglib uri = "utilities" prefix = "util" %>
<! -- Call a custom tag and reference it as util. Utilities of Uri are mapped to Web. xml. -->
<P> <font color = "# 6666cc"> Please log on </font> </P>
<HR>
<Form name = "form1" method = "Post" Action = "<% = response. encodeurl (" login ") %>"> <! -- Login is a loginsevlet ing through web. xml -->
<Table width = "68%" border = "0" cellpadding = "2" cellspacing = "2">
<Tr>
<TD width = "33%" align = "right"> User name: </TD>
& Lt; TD width = "67%" & gt;
<Input type = "text" name = "username" value = "<util: requestparameter property = 'username'/>"> </TD> <! -- Note that the custom tag is used here. If there is a value, the custom tag is displayed. -->
</Tr>
<Tr>
<TD align = "right"> password: </TD>
<TD> <input type = "text" name = "userpwd"> </TD>
</Tr>
<Tr align = "center">
<TD colspan = "2">
<Input type = "Submit" name = "Submit" value = "login">
</TD>
</Tr>
</Table>
</Form>
Loginservlet. Java
Import javax. servlet .*;
Import javax. servlet. http .*;
Import java. Io. ioexception;
Import beans. user;
Import beans. logindb;
Public class loginservlet extends httpservlet {
Private logindb;
Public void Init (servletconfig config) throws servletexception {
Super. INIT (config );
Logindb = new logindb ();
Config. getservletcontext (). setattribute ("logindb", logindb );
}
Public void doget (httpservletrequest request, httpservletresponse response)
Throws servletexception, ioexception {
String name = request. getparameter ("username ");
// Obtain the value from the login_form.
String Pwd = request. getparameter ("userpwd ");
User user = logindb. getuser (name, PWD );
If (user! = NULL) {// indicates that a user exists.
Request. getsession (). setattribute ("user", user );
// Put it in the session
Request. getrequestdispatcher (response. encodeurl ("/welcome. jsp "))
. Forward (request, response );
// Successfully forwarded to welcome. jsp
} Else {
Request. getrequestdispatcher (response. encodeurl ("/loginfailed. jsp "))
. Forward (request, response );
}
}
Public void dopost (httpservletrequest request, httpservletresponse response)
Throws servletexception, ioexception {
Doget (request, response );
}
}
Add web. xml
login
loginservlet
new_account
newaccountservlet
new_account
/new_account
login
/login
utilities
/WEB-INF/TLDs/utilities. TLD
Utilities. TLD key part
<Taglib>
<Tag>
<Name> requestparameter </Name>
<Tagclass> tags. getrequestparametertag </tagclass>
<! -- Class location. If there is a package, write -->
<Info> simplest example: inserts one line of output </INFO>
<Bodycontent> Empty </bodycontent>
<Attribute>
<Name> property </Name>
<Required> true </required>
<Rtexprvalue> true </rtexprvalue>
</Attribute>
</Tag>
</Taglib>
Custom tag class getrequestparametertag. Java
Package tags;
Import javax. servlet. servletrequest;
Import javax. servlet. jsp. jspexception;
Import javax. servlet. jsp. tagext. tagsupport;
Public class getrequestparametertag extends tagsupport {
Private string property;
Public void setproperty (string property ){
This. Property = property;
}
Public int dostarttag () throws jspexception {
Servletrequest Reg = pagecontext. getrequest ();
String value = reg. getparameter (property );
Try {
Pagecontext. getout (). Print (value = NULL? "": Value );
} Catch (Java. Io. ioexception e ){
Throw new jspexception (E. getmessage ());
}
Return skip_body;
}
}
Successfully logged on to welcome. jsp.
<HTML>
<Head>
<Meta http-equiv = "Content-Type" content = "text/html; charset = gb2312">
<Title> welcome page </title>
</Head>
<Body>
<JSP: userbean id = "user" Scope = "session" class = "Beans. User"/>
<! -- Yes
<%
User user = (User) Session. getattribute ("user ");
%>
-->
Welcome: <font color = Red> <% = user. GetUserName () %> </font>
</Body>
</Html>
Loginfailed. jsp Login Failed
<HTML>
<Head>
<Meta http-equiv = "Content-Type" content = "text/html; charset = gb2312">
<Title> Login Failed </title>
</Head>
<Body>
<Font color = "#993366"> enter your username and password, or create a new user! </Font>
<% @ Include file = "/login_form.jsp" %>
<HR>
<A href = "<% = response. encodeurl (" newaccount. jsp ") %>"> Create a new user </a>
</Body>
</Html>
Create newaccount. jsp
<HTML>
<Head>
<Meta http-equiv = "Content-Type" content = "text/html; charset = gb2312">
<Title> New Account </title>
</Head>
<Body>
<Font color = "#996633"> Create a new user </font>
<Form name = "form1" method = "Post" Action = "<% = response. encodeurl (" new_account ") %>">
<Table width = "75%" border = "0" cellpadding = "3">
<Tr>
<TD width = "42%" align = "right"> User name: </TD>
<TD width = "58%"> <input type = "text" name = "username"> </TD>
</Tr>
<Tr>
<TD align = "right"> password: </TD>
<TD> <input type = "text" name = "userpwd"> </TD>
</Tr>
<Tr>
<TD align = "right"> password: </TD>
<TD> <input type = "text" name = "hint"> </TD>
</Tr>
<Tr align = "center">
<TD colspan = "2"> <input type = "Submit" name = "Submit" value = "Submit"> </TD>
</Tr>
</Table>
</Form>
</Body>
</Html>
Registered controller newaccountservlet
Import javax. servlet .*;
Import javax. servlet. http .*;
Import java. Io. ioexception;
Import beans. logindb;
Public class newaccountservlet extends httpservlet {
Public void dopost (httpservletrequest request, httpservletresponse response)
Throws servletexception, ioexception {
Logindb = (logindb) getservletcontext (). getattribute ("logindb ");
Logindb. adduser (request. getparameter ("username "),
Request. getparameter ("userpwd "),
Request. getparameter ("hint "));
Request. getrequestdispatcher (response. encodeurl ("accountcreated. jsp"). Forward (request, response );
}
}
Accountcreated. jsp
untitled document
the new user has create! <% = request. getparameter ("username ") %>
<% @ include file = "login_form.jsp" %>