一,session登录状态验证
1.登录界面
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<!--登录界面 -->
<form action="logonServlet.do" method="post">
<div>
账户名:<input type='text' name="oname"/>
</div>
<div>
密码:<input type="password" name="opwd"/>
</div>
<button>登录</button>
</form>
</body>
</html>
2.登录servlet程序
servlet自己需要配置web.xml哦
package com.jmh.servlet;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import com.jmh.dao.Tb_LogonDao;
import com.jmh.entity.Tb_Logon;
/**
* 登录
*
*/
public class LogonServlet extends HttpServlet{
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
this.doGet(req, resp);
}
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//设置编码
req.setCharacterEncoding("utf-8");
resp.setContentType("text/html;charset=utf-8");
//获取out、session对象
PrintWriter out = resp.getWriter();
HttpSession session = req.getSession();
//处理业务逻辑代码
//获取传入过来的值
String oname = req.getParameter("oname");
String opwd = req.getParameter("opwd");
//实例化实体类
Tb_Logon lo=new Tb_Logon(oname, opwd);
//实例化方法类
Tb_LogonDao tbl=new Tb_LogonDao();
try {
boolean logon = tbl.Logon(lo);
if(logon) {//登录成功
//登录状态:登录成功后把用户名传入给session作用域
session.setAttribute("oname", oname);
req.getRequestDispatcher("登录成功后要跳转的页面").forward(req, resp);
}else {//登录失败
out.print("<script>alert('登录失败!');location.href='登录失败后要跳转的页面'</script>");
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
3.session判断登录状态
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<!--判断登录状态 -->
<%
String str=(String)session.getAttribute("oname");
if(null==str){//没有登录
out.print("<script>alert('请先登录!');location.href='自动跳转到登录界面';</script>");
}
%>
</body>
</html>
4.跳转指定页面session判断登录状态(登录过就可以访问/反之)
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<!--判断登录状态 引入判断登录session状态页面-->
<%@ include file="sessionLogon.jsp" %>
<!-- 主页 -->
<h1>登录成功!</h1>
</body>
</html>
二,Cookie登录状态验证
1.登录界面
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<!--登录界面 -->
<form action="logonServlet.do" method="post">
<div>
账户名:<input type='text' name="oname"/>
</div>
<div>
密码:<input type="password" name="opwd"/>
</div>
<button>登录</button>
</form>
</body>
</html>
2.登录servlet程序
servlet自己需要配置web.xml哦
package com.jmh.servlet;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;
import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import com.jmh.dao.Tb_LogonDao;
import com.jmh.entity.Tb_Logon;
/**
* 登录
*
*/
public class LogonServlet extends HttpServlet{
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
this.doGet(req, resp);
}
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//设置编码
req.setCharacterEncoding("utf-8");
resp.setContentType("text/html;charset=utf-8");
//获取out、session对象
PrintWriter out = resp.getWriter();
HttpSession session = req.getSession();
//处理业务逻辑代码
//获取传入过来的值
String oname = req.getParameter("oname");
String opwd = req.getParameter("opwd");
//实例化实体类
Tb_Logon lo=new Tb_Logon(oname, opwd);
//实例化方法类
Tb_LogonDao tbl=new Tb_LogonDao();
try {
boolean logon = tbl.Logon(lo);
if(logon) {//登录成功
//使用Cookie
Cookie ck=new Cookie("oname", oname);
resp.addCookie(ck);//将Cookie对象输出客户端
resp.sendRedirect("登录成功后跳转指定的页面");
}else {//登录失败
out.print("<script>alert('登录失败!');location.href='登录成功后跳转指定的页面'</script>");
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
3.Cookie判断登录状态
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<!--Cookie登录判断验证 -->
<%
boolean f=false;
//获取所有Cookie
Cookie[] ck= request.getCookies();
//遍历Cookie
for(Cookie c : ck){
//非空判断
if(null!=c){
if(c.getName().equals("oname")){
f=true;//已经登录
break;
}
}
}
if(f==false){
out.print("<script>alert('请先登录!');location.href='没有登录就自动跳转到登录界面';</script>");
}
%>
</body>
</html>
4.跳转指定页面Cookie判断登录状态(登录过就可以访问/反之)
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<!--判断登录状态 引入判断登录Cookie状态页面-->
<%@ include file="cookieLogon.jsp" %>
<!-- 主页 -->
<h1>登录成功!</h1>
</body>
</html>