<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    Kimi's NutShell

    我荒廢的今日,正是昨日殞身之人祈求的明日

    BlogJava 新隨筆 管理
      141 Posts :: 0 Stories :: 75 Comments :: 0 Trackbacks
    這次涉及到四個文件:
    登錄頁面:login.html
    登錄成功歡迎頁面:login_success.jsp
    登錄失敗頁面:login_failure.jsp
    Servlet處理文件:LoginServlet.java
    其實還涉及到一個文件:web.xml,這個后面再說:
    下面分別介紹這幾個文件:
    登錄頁面:login.html

    <!-- 該Login頁面是一個簡單的登錄界面 -->
    <!--
    該JSP程序是用來測試與MySQL數據庫的連接,
    需要一個數據庫:LearnJSP,和其中一個表:userinfo
    表中有兩個字段分別為:UserName varchar (20) not null,UserPwd varchar (20) not null
    -->

    < html >
    ??<head>
    ????<title>登錄</title>
    ????<meta http-equiv="content-type"content="text/html; charset=UTF-8">
    ????<meta http-equiv="Content-Language"content="ch-cn">
    ??</head>
    ??<body>
    <!-- Form 用來提取用戶填入并提交的信息-->
    <formmethod="post"name="frmLogin"action="LoginServlet">
    ???<h1align="center">用戶登錄</h1><br>
    ???<divalign="center">用戶名:
    ??????<inputtype="text"name="txtUserName"value="Your name"
    ???????size="20"maxlength="20"
    ???????onfocus="if(this.value=='Your name')this.value='';"><br>密碼:
    ??????<inputtype="password"name="txtPassword"value="Your password"
    ???????size="20"maxlength="20"
    ???????onfocus="if(this.value=='Your password')this.value='';"><br>
    ??????<inputtype="submit"name="Submit" value="提交"onClick="validateLogin();">
    ???????&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
    ??????<inputtype="reset"name="Reset"value="重置"><br>
    ???</div>
    </form>
    <!-- javaScript 函數 validateLogin(),用來驗證用戶名和密碼是否為空 -->
    ????<scriptlanguage="javaScript">
    ????? function validateLogin()
    ?????{
    ??????var sUserName = document.frmLogin.txtUserName.value;
    ??????var sPassword = document.frmLogin.txtPassword.value;
    ?????? if( sUserName=="")
    ??????{
    ??????? alert("請輸入用戶名!");
    ??????? return false;
    ??????}
    ?????? if( sPassword=="")
    ??????{
    ??????? alert("請輸入密碼!");
    ??????? return false;
    ??????}
    ?????}
    ????</script>
    ??</body>
    </html>

    登錄成功歡迎頁面:login_success.jsp

    < % @ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
    <html>
    <head>
    ??<title>My JSP 'login_failure.jsp' starting page</title>
    ??<meta http-equiv="content-type"content="text/html; charset=UTF-8">
    ??<meta http-equiv="pragma"content="no-cache">
    ??<meta http-equiv="cache-control"content="no-cache">
    ??<meta http-equiv="expires"content="0">
    ??<meta http-equiv="keywords"content="keyword1,keyword2,keyword3">
    ??<meta http-equiv="description"content="This is my page">
    ??<!--
    <link rel="stylesheet" type="text/css" href="styles.css">
    -->

    </head>
    <body>
    ??<%
    ???String userName =(String)session.getAttribute ("UserName");
    ??%>
    ??<divalign=center>
    ???<%=userName%>
    ??? 歡迎您,登錄成功!
    ??</div>
    </body>
    </html>

    登錄失敗頁面:login_failure.jsp

    < % @ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
    <html>
    <head>
    ??<title>My JSP 'login_failure.jsp' starting page</title>
    ??<meta http-equiv="content-type"content="text/html; charset=UTF-8">
    ??<meta http-equiv="pragma"content="no-cache">
    ??<meta http-equiv="cache-control"content="no-cache">
    ??<meta http-equiv="expires"content="0">
    ??<meta http-equiv="keywords"content="keyword1,keyword2,keyword3">
    ??<meta http-equiv="description"content="This is my page">
    ??<!--
    <link rel="stylesheet" type="text/css" href="styles.css">
    -->

    </head>
    <body>
    ??<%
    ??String userName =(String)session.getAttribute ("UserName");
    ??%>
    ??<divalign=center>
    ???<%=userName%>
    ??? 對不起,登錄失敗!
    ??</div>
    </body>
    </html>

    Servlet處理文件:LoginServlet.java

    /**
    * 該JSP程序是用來測試與MySQL數據庫的連接,
    * 需要一個數據庫:LearnJSP,和其中一個表:userinfo
    * 表中有兩個字段分別為:UserName varchar (20) not null,UserPwd varchar (20) not null
    */

    package zieckey.login.servlet;
    importjava.sql.Statement;
    importjava.io.IOException;
    importjava.sql.DriverManager;
    importjava.sql.ResultSet;
    importjava.sql.SQLException;
    importjavax.servlet.Servlet;
    importjavax.servlet.ServletException;
    importjavax.servlet.http.HttpServlet;
    importjavax.servlet.http.HttpServletRequest;
    importjavax.servlet.http.HttpServletResponse;
    publicclass LoginServlet extends HttpServlet implements Servlet
    {
    public LoginServlet ()
    {
    ??
    // TODO Auto-generated constructor stub

    }
    /*
    ?? * (non-Javadoc)
    ?? *
    ?? * @see javax.servlet.http.HttpServlet#doGet(javax.servlet.http.HttpServletRequest,
    ?? * javax.servlet.http.HttpServletResponse)
    ?? */

    @Override
    protectedvoid doGet ( HttpServletRequest arg0, HttpServletResponse arg1 )
    ???throws ServletException,IOException
    {
    }
    /*
    ?? * (non-Javadoc)
    ?? *
    ?? * @see javax.servlet.http.HttpServlet#doPost(javax.servlet.http.HttpServletRequest,
    ?? * javax.servlet.http.HttpServletResponse)
    ?? */

    @Override
    protectedvoid doPost ( HttpServletRequest request, HttpServletResponse response )
    ???throws ServletException,IOException
    {
    ?? response.setContentType("text/html");
    ??Stringresult="";
    ??
    // 獲取用戶名

    ??String sUserName =request.getParameter("txtUserName");
    ??if( sUserName ==""|| sUserName ==null|| sUserName.length()> 20 )
    ??{
    ???try
    ???{
    ????result="請輸入用戶名(不超過20字符)!";
    ????request.setAttribute("ErrorUserName",result);
    ???? response.sendRedirect ("login.html");
    ???}catch(Exception e )
    ???{
    ???}
    ??}
    ??
    // 獲取密碼

    ??String sPasswd =request.getParameter("txtPassword");
    ??if( sPasswd ==""|| sPasswd ==null|| sPasswd.length()> 20 )
    ??{
    ???try
    ???{
    ????result="請輸入密碼(不超過20字符)!";
    ????request.setAttribute("ErrorPassword",result);
    ???? response.sendRedirect ("login.html");
    ???}catch(Exception e )
    ???{
    ???}
    ??}
    ??
    // 登記JDBC驅動程序

    ??try
    ??{
    ???Class.forName("org.gjt.mm.mysql.Driver").newInstance();
    ??}catch(InstantiationException e )
    ??{
    ???
    // TODO Auto-generated catch block

    ??? e.printStackTrace();
    ???System.out.println("InstantiationException");
    ??}catch(IllegalAccessException e )
    ??{
    ???
    // TODO Auto-generated catch block

    ??? e.printStackTrace();
    ???System.out.println("IllegalAccessException");
    ??}catch(ClassNotFoundException e )
    ??{
    ???
    // TODO Auto-generated catch block

    ??? e.printStackTrace();
    ???System.out.println("ClassNotFoundException");
    ??}
    ??
    // 連接參數與Access不同

    ??Stringurl="jdbc:mysql://localhost/LearnJSP";
    ??
    // 建立連接

    ??java.sql.Connectionconnection=null;
    ??Statement stmt =null;
    ??ResultSet rs =null;
    ??try
    ??{
    ???connection=DriverManager.getConnection(url,"root","011124");
    ??? stmt =connection.createStatement();
    ???
    // SQL語句

    ???Stringsql="select * from userinfo where username='"+ sUserName
    ?????+"' and userpwd = '"+ sPasswd +"'";
    ??? rs = stmt.executeQuery(sql);
    // 返回查詢結果

    ??}catch(SQLException e )
    ??{
    ???
    // TODO Auto-generated catch block

    ??? e.printStackTrace();
    ??}
    ??try
    ??{
    ???if( rs.next())
    // 如果記錄集非空,表明有匹配的用戶名和密碼,登陸成功

    ???{
    ????
    // 登錄成功后將sUserName設置為session變量的UserName

    ????
    // 這樣在后面就可以通過 session.getAttribute("UserName") 來獲取用戶名,

    ????
    // 同時這樣還可以作為用戶登錄與否的判斷依據

    ????request.getSession().setAttribute("UserName", sUserName );
    ???? response.sendRedirect ("login_success.jsp");
    ???}else
    ???{
    ????
    // 否則登錄失敗

    ????
    //response.sendRedirect ( "MyJsp.jsp" );

    ???? response.sendRedirect ("login_failure.jsp");
    ???}
    ??}catch(SQLException e )
    ??{
    ???
    // TODO Auto-generated catch block

    ??? e.printStackTrace();
    ??}
    ??try
    ??{
    ???if( null!=rs )
    ???{
    ???? rs.close();
    ???}
    ???if( null!=stmt )
    ???{
    ???? stmt.close();
    ???}
    ???if( null!=connection )
    ???{
    ????connection.close();
    ???}
    ??}catch(SQLException e )
    ??{
    ???
    // TODO Auto-generated catch block

    ??? e.printStackTrace();
    ??}
    }
    /**
    ?? *
    ?? */

    private static final long serialVersionUID = 1L;
    }

    為了讓這個網站正常運行還要到web.xml中注冊一下,
    現該文件內容修改如下:

    < ?xml version="1.0"encoding="UTF-8"?>
    <web-app xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"version="2.4" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
    ??<servlet>
    ????<display-name>LoginServlet</display-name>
    ????<servlet-name>LoginServlet</servlet-name>
    ????<servlet-class>zieckey.login.servlet.LoginServlet</servlet-class>
    ??</servlet>
    ??<servlet-mapping>
    ????<servlet-name>LoginServlet</servlet-name>
    ????<url-pattern>/LoginServlet</url-pattern>
    ??</servlet-mapping>
    </web-app>

    好了,這幾個文件就可以構成我們的這個登錄界面了.
    注意事項:
    1. 文件目錄形式
    login.html,login_success.html,login_failure.html這三個文件放在同一目錄,
    LoginServlet.java該文件的字節碼文件LoginServlet.class放在WEB-INF\classes目錄下(注意jar包順序)
    現在整個工程的目錄形式是:
    M:\Tomcat5.5\webapps\JSP_Servlet_JavaBean_Login 的目錄
    007-01-18 15:16??? <DIR>????????? META-INF
    007-01-18 15:16??? <DIR>????????? WEB-INF
    007-01-18 16:17???????????? 1,801 login.html
    007-01-18 15:48?????????????? 858 login_failure.jsp
    007-01-18 15:40?????????????? 234 login_success.html
    007-01-18 15:46?????????????? 781 MyJsp.jsp
    007-01-18 16:12?????????????? 859 login_success.jsp
    M:\Tomcat5.5\webapps\JSP_Servlet_JavaBean_Login\WEB-INF 的目錄
    007-01-18 15:16??? <DIR>????????? classes
    007-01-18 15:16??? <DIR>????????? lib
    007-01-18 16:21?????????????? 606 web.xml
    M:\Tomcat5.5\webapps\JSP_Servlet_JavaBean_Login\WEB-INF\classes\zieckey\login\servlet 的目錄
    2007-01-18 16:18???????????? 3,900 LoginServlet.class
    2. 其他注意事項
    數據庫MySQL服務器程序要先啟動起來.
    posted on 2008-02-28 14:39 Kimi 閱讀(2001) 評論(3)  編輯  收藏 所屬分類: Java

    評論

    # re: MVC簡單模式: 登錄 (轉) 2011-02-22 11:24 vghv
    m mkllmkmk;  回復  更多評論
      

    # re: MVC簡單模式: 登錄 (轉) 2011-02-22 11:25 vghv
    怎么運行不成功?  回復  更多評論
      

    # re: MVC簡單模式: 登錄 (轉)[未登錄] 2011-02-22 11:43 nick
    不會啊,是不是你哪里沒有配置好?@vghv
      回復  更多評論
      

    主站蜘蛛池模板: 成人毛片免费视频| 免费国产成人午夜电影| 免费成人福利视频| 又黄又爽一线毛片免费观看| 亚洲午夜无码久久久久小说| 国产高潮久久免费观看| 最近免费中文字幕大全免费版视频| 在线观看免费毛片| 久久精品亚洲中文字幕无码网站 | 激情无码亚洲一区二区三区| 最近免费中文字幕MV在线视频3 | 日韩亚洲人成在线综合| 国产精品国产午夜免费福利看| 亚洲第一成年男人的天堂| 在线观看国产一区亚洲bd| 日韩视频免费在线观看| 成人亚洲网站www在线观看| 亚洲国产综合在线| 久久免费视频精品| 亚洲成aⅴ人在线观看| 国产精品视频永久免费播放| 精品亚洲A∨无码一区二区三区| 一出一进一爽一粗一大视频免费的 | 亚洲а∨天堂久久精品9966| 免费高清国产视频| 亚洲精品成人a在线观看| 亚洲日韩一区二区三区| 国产男女爽爽爽爽爽免费视频| 亚洲国产精品无码专区影院| 久久亚洲AV成人无码国产最大| 毛片a级毛片免费播放下载| 国产亚洲精品欧洲在线观看| 久久久久亚洲av毛片大| 一道本在线免费视频| 亚洲VA中文字幕无码毛片| www.999精品视频观看免费| 亚洲综合激情九月婷婷| 久久免费观看国产99精品| 亚洲国产午夜精品理论片| 五月亭亭免费高清在线| 亚洲成a∧人片在线观看无码|