2011年11月7日 星期一

saru的程式回家作業壹


=====================《p6》=====================

<%@ page contentType="text/html; charset=Big5"%>
<HTML>
<HEAD>
<TITLE>學生成績查詢系統</TITLE>
</HEAD>
<BODY>
<CENTER>
<FONT SIZE = 5 COLOR = blue>學生成績查詢系統</FONT>
</CENTER>

<%

String slog = (String) session.getAttribute( "slog" );

if(slog != null && slog.equals("t"))
{
response.sendRedirect("p6-1.jsp");
}

%>

<HR>
<P></P>
<!-- 下面的表單將以Post方法, 將資料傳遞給Response.jsp檔 -->
<FORM action="p6-1.jsp" method=post name=FORM1>
<P>學號:<INPUT name=num ></P>
<P>密碼:<INPUT name=pwd ></P>
<INPUT name=submit1 type=submit value=送出>
<INPUT name=submit1 type=reset value=重填>
</FORM>

</BODY>
</HTML>

=====================《p6-1》=====================

<%@ page contentType="text/html; charset=Big5"
            import="java.util.Date, java.io.*"%>
<HTML>
<HEAD>
<TITLE>-系統登入-</TITLE>
</HEAD>
<BODY>

<HR>
<P></P>

<%

Cookie coo = null;
Date STime = new Date();
int cnt = 1;

Cookie[] cookies = request.getCookies(); //取得Cookie變數陣列 //out.println(cookies.toString());

if(cookies != null)
{
int cookielen = cookies.length;
//取得Cookie變數陣列的長度
for(int i=0; i < cookielen; i++)
{
coo = cookies[i]; //將Cookie變數陣列中的元素設定給Time

if (coo.getName().equals("logintime"))
{
STime = new Date(Long.parseLong(cookies[i].getValue()));
}else if (coo.getName().equals("cnt"))
{
cnt = Integer.parseInt(cookies[i].getValue());
}
}
}
request.setCharacterEncoding("big5");

String path2 = request.getRequestURI();
int    end1  = path2.lastIndexOf('/') + 1;
String webdir = path2.substring(0, end1) ;
String path  = request.getRealPath(webdir);

FileReader fr = new FileReader(path + "\\score.dat"); //建立FileReader物件,並設定由fr物件變數引用
BufferedReader br = new BufferedReader(fr); //建立BufferedReader物件, 並設定由br物件變數引用
String Line = br.readLine(); //自檔案讀取一行字串

String num = request.getParameter( "num" );
String pwd = request.getParameter( "pwd" );

if(num != null)
{
   session.setAttribute( "snum", num );
   session.setAttribute( "spwd", pwd );
}

String snum = (String) session.getAttribute( "snum" );
String spwd = (String) session.getAttribute( "spwd" );

boolean pair = false;
String slog = (String) session.getAttribute( "slog" );
String[] token ={"","","","","","",""};

if(slog == null || slog.equals("f"))
{
while(Line != null && pair==false)
{
token = Line.split(" ");
if(token[0].equals(snum) && token[1].equals(spwd))
{
pair=true;
}
Line = br.readLine(); //自檔案讀取一行資料
    }

if(pair)
{
out.print("學 號:" + snum + "<br>");
out.print("登入成功" + "<br>");
out.print("國文" + token[3] + "<br>");
out.print("英文" + token[4] + "<br>");
out.print("數學" + token[5] + "<br>");
out.print("平均" + (Integer.parseInt(token[3])+Integer.parseInt(token[4])+Integer.parseInt(token[5]))/3 + "<br>");
  %><br><a href="p6.jsp">回登入畫面</a><%
session.setAttribute( "slog", "t" );
out.print("登入次數:" + cnt + "<br>");
if (cnt != 1)
{
out.print("上次登入時間:" + STime + "<br>");
}
Cookie abc = new Cookie("cnt", String.valueOf(cnt+1));
abc.setMaxAge(604800);
response.addCookie(abc);
response.addCookie(new Cookie("logintime", String.valueOf(STime.getTime())));
}
else 
{
out.print("學 號:" + snum + "<br>");
out.print("噗噗~登入錯誤" + "<br>");
session.setAttribute( "slog", "f" );
%><br><a href="p6.jsp">回登入畫面</a><%
}
}
else
{

while(Line != null && pair==false)
{
token = Line.split(" ");
if(token[0].equals(snum) && token[1].equals(spwd))
{
pair=true;
}
Line = br.readLine(); //自檔案讀取一行資料
}
out.print("學 號:" + snum + "<br>");
out.print("登入成功" + "<br>");
out.print("國文" + token[3] + "<br>");
out.print("英文" + token[4] + "<br>");
out.print("數學" + token[5] + "<br>");
out.print("平均" + (Integer.parseInt(token[3])+Integer.parseInt(token[4])+Integer.parseInt(token[5]))/3 + "<br>");
  %><br><a href="p6.jsp">回登入畫面</a><%
session.setAttribute( "slog", "t" );
out.print("登入次數:" + cnt + "<br>");
if (cnt != 1)
{
out.print("上次登入時間:" + STime + "<br>");
}
Cookie abc = new Cookie("cnt", String.valueOf(cnt+1));
abc.setMaxAge(604800);
response.addCookie(abc);
response.addCookie(new Cookie("logintime", String.valueOf(STime.getTime())));
}

br.close(); //關閉BufferedReader物件
fr.close(); //關閉檔案

%>

</BODY>
</HTML>

沒有留言:

張貼留言