系統架構圖如下:
三個檔案如下:
//登入介面
/** *//**
* @(#)Login.java
*
*
* @author shiyi05
* @version 1.00 2008/5/10
*/
package com.rao;
import javax.servlet.http.*;
import java.io.*;
public class Login extends HttpServlet
{
public Login()
{
}
//處理Get請求
public void doGet(HttpServletRequest req,HttpServletResponse res)
{
try
{
//中文亂碼
res.setContentType("text/html; charset=gbk");
PrintWriter pw=res.getWriter();
//返回登陸介面
pw.println("<html>");
pw.println("<body>");
pw.println("<h1>登陸介面</h1>");
pw.println("<form action=LoginCl method=post>");
pw.println("使用者名稱: <input type=text name=username><br>");
pw.println("密碼: <input type=password name=passwd><br>");
pw.println("<input type=submit value=loing><br>");
pw.println("</body>");
pw.println("</html>");
}
catch(Exception ex)
{
ex.printStackTrace();
}
}
public void doPost(HttpServletRequest req,HttpServletResponse res)
{
this.doGet(req,res);
}
}
//使用者驗證
/** *//**
* @(#)LoginCl.java
*
*
* @author shiyi05
* @version 1.00 2008/5/10
*/
package com.rao;
import javax.servlet.http.*;
import java.io.*;
public class LoginCl extends HttpServlet
{
public LoginCl()
{
}
//處理Get請求
public void doGet(HttpServletRequest req,HttpServletResponse res)
{
try
{
//中文亂碼
res.setContentType("text/html; charset=gbk");
//接收使用者名稱和密碼
String u=req.getParameter("username");
String p=req.getParameter("passwd");
//驗證
if(u.equals("sp") && p.equals("123"))
{
//合法
res.sendRedirect("Welcome");
}
else
{
//不合法
res.sendRedirect("Login");
}
}
catch(Exception ex)
{
ex.printStackTrace();
}
}
public void doPost(HttpServletRequest req,HttpServletResponse res)
{
this.doGet(req,res);
}
}
//歡迎介面
/** *//** *//** *//**
* @(#)Welcome.java
*
*
* @author shiyi05
* @version 1.00 2008/5/10
*/
package com.rao;
import javax.servlet.http.*;
import java.io.*;
public class Welcome extends HttpServlet
{
public Welcome()
{
}
//處理Get請求
public void doGet(HttpServletRequest req,HttpServletResponse res)
{
try
{
//中文亂碼
res.setContentType("text/html; charset=gbk");
PrintWriter pw=res.getWriter();
//返回登陸介面
pw.println("<html>");
pw.println("<body>");
pw.println("<h1>Welcome</h1>");
pw.println("</body>");
pw.println("</html>");
}
catch(Exception ex)
{
ex.printStackTrace();
}
}
public void doPost(HttpServletRequest req,HttpServletResponse res)
{
this.doGet(req,res);
}
}