2013年9月11日 星期三

登入帳號密碼-傳統JSP網頁寫法。

建立4個檔案
使用者登入畫面login.jsp-->判斷check.jsp-->ok.jsp或fail.jsp

 login.jsp
<body>
<form id="form1" name="form1" method="post" action="/0910/check.jsp">
  <p>
    <label for="username">姓名:</label>  //使用label可以讓使用者點到姓名時,游標直接到姓名的文字框
    <input type="text" name="username" id="username" />
  </p>
  <p>
    <label for="pwd">  密碼:</label>
 
    <input type="password" name="pwd" id="pwd" />
  </p>
  <p>
    <input type="submit" name="button" id="button" value="送出" />
  </p>
</form>
</body>
-------------------------------------------------------------------------------------------------------------
check.jsp
<%@ page contentType="text/html; charset=utf-8" language="java" import="java.sql.*" errorPage="" %>

<%
request.setCharacterEncoding("utf-8");

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

if(username.equals("")||pwd.equals("")){
     response.sendRedirect("fail.jsp");
}else{
    session.setAttribute("login","ok");
    response.sendRedirect("ok.jsp");
}
%>

-----------------------------------------------------------------------------------------------------------------
ok.jsp

<%
String susername=(String)session.getAttribute("login");
if(susername==null){
response.sendRedirect("login.jsp");
}
%>



fail.jsp就隨自己要給使用著甚麼資訊。

沒有留言:

張貼留言