loginaction.java

来自「tomcat与java web 这本书的配套源代码」· Java 代码 · 共 57 行

JAVA
57
字号
package mypack;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

public class LoginAction extends Action {

  protected String checkUser(String username, String password) {

    String user = null;

    // You would normally do some real User lookup here, but
    // for this example we will have only one valid username "swq"
    if ( username.equals("swq") && password.equals("1234") ) {
      user = new String("Sun weiqin");
    }
    return user;
  }

	public ActionForward execute(ActionMapping mapping,
		ActionForm form,
		HttpServletRequest request,
		HttpServletResponse response)
		throws IOException, ServletException {

    String user = null;

		// Default target to success
    String target = new String("success");

    // Use the LoginForm to get the request parameters
	  String username = ((LoginForm)form).getUsername();
	  String password = ((LoginForm)form).getPassword();

    user = checkUser(username, password);

    // Set the target to failure
    if ( user == null ) {
      ((LoginForm)form).setUsername("username");
      ((LoginForm)form).setPassword("password");
      target = new String("failure");
    }
    else {

      request.setAttribute("USER", user);
    }
	  // Forward to the appropriate View
	  return (mapping.findForward(target));
	}
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?