本文實(shí)例講述了JSP+Servlet+JavaBean實(shí)現(xiàn)登錄網(wǎng)頁的方法。分享給大家供大家參考。具體如下:
這里涉及到四個(gè)文件:
1. 登錄頁面:login.html
2. 登錄成功歡迎頁面:login_success.jsp
3. 登錄失敗頁面:login_failure.jsp
4. Servlet處理文件:LoginServlet.java
其實(shí)還涉及到一個(gè)文件:web.xml,這個(gè)后面再說:
下面分別介紹這幾個(gè)文件:
1. 登錄頁面:login.html
!-- 該Login頁面是一個(gè)簡單的登錄界面 -->
!--
該JSP程序是用來測(cè)試與MySQL數(shù)據(jù)庫的連接,
需要一個(gè)數(shù)據(jù)庫:LearnJSP,和其中一個(gè)表:userinfo
表中有兩個(gè)字段分別為: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 用來提取用戶填入并提交的信息-->
form method="post" name="frmLogin" action="LoginServlet">
h1 align="center">用戶登錄/h1>br>
div align="center">用戶名:
input type="text" name="txtUserName" value="Your name"
size="20" maxlength="20"
onfocus="if(this.value=='Your name')this.value='';">br>密碼:
input type="password" name="txtPassword" value="Your password"
size="20" maxlength="20"
onfocus="if(this.value=='Your password')this.value='';">br>
input type="submit" name="Submit" value="提交" onClick="validateLogin();" >
nbsp;nbsp;nbsp;nbsp;nbsp;
input type="reset" name="Reset" value="重置">br>
/div>
/form>
!-- javaScript 函數(shù) validateLogin(),用來驗(yàn)證用戶名和密碼是否為空 -->
script language="javaScript">
function validateLogin()
{
var sUserName = document.frmLogin.txtUserName.value;
var sPassword = document.frmLogin.txtPassword.value;
if( sUserName=="" )
{
alert("請(qǐng)輸入用戶名!");
return false;
}
if( sPassword=="" )
{
alert("請(qǐng)輸入密碼!");
return false;
}
}
/script>
/body>
/html>
2. 登錄成功歡迎頁面: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" );
%>
div align=center>
%=userName%>
歡迎您,登錄成功!
/div>
/body>
/html>
3. 登錄失敗頁面: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" );
%>
div align=center>
%=userName%>
對(duì)不起,登錄失?。?
/div>
/body>
/html>
4. Servlet處理文件:LoginServlet.java
/**
* 該JSP程序是用來測(cè)試與MySQL數(shù)據(jù)庫的連接,
* 需要一個(gè)數(shù)據(jù)庫:LearnJSP,和其中一個(gè)表:userinfo
* 表中有兩個(gè)字段分別為:UserName varchar (20) not null,UserPwd varchar (20) not null
*/
package zieckey.login.servlet;
import java.sql.Statement;
import java.io.IOException;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import javax.servlet.Servlet;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class 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
protected void 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
protected void doPost ( HttpServletRequest request, HttpServletResponse response )
throws ServletException, IOException
{
response.setContentType ( "text/html" );
String result = "";
// 獲取用戶名
String sUserName = request.getParameter ( "txtUserName" );
if ( sUserName == "" || sUserName == null || sUserName.length ( ) > 20 )
{
try
{
result = "請(qǐng)輸入用戶名(不超過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 = "請(qǐng)輸入密碼(不超過20字符)!";
request.setAttribute ( "ErrorPassword", result );
response.sendRedirect ( "login.html" );
} catch ( Exception e )
{
}
}
// 登記JDBC驅(qū)動(dòng)程序
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");
}
// 連接參數(shù)與Access不同
String url = "jdbc:mysql://localhost/LearnJSP";
// 建立連接
java.sql.Connection connection = null;
Statement stmt = null;
ResultSet rs = null;
try
{
connection = DriverManager.getConnection ( url, "root", "011124" );
stmt = connection.createStatement ( );
// SQL語句
String sql = "select * from userinfo where username='" + sUserName
+ "' and userpwd = '" + sPasswd + "'";
rs = stmt.executeQuery ( sql );// 返回查詢結(jié)果
} catch ( SQLException e )
{
// TODO Auto-generated catch block
e.printStackTrace ( );
}
try
{
if ( rs.next ( ) )// 如果記錄集非空,表明有匹配的用戶名和密碼,登陸成功
{
// 登錄成功后將sUserName設(shè)置為session變量的UserName
// 這樣在后面就可以通過 session.getAttribute("UserName") 來獲取用戶名,
// 同時(shí)這樣還可以作為用戶登錄與否的判斷依據(jù)
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;
}
為了讓這個(gè)網(wǎng)站正常運(yùn)行還要到web.xml中注冊(cè)一下,
現(xiàn)該文件內(nèi)容修改如下:
?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>
好了,這幾個(gè)文件就可以構(gòu)成我們的這個(gè)登錄界面了.
注意事項(xiàng):
1. 文件目錄形式
login.html,login_success.html,login_failure.html這三個(gè)文件放在同一目錄,
LoginServlet.java該文件的字節(jié)碼文件LoginServlet.class放在WEB-INF/classes目錄下(注意jar包順序)
現(xiàn)在整個(gè)工程的目錄形式是:
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. 其他注意事項(xiàng)
數(shù)據(jù)庫MySQL服務(wù)器程序要先啟動(dòng)起來.
希望本文所述對(duì)大家的JSP程序設(shè)計(jì)有所幫助。
您可能感興趣的文章:- JavaWeb實(shí)現(xiàn)用戶登錄注冊(cè)功能實(shí)例代碼(基于Servlet+JSP+JavaBean模式)
- JSP+Servlet制作Java Web登錄功能的全流程解析
- java中servlet實(shí)現(xiàn)登錄驗(yàn)證的方法
- Servlet+JavaBean+JSP打造Java Web注冊(cè)與登錄功能
- JSP + Servlet實(shí)現(xiàn)生成登錄驗(yàn)證碼示例
- 在jsp中用bean和servlet聯(lián)合實(shí)現(xiàn)用戶注冊(cè)、登錄
- servlet實(shí)現(xiàn)用戶登錄小程序
- servlet+jsp實(shí)現(xiàn)過濾器 防止用戶未登錄訪問
- 使用Java servlet實(shí)現(xiàn)自動(dòng)登錄退出功能
- Servlet簡單實(shí)現(xiàn)登錄功能