2013年9月14日 星期六

JSP-javaBean、setProperty演練:改寫登入流程

登入流程:login.jsp--->check.jsp--->ok.jsp或fail.jsp
 check.jsp使用ChkLogin.java這支程式。

 login.jsp
<%@ page contentType="text/html; charset=utf-8" language="java" import="java.sql.*" errorPage="" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">

<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>無標題文件</title>
</head>

<body>

<form id="form1" name="form1" method="post" action="check.jsp">
  <p>
    <label for="username">帳號:</label>
    <input type="text" name="username" id="username" value="${cookie.username.value}"/>
  </p>
  <p>密碼:
    <input type="password" name="password" id="password" />
  </p>
  <p>
    <input type="submit" name="button" id="button" value="送出" />
  </p>
</form>
</body>
</html>


check.jsp
<%@ page contentType="text/html; charset=utf-8" language="java" import="java.sql.*" errorPage="" %>
<jsp:useBean id="objCheck" scope="page" class="t0915.ChkLogin" />
<jsp:setProperty name="objCheck" property="userName" param="username" />
<jsp:setProperty name="objCheck" property="pwd" param="password" />
<%

if (objCheck.check())
{
    session.setAttribute("login", "ok");
    session.setAttribute("username", objCheck.getUserName());
   
    Cookie myCookie = new Cookie("username", objCheck.getUserName());
    myCookie.setMaxAge(60);
    response.addCookie(myCookie);
   
    response.sendRedirect("ok.jsp");
}
else
{
    response.sendRedirect("fail.jsp");
}
%>



ChkLogin.java
package t0915;

public class ChkLogin {
    private String name;
    private String pwd;
   
    public ChkLogin()
    {
       
    }
   
    public void setUserName(String u)
    {
        name = u;
    }
   
    public String getUserName()
    {
        return name;
    }
   
    public void setPwd(String p)
    {
        pwd = p;
    }
   
    public String getPwd()
    {
        return pwd;
    }
   
    public boolean check()
    {
        if (name.equals("abc") && pwd.equals("123"))               
            return true;
        else
            return false;
    }
}

沒有留言:

張貼留言