/**************************************************
*Author:Java619
*Time:2006-03-25
*E-Mail:ceun@163.com
*QQ:395676370
**************************************************/
登陆是我们在实际应用中经常用到的,我在这边举个简单的登陆例子,作为抛砖引玉吧!
程序结构如下:
采用JSP+Servlet+JavaBean
1.数据库结构(为简便这边采用access,实际应用中建议采用其他数据库如MySQL,MSSQL等)
==============================
uname 用户名 文本型
pword 密码 文本型
初始数据uname :ceun pword :123
==============================
2.视图(JSP)
<%
...
@page contentType="text/html"
%>
<% ... @page pageEncoding="UTF-8" %>
< html >
< head >
< meta http-equiv ="Content-Type" content ="text/html; charset=UTF-8" >
< title > 登陆 </ title >
</ head >
< body >
< center >< br >< br >< br >
< p >< form action ="<%=request.getContextPath ()%>/CheckServlet" method ="post" >
< table width ="259" border ="1" cellpadding ="0" cellspacing ="0" bordercolor ="#0099FF" >
< tr align ="center" >
< td height ="20" colspan ="2" >< span class ="style1" > 登陆 </ span ></ td >
</ tr >
< tr >
< td width ="50" height ="20" > 用户名 </ td >
< td width ="161" align ="left" >< input name ="uname" type ="text" id ="uname" size ="19" ></ td >
</ tr >
< tr >
< td height ="20" > 密码 </ td >
< td align ="left" >< input name ="pword" type ="password" id ="pword" size ="20" ></ td >
</ tr >
< tr align ="center" >
< td colspan ="2" >< input type ="submit" name ="Submit" value ="提交" >
< input type ="reset" name ="Submit" value ="重置" ></ td >
</ tr >
</ table ></ form ></ p >
</ center >
</ body >
</ html >
<% ... @page pageEncoding="UTF-8" %>
< html >
< head >
< meta http-equiv ="Content-Type" content ="text/html; charset=UTF-8" >
< title > 登陆 </ title >
</ head >
< body >
< center >< br >< br >< br >
< p >< form action ="<%=request.getContextPath ()%>/CheckServlet" method ="post" >
< table width ="259" border ="1" cellpadding ="0" cellspacing ="0" bordercolor ="#0099FF" >
< tr align ="center" >
< td height ="20" colspan ="2" >< span class ="style1" > 登陆 </ span ></ td >
</ tr >
< tr >
< td width ="50" height ="20" > 用户名 </ td >
< td width ="161" align ="left" >< input name ="uname" type ="text" id ="uname" size ="19" ></ td >
</ tr >
< tr >
< td height ="20" > 密码 </ td >
< td align ="left" >< input name ="pword" type ="password" id ="pword" size ="20" ></ td >
</ tr >
< tr align ="center" >
< td colspan ="2" >< input type ="submit" name ="Submit" value ="提交" >
< input type ="reset" name ="Submit" value ="重置" ></ td >
</ tr >
</ table ></ form ></ p >
</ center >
</ body >
</ html >
3.Servlet
/**/
/*
* CheckServlet.java
*@author Java619
* Created on 2006年3月25日, 下午6:05
*/
package com.ceun;
import java.io. * ;
import java.net. * ;
import javax.servlet. * ;
import javax.servlet.http. * ;
import com.ceun.bean.UserBean;
/** */ /**
*
* @author ceun
* @version
*/
public class CheckServlet extends HttpServlet ... {
/** *//** Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
* @param request servlet request
* @param response servlet response
*/
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException ...{
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
String name=request.getParameter("uname");
String pword=request.getParameter("pword");
out.println("<br><br><br><hr><center><font color=red size=12><B>");
try...{
UserBean user=new UserBean();
if(user.check(name,pword))
out.println("登陆成功");
else
out.println("登陆失败");
}catch(Exception e)...{
}
out.println("</B></font></center>");
out.close();
}
// <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
/** *//** Handles the HTTP <code>GET</code> method.
* @param request servlet request
* @param response servlet response
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException ...{
processRequest(request, response);
}
/** *//** Handles the HTTP <code>POST</code> method.
* @param request servlet request
* @param response servlet response
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException ...{
processRequest(request, response);
}
/** *//** Returns a short description of the servlet.
*/
public String getServletInfo() ...{
return "Short description";
}
// </editor-fold>
}
* CheckServlet.java
*@author Java619
* Created on 2006年3月25日, 下午6:05
*/
package com.ceun;
import java.io. * ;
import java.net. * ;
import javax.servlet. * ;
import javax.servlet.http. * ;
import com.ceun.bean.UserBean;
/** */ /**
*
* @author ceun
* @version
*/
public class CheckServlet extends HttpServlet ... {
/** *//** Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
* @param request servlet request
* @param response servlet response
*/
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException ...{
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
String name=request.getParameter("uname");
String pword=request.getParameter("pword");
out.println("<br><br><br><hr><center><font color=red size=12><B>");
try...{
UserBean user=new UserBean();
if(user.check(name,pword))
out.println("登陆成功");
else
out.println("登陆失败");
}catch(Exception e)...{
}
out.println("</B></font></center>");
out.close();
}
// <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
/** *//** Handles the HTTP <code>GET</code> method.
* @param request servlet request
* @param response servlet response
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException ...{
processRequest(request, response);
}
/** *//** Handles the HTTP <code>POST</code> method.
* @param request servlet request
* @param response servlet response
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException ...{
processRequest(request, response);
}
/** *//** Returns a short description of the servlet.
*/
public String getServletInfo() ...{
return "Short description";
}
// </editor-fold>
}
4.JavaBean
/**/
/*
* UserBean.java
*@author Java619
* Created on 2006年3月25日, 下午6:22
*
* To change this template, choose Tools | Template Manager
* and open the template in the editor.
*/
package com.ceun.bean;
import java.sql. * ;
import com.ceun.util.DbUtil;
/**/ /**
*
* @author ceun
*/
public class UserBean ... {
/**//** Creates a new instance of UserBean */
public UserBean() ...{
}
public boolean check(String username,String password)
throws Exception...{
Connection con= DbUtil.connectToDb();
Statement stmt=null;
try...{
String sql="SELECT * FROM loginInfo "+
" WHERE uname='"+username+"' and pword='"+password+"'";
stmt=con.createStatement();
ResultSet rs=stmt.executeQuery(sql);
if(rs.next()) return true;
}
catch(Exception ex)
...{
}finally...{
try...{
stmt.close();
con.close();
}catch(Exception e)...{e.printStackTrace();}
}
return false;
}
}
* UserBean.java
*@author Java619
* Created on 2006年3月25日, 下午6:22
*
* To change this template, choose Tools | Template Manager
* and open the template in the editor.
*/
package com.ceun.bean;
import java.sql. * ;
import com.ceun.util.DbUtil;
/**/ /**
*
* @author ceun
*/
public class UserBean ... {
/**//** Creates a new instance of UserBean */
public UserBean() ...{
}
public boolean check(String username,String password)
throws Exception...{
Connection con= DbUtil.connectToDb();
Statement stmt=null;
try...{
String sql="SELECT * FROM loginInfo "+
" WHERE uname='"+username+"' and pword='"+password+"'";
stmt=con.createStatement();
ResultSet rs=stmt.executeQuery(sql);
if(rs.next()) return true;
}
catch(Exception ex)
...{
}finally...{
try...{
stmt.close();
con.close();
}catch(Exception e)...{e.printStackTrace();}
}
return false;
}
}
5.实用类(用于连接数据库)
/**/
/*
* DbUtil.java
*
* Created on 2006年3月25日, 下午6:20
*@author Java619
* To change this template, choose Tools | Template Manager
* and open the template in the editor.
*/
package com.ceun.util;
import java.io. * ;
import java.sql. * ;
/**/ /**
* <strong>DbUtil</strong> is a utility class to create
* a connection to our sample database.
*/
public class DbUtil ... {
static String driverName="sun.jdbc.odbc.JdbcOdbcDriver";
static String dbUrl="jdbc:odbc:";
public DbUtil() ...{
}
public static java.sql.Connection connectToDb(String databaseName)throws Exception
...{
Connection connection=null;
String connName = dbUrl+databaseName;
Class.forName(driverName).newInstance();
connection = DriverManager.getConnection(connName);
return connection;
}
public static java.sql.Connection connectToDb()throws Exception...{
return (connectToDb("logInfo"));
}
}
* DbUtil.java
*
* Created on 2006年3月25日, 下午6:20
*@author Java619
* To change this template, choose Tools | Template Manager
* and open the template in the editor.
*/
package com.ceun.util;
import java.io. * ;
import java.sql. * ;
/**/ /**
* <strong>DbUtil</strong> is a utility class to create
* a connection to our sample database.
*/
public class DbUtil ... {
static String driverName="sun.jdbc.odbc.JdbcOdbcDriver";
static String dbUrl="jdbc:odbc:";
public DbUtil() ...{
}
public static java.sql.Connection connectToDb(String databaseName)throws Exception
...{
Connection connection=null;
String connName = dbUrl+databaseName;
Class.forName(driverName).newInstance();
connection = DriverManager.getConnection(connName);
return connection;
}
public static java.sql.Connection connectToDb()throws Exception...{
return (connectToDb("logInfo"));
}
}
6.应用配置文件
<?
xml version="1.0" encoding="UTF-8"
?>
< web-app version ="2.4" xmlns ="http://java.sun.com/xml/ns/j2ee" xmlns:xsi ="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation ="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd" >
< servlet >
< servlet-name > CheckServlet </ servlet-name >
< servlet-class > com.ceun.CheckServlet </ servlet-class >
</ servlet >
< servlet-mapping >
< servlet-name > CheckServlet </ servlet-name >
< url-pattern > /CheckServlet </ url-pattern >
</ servlet-mapping >
< session-config >
< session-timeout >
30
</ session-timeout >
</ session-config >
< welcome-file-list >
< welcome-file >
index.jsp
</ welcome-file >
</ welcome-file-list >
</ web-app >
< web-app version ="2.4" xmlns ="http://java.sun.com/xml/ns/j2ee" xmlns:xsi ="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation ="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd" >
< servlet >
< servlet-name > CheckServlet </ servlet-name >
< servlet-class > com.ceun.CheckServlet </ servlet-class >
</ servlet >
< servlet-mapping >
< servlet-name > CheckServlet </ servlet-name >
< url-pattern > /CheckServlet </ url-pattern >
</ servlet-mapping >
< session-config >
< session-timeout >
30
</ session-timeout >
</ session-config >
< welcome-file-list >
< welcome-file >
index.jsp
</ welcome-file >
</ welcome-file-list >
</ web-app >
到这边一个简单的登陆实例就做好了,你现在可以放在服务器下测试下。