?? appletlogin.java
字號:
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
public class AppletLogin extends HttpServlet
{
public static String USER_KEY = "ServletLogin.user";
static java.util.Hashtable crossRef;
public void doGet(HttpServletRequest req,
HttpServletResponse resp)
throws ServletException, java.io.IOException
{
// 設(shè)置輸出類型
resp.setContentType("text/html");
// 定義個PrinterWriter變量來輸出內(nèi)容
java.io.PrintWriter out = resp.getWriter();
// 設(shè)置響應頭
// 強制頁面從server端輸出
// 而不是從緩存(cache)中輸出
resp.setHeader("Expires", "Tues, 01 Jan 1980 00:00:00 GMT");
// 通過session獲取當前用戶
// 通過Form獲取用戶和密碼
HttpSession session = req.getSession(true);
String user = (String) session.getValue(USER_KEY);
if (user == null) {
// 檢查交叉引用表
user = (String) crossRef.get(session.getId());
if (user != null) {
// 在表中發(fā)現(xiàn)用戶
// 于是把用戶存入session中
// 同時從交叉引用表中刪除
session.putValue(USER_KEY, user);
crossRef.remove(session.getId());
}
}
if (user == null) {
// 如果沒有發(fā)現(xiàn)用戶,則顯示初始的登錄界面
login(out, req);
return;
}
// 打印標準頭
//顯示登錄成功的信息
out.println("<html>");
out.println("<head>");
out.println("<title>歡迎光臨</title>");
out.println("</head>");
out.println("<body>");
out.println("<center><h2>登錄成功!</h2>");
out.println("<br>");
out.println("</center>");
out.println("</body>");
out.println("</html>");
out.flush();
}
public void doPost(HttpServletRequest req,
HttpServletResponse resp)
throws ServletException, java.io.IOException
{
// 得到輸入流,用來讀取客戶端的數(shù)據(jù)
DataInputStream in =
new DataInputStream(req.getInputStream());
// 把二進制送回客戶端,
// 需要正確設(shè)置content-type的值
resp.setContentType("application/octet-stream");
// 數(shù)據(jù)必須寫入一個字節(jié)緩沖區(qū)中
// 這樣我們就可以告訴客戶端數(shù)據(jù)的長度
ByteArrayOutputStream byteOut = new ByteArrayOutputStream();
// 創(chuàng)建一個輸出流,
// 把數(shù)據(jù)寫入緩沖區(qū)
DataOutputStream out = new DataOutputStream(byteOut);
// 讀出正確的session
String sessionId = in.readUTF();
// 讀取用戶名和密碼
String user = in.readUTF();
String password = in.readUTF();
// 驗證用戶身份
if (validUser(user, password)) {
// 返回一個boolean類型的值
// 代表用戶是否合法
out.writeBoolean(false);
}
else {
// 如果用戶合法,設(shè)置相應的交叉引用表
// 同時返回一個boolean值表示用戶合法
crossRef.put(sessionId, user);
out.writeBoolean(true);
// 輸出下一個將要顯示的頁面的URL
out.writeUTF("http://10.9.41.41:8080/star" + req.getRequestURI());
}
// 把輸出強制寫入緩沖區(qū)
out.flush();
// 獲得保存返回內(nèi)容的緩沖區(qū)地址
byte[] buf = byteOut.toByteArray();
// 通知客戶端發(fā)送的數(shù)據(jù)是多少
resp.setContentLength(buf.length);
// 把緩沖區(qū)內(nèi)容輸出到客戶端
ServletOutputStream servletOut = resp.getOutputStream();
servletOut.write(buf);
servletOut.close();
}
//下面是登錄頁面,包含了LoginApplet
protected void login(java.io.PrintWriter out,
HttpServletRequest req)
throws java.io.IOException
{
HttpSession session = req.getSession(true);
out.println("<html>");
out.println("<head>");
out.println("<title>Login</title>");
out.println("<center><h2>歡迎光臨,請您先登錄</h2>");
out.println("<applet width=200 height=120");
out.println(" name=\"LoginApplet\"");
out.println(" codebase=\"http://10.9.41.41:8080/star\"");
out.println(" code=\"LoginApplet\">");
out.println("<param name=\"servlet\" value=\"" +
req.getRequestURI() + "\">");
out.println("<param name=\"id\" value=\"" +
session.getId() + "\">");
out.println("</applet>");
out.println("</center></body></html>");
}
//下面這個函數(shù)用來驗證用戶是否合法
protected boolean validUser(String username, String password)
{
boolean valid = false;
// 對用戶身份進行簡單認證
if ((username != null) && (username.length() > 0)) {
valid = username.equals(password);
}
return valid;
}
//重載servlet的初始化函數(shù)
public void init(ServletConfig cfg)
throws ServletException
{
// 創(chuàng)建一個交叉引用表(哈希表)
if (crossRef == null) {
crossRef = new java.util.Hashtable();
}
super.init(cfg);
}
public void destroy()
{
super.destroy();
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -