JSP Cookies 学习


1.建表

CREATE TABLE login( username     varchar2(10)
, password varchar2(10)
, passwordtype varchar2(10)
, status number(9)
);

INSERT INTO login
VALUES ('a' , 'w123' , 'm' , 1);
INSERT INTO login
VALUES ('b' , 'w123' , 's' , 1);
INSERT INTO login
VALUES ('c' , 'w123' , 'c' , 1);
INSERT INTO login
VALUES ('d' , 'w123' , 'm' , 0);
COMMIT;

2.login.html 页面

<html>
<body bgcolor="salmon">
<form name="login" action="LoginSubmit.jsp" method="get">
<table border=2>
<tr><td>User Name:</td><td><input type="text" name="username"></td></tr>
<tr><td>Password :</td><td><input type="password" name="password"></td></tr>
<tr><td colspan=2><input type="submit" name="submit" value="submit"></td></tr>
</table>
</form>
</body>
</table>

3.LoginSubmit.jsp 页面

<%@ page import ="java.sql.*"%>
<%@ include file="Dbconnection.jsp"%>
<%
MakeConnection();
try
{
user
=request.getParameter("username");
pwd1
=request.getParameter("password");
rs
= stmt.executeQuery("Select * from login WHERE username='"+user+"'");
if(rs.next()==false)
{
%>
<Center><h2>Invalid User</h2></center>
<jsp:include page="login.html"/>
<%
}
else
{
pwd2
=rs.getString(2);
passwordtype
=rs.getString(3);
int st=rs.getInt(4);

if(st==0)
{
%>
<Center><h2>Access is denied for this User</h2></center>
<jsp:include page="login.html"/>
<%
}
else
{
if(pwd2.equals(pwd1) )
{
session.setAttribute(
"username",(String)user);
if(passwordtype.equals("s"))
{
%>
<script language="javascript">
parent.top.location.href
="staffhome.jsp";
</script>
<%
}

if(passwordtype.equals("m"))
{
%>
<script language="javascript">
parent.top.location.href
="managerscreen.html";
</script>
<%
}
if(passwordtype.equals("c"))
{
%>
<script language="javascript">
parent.top.location.href
="customerscreen.html";
</script>
<%
}
}
else
{
out.println(
" <b>Wrong Password</b> "); %>
<jsp:include page="login.html"/>
<%
}
}
}
}
catch(SQLException e)
{
out.println(
"Error"+e);
}
%>

4.Dbconnection.jsp 页面

<%!
ResultSet rs;
Connection con;
Statement stmt;
String pwd1,user,pwd2,passwordtype;
public void MakeConnection()
{
try
{
Class.forName("oracle.jdbc.driver.OracleDriver");
con=DriverManager.getConnection("Jdbc:Oracle:thin:@localhost:1521:ORCL1","scott","tiger ");
stmt = con.createStatement();
}
catch(Exception e)
{
System.out.println(e);
}
}
%>

5.staffhome.jsp

<form name="staff" action="logout.jsp" method=post>
<center>
<body bgcolor="LightCyan"><h1> STAFF</h1><br>
<%
Cookie[] k
=null;
java.util.Date dt
=new java.util.Date();//current Date and time
String lastdate=dt.toString();
try
{
k
=request.getCookies(); //get All the existing cookies
for(int i=0;i<k.length;i++)
{
if((k[i].getName()).equals("staffCookie")) //search for cookie name staffCookie
{
String value = k[i].getValue();
out.println(
"<br><b> Last Login : </b>"+value);
}
}
}

catch(Exception e)
{
out.println(e);
}

out.println(
"<br><b>Curent Time: </b>"+lastdate);
Cookie ck
= new Cookie("staffCookie",lastdate); // setting a new Value to a cookie
ck.setMaxAge(
36000);
response.addCookie(ck);
%>
<br><b> You have logged in As :<%=(String)session.getAttribute("username")%></b><br>
<br>
<em>You are authorised to access Valued Customer Account</em><br>
<br><input type ="submit" name="logout" value="LogOut">
</center>
</form>

6. managerscreen.html

<html>
<body bgcolor="green">
<h2>This is the manager screen</h2>
</body>
</html>

7.customerscreen.html

<html>
<body bgcolor="magenta">
<h2>This is the customer screen</h2>
</body>
</html>

8.logout.jsp

<html>
<body bgcolor="teal">
<font size="+3">
<% out.println("You have logged off");
%>
</font>
</body>
</html>








 

posted @ 2011-12-31 15:27  天涯行客  阅读(329)  评论(0编辑  收藏  举报
天道酬勤--埋头静默--厚积薄发