⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 loginaction.java

📁 tomcat与java web 这本书的配套源代码
💻 JAVA
字号:
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 + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -