Jsp登陆以及失效时间

aaa.jsp

<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
<%@ page import="java.sql.*,java.util.*" %>
<!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>Java正则表达式</title>
</head>
<body>

 <form id="loginform" action="bbb.jsp" method="post">
 用户名:
 <input type="text" name="username"/>
 <br/>
 密码:
 <input type="password" name="password"/>
 <br/>
 <input type="submit" value="提交"/>
 </form>

</body>
</html>



bbb.jsp


<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
<%@ page import="java.util.regex.*,java.sql.*"%>
<!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>Java正则表达式</title>
</head>
<body>
<%
		
	String  username= request.getParameter("username");
	String  password= request.getParameter("password");
	if(null==username||"".equals(username)){
		out.println("用户名不能为空");
		return;
	}
	if(null==password||"".equals(password)){
		out.println("密码不能为空");
		return;
	}
	
	 Connection conn=null;
	 PreparedStatement ps=null;
	 ResultSet rs=null;
	 try{
			 Class.forName("org.gjt.mm.mysql.Driver").newInstance();  
			 String url ="jdbc:mysql://localhost/admin?user=root&password=root";
			 //testDB为你的数据库名  
			 conn= DriverManager.getConnection(url); 
			 String sql="select * from  login where id=1 order by createtime desc";  
			 ps=conn.prepareStatement(sql); 
			 rs=ps.executeQuery();
			 String name=null;
			 String pass=null;
			 while(rs.next()){
					name=rs.getString("username");
					pass=rs.getString("password");
			 }
			 if(username.equals(name)&&pass.equals(password)){
			 	session.setAttribute("adminlogin",username);
			 	session.setAttribute("adminpass",password);
			 }
	 		response.sendRedirect("page.jsp");
	 }catch(Exception e){
	 	e.printStackTrace();
	 }finally{
	 	 ps.close();  
	 	conn.close();  
	 }
	
	
	
	
	

%>
</body>
</html>




page.jsp

<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
    <%@ page import="java.sql.*,java.util.*,java.util.regex.Matcher,java.util.regex.Pattern" %>
<!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>Mysql Page</title>
</head>
<body>
<%
		session.setMaxInactiveInterval(1800);
		if(null==session.getAttribute("adminlogin")||"".equals(session.getAttribute("adminlogin"))){
			response.sendRedirect("aaa.jsp");
		}
		request.getLocalAddr();
		session.getAttribute("adminlogin");
		out.println("欢迎"+session.getAttribute("adminlogin"));
		out.println("<br/>");
		out.println("欢迎"+request.getLocalAddr());
		out.println("<br/>");
		out.println("欢迎"+request.getCookies());
		out.println("<br/>");

 %>
 <%
 	Connection conn=null;
	 PreparedStatement ps=null;
	 ResultSet rs=null;
	 try{
			 Class.forName("org.gjt.mm.mysql.Driver").newInstance();  
			 String url ="jdbc:mysql://localhost/admin?user=root&password=root";
			 //testDB为你的数据库名  
			 conn= DriverManager.getConnection(url); 
			 String sql="select * from  test order by createtime desc";  
			 ps=conn.prepareStatement(sql); 
			 rs=ps.executeQuery();
			 String name=null;
			 String pass=null;
			 while(rs.next()){
					out.println("<a href='update.jsp'>"+rs.getString("content")+"</a>");
					out.println("<br/>");
					out.println(rs.getString("company"));
					out.println("<br/>");
					out.println(rs.getString("comployee"));
					out.println("<br/>");
			 }
	 }catch(Exception e){
	 	e.printStackTrace();
	 }finally{
	 	 ps.close();  
	 	conn.close();  
	 }
 
  %>
下面是操作页面:
<br/>
<a href="add.jsp">增加</a>
<br/>
<a href="logout.jsp">注销</a>






</body>
</html>



logout.jsp


<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
    <%@ page import="java.sql.*,java.util.*,java.util.regex.Matcher,java.util.regex.Pattern" %>

<%
		session.getAttribute("adminlogin");
		session.removeAttribute("adminlogin");
		if(null==session.getAttribute("adminlogin")||"".equals(session.getAttribute("adminlogin"))){
			response.sendRedirect("aaa.jsp");
		}else{
			response.sendRedirect("logout.jsp");
		}

 %>
 

你可能感兴趣的:(java,sql,mysql,jsp,正则表达式)