package dick; import org.apache.struts.action.ActionForm; import org.apache.struts.action.ActionMapping; import javax.servlet.http.HttpServletRequest; public class UserInfoForm extends ActionForm {private String username; private String password; public String getUsername () {return (this.username);} public void setUsername (String username) {this.username = username;} public String getPassword () {return (this.password);} public void setPassword (String password) {this .password = password;}}
Loginaction.java
package dick; import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpSession; import javax.servlet.http.HttpServletResponse; import org.apache.struts .action.action; import org.apache.struts.action.actionError; import org.apache.struts.Action.Apache.struts.Action.Apache.struts.Act org.apache.struts.Action.ActionForward; Import org.apache.struts.action.ActionMapping; import org.apache.struts.action.ActionServlet; public final class LoginAction extends Action {public ActionForward execute (ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {UserInfoForm Userinfoform = (userinfoform) form; // get username and password string username = userinfoform.getusername (). Trim (); string password (); string password = userinfoform.getPassword (). Trim (); // Declarative error set object actionerrorS errors = New Act IonerrorS (); // Verification Input IF (username.equals (")) {ActionError Error = New ActionError (" Error.Missing.userName); Errors.Add (ActionerRrors.global_Error, Error);} if (Password . Equals (")) {anctionsError = new actionerror (" error.missing.password "); errors.add (actionerRORS.GLOBAL_ERROR, ERROR);} // call business logic if (errors.size () == 0 ) {String Validated = ""; try {userinfobo userinfobo = new userinfobo (); validated = userinfobo.validatePwd (username, password); if (Validated.equals ("match")) {// All Normal save user information and Turning successful page httpsession session = request.getations ();
Session.setttribute ("UserInfoForm"; Return mapping.findforward ("success");}} catch (throwable e) {// Process possible error E.PrintStackTrace (); actionerror error = new actionerror (e. GetMessage ()); errors.add (ActionerRors.global_Error, Error);}} // Turn to the input page as an error, and display the corresponding error message SaveErRors (Request, Errors); return new actionforward (mapping.getInput ()) }} Userinfobo.java
package dick; public class UserInfoBo {public UserInfoBo () {} public String validatePwd (String username, String password) {String validateResult = ""; if (username.equals ( "Dick") && password.equals ( "101")) { Validateresult = "match";} else {throw new runtimeException ("error.nomatch");} return validateresult;}}