checklogin.java

来自「这是所有的关于java初学者的学习的源代码」· Java 代码 · 共 48 行

JAVA
48
字号
//checkLogin.java
package beans;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;

public class checkLogin {
	public String username = "";

	public boolean check(HttpServletRequest req, HttpServletResponse res)
		throws IOException, ServletException {
		String cookieName = "username";
		PrintWriter out = res.getWriter();
		try {
			Cookie[] myCookies = req.getCookies();
			this.username =
				this.getCookieValue(myCookies, cookieName, "not found");
		} 
		catch (Exception e) {
			return false;
		}

		if (!this.username.equals(new String("not found"))) {
			return true;
		} 
		else {
			return false;
		}

	}

	public String getUserName() {
		return this.username;
	}

	public static String getCookieValue(
		Cookie[] cookies,
		String cookieName,
		String defaultValue) {
		for (int i = 0; i < cookies.length; i++) {
			Cookie cookie = cookies[i];
			if (cookieName.equals(cookie.getName()))
				return (cookie.getValue());
		}
		return (defaultValue);
	}
}

⌨️ 快捷键说明

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