A Techno Blog, mainly about Java

First Struts Example

Using WSAD 5.1.1 for the Struts register example.
Note that I had to apply this fix to WSAD due to an error I was having with the EAR files.
In WASD I create a blank struts web project (with struts 1.0.2) and imported the WAR example file.
Some of the files in the WEB-INF to know about:


Lets start out with the register.jsp file:

<%@ taglib uri=”/WEB-INF/struts-form.tld” prefix=”form” %>
<form:form action=””>

enter password:<form:password property=”password1″/>
re-enter password:

<form:submit value=”Register”/>

The key thing here is that the jsp makes use of the struts form tag library, and submits the data to

Now lets take a look first at the web.xml and then the struts-config.xml file:

<?xml version=”1.0″ encoding=”ISO-8859-1″?>
<!DOCTYPE web-app
PUBLIC “-//Sun Microsystems, Inc.//DTD Web Application 2.2//EN””&gt;

The key thing to note here is its using the web-app_2_2.dtd. Now on to the struts config file:

<?xml version=”1.0″ encoding=”ISO-8859-1″ ? >

<!DOCTYPE struts-config PUBLIC
“-//Apache Software Foundation//DTD Struts Configuration 1.0//EN””&gt;
<form-bean name=”registerForm” type=”app.RegisterForm”/>
<action path=”/register”
<forward name=”success” path=”/success.html”/>
<forward name=”failure” path=”/failure.html”/>

So, from the config xml file, its using the struts-config_1_0.dtd, specifies a form bean of
app.RegisterForm, specifies an action of app.RegisterAction, and contains two forwards of
success and failure.

So, in the java resources side of the project, we have a resources folder and a package called
app. One class in the app folder is the RegisterAction mentioned above, lets take a look:

package app;
import org.apache.struts.action.*;
import javax.servlet.http.*;
public class RegisterAction extends Action {
public ActionForward perform (ActionMapping mapping,
ActionForm form,
HttpServletRequest req,
HttpServletResponse res) {
// (1) Cast the form to the RegisterForm
RegisterForm rf = (RegisterForm) form;
String username = rf.getUsername();
String password1 = rf.getPassword1();

String password2 = rf.getPassword2();

// (2) Apply business logic
if (password1.equals(password2)) {

try {
// (3) Return ActionForward for success
return mapping.findForward(“success”);
} catch (UserDirectoryException e) {
return mapping.findForward(“failure”);
// (4) Return ActionForward for failure
return mapping.findForward(“failure”);

So in this RegisterAction class (which inherits from the Action class),
we have method perform. On of the parms to the method is the ActionForm
which I’ll show you in just a sec. We cast the ActionForm parm as RegisterForm which
was specified in the struts config file. Lets go to that RegisteForm class now.

package app;
import org.apache.struts.action.*;
public class RegisterForm extends ActionForm {
protected String username;
protected String password1;
protected String password2;
public String getUsername () {return this.username;};
public String getPassword1() {return this.password1;};
public String getPassword2() {return this.password2;};
public void setUsername (String username) {this.username = username;};
public void setPassword1(String password) {this.password1 = password;};
public void setPassword2(String password) {this.password2 = password;};


package app;
import java.util.Enumeration;
import java.util.Properties;
public class UserDirectory {
private static final String UserDirectoryFile = “resources/”;
private static final String UserDirectoryHeader = “${user}=${password}”;
private static UserDirectory userDirectory = null;
private static Properties p;
private UserDirectory() throws UserDirectoryException { i = null;
p = null;
i = this.getClass().getClassLoader().
if (null==i) {
throw new UserDirectoryException();
else {
try {
p = new Properties();
catch ( e) {
p = null;
throw new UserDirectoryException();
finally {
i = null;
} // end else
} // end UserDirectory
public static UserDirectory getInstance() throws
UserDirectoryException {
if (null==userDirectory) {
userDirectory = new UserDirectory();
return userDirectory;
public String fixId(String userId) {
return userId.toUpperCase();
public boolean isValidPassword(String userId, String password) {
// no null passwords
if (null==password) return false;
// conform userId to uppercase
String _userId = fixId(userId);
// no passwords for non-users
if (!isUserExist(_userId)) return false;
// does password match user’s password
return (password.equals(getPassword(_userId)));
public boolean isUserExist(String userId) {
// no null users
if (null==userId) return false;
// if not null, it’s a user
return !(null==p.getProperty(userId));
public String getPassword(String userId) {
return p.getProperty(userId);
public Enumeration getUserIds() {
return p.propertyNames();
public void setUser(String userId, String password) throws
UserDirectoryException {
// no nulls
if ((null==userId) || (null==password)) {
throw new UserDirectoryException();
try {
// conform userId to uppercase when stored
p.put(fixId(userId), password);
String o = this.getClass().getClassLoader().getResource(UserDirectoryFile).getFile(); FileOutputStream(o), UserDirectoryHeader);
catch (IOException e) {
throw new UserDirectoryException();

August 16, 2004 - Posted by | Struts


  1. is one of the industry’s foremost marketers of consumer products.
    Wall units today can be extensions of book shelves, TV stands and they can even function as room dividers.

    You will find a lot of hostels in Berlin to choose from at very low prices.

    Comment by Elaine | December 24, 2012 | Reply

  2. Have you ever thought about adding a little bit more than just your
    articles? I mean, what you say is valuable and everything.
    Nevertheless think about if you added some great photos or
    video clips to give your posts more, “pop”!
    Your content is excellent but with pics and videos, this website could definitely be one of the best in its field.

    Very good blog!

    Comment by reverse hotmail search | June 7, 2013 | Reply

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: