1.开启Eclipse,单击Flie→New→Project..出现New Project对话框如下图。
选择MyEclipse→J2EE Projects→Web Project单击Next如下图。
在Project Name文本框中写入logindemo其它保持默认选项单击Finish按钮。此时在Package Explorer(包浏览器)中出现我们新建的项目,把项目展开在src文件夹上单击右键选择New→Package出现New Java Package对话框如下图
在Name文本框中写入register单击Finish按钮。在src文件夹中新建了一个register包,用同样的方法在src文件夹中建一个login包。右键单击register包选择New→Class出现New Java Class对话框如下图
在Name文本框中填写Register单击Finish按钮。在register包下生成Register.java文件,更改其内容如下:
packageregister;
importjava.io.*;
importjava.util.*;
importjavax.servlet.*;
importjavax.servlet.http.*;
importjava.sql.*;
//import com.mysql.jdbc.*;publicclassRegisterextendsHttpServlet
...{
privateString name;
privateString pass;
privateString confirm;
publicsynchronizedvoiddoPost(HttpServletRequest request,HttpServletResponse response)
throwsServletException,IOException
...{
this.name=request.getParameter("account");
this.pass=request.getParameter("password");
this.confirm=request.getParameter("confirm");
PrintWriter out=response.getWriter();
//驱动程序名String driverName="com.mysql.jdbc.Driver";
//数据库用户名String userName="root";
//密码String userPasswd="123";
//数据库名String dbName="database";
//表名String tableName="users";
//联结字符串String url="jdbc:mysql://localhost/"+dbName+"?user="+userName+"&password="+userPasswd;
try
...{
Class.forName("org.gjt.mm.mysql.Driver");
Connection connection=DriverManager.getConnection(url);
Statement statement=connection.createStatement();
String sql="SELECT * FROM"+tableName;
ResultSet rs=statement.executeQuery(sql);
while(true)
...{
if(rs.next())
...{
if(this.name.equals(rs.getString(1)))
...{
out.print("
"+"User Name is in Database!");
break;
} }else
...{
sql="INSERT INTO"+tableName+"VALUES('"+this.name+"','"+this.pass+"')";
statement.execute(sql);
out.print("
"+"Register Successful!");
break;
} }return;
}catch(SQLException e)
...{
}catch(ClassNotFoundException e)
...{
}
}
publicsynchronizedvoiddoGet(HttpServletRequest request,HttpServletResponse response)
throwsServletException,IOException
...{
doPost(request,response);
}}
用同样的方法在login包下建一个Login.java文件,其内容如下:
packagelogin;
importjava.io.*;
importjava.util.*;
importjavax.servlet.*;
importjavax.servlet.http.*;
importjava.sql.*;
//import com.mysql.jdbc.*;publicclassLoginextendsHttpServlet
...{
privateString name;
privateString pass;
publicsynchronizedvoiddoPost(HttpServletRequest request,HttpServletResponse response)
throwsServletException,IOException
...{
this.name=request.getParameter("account");
this.pass=request.getParameter("password");
PrintWriter out=response.getWriter();
//驱动程序名String driverName="com.mysql.jdbc.Driver";
//数据库用户名String userName="root";
//密码String userPasswd="123";
//数据库名String dbName="database";
//表名String tableName="users";
//联结字符串String url="jdbc:mysql://localhost/"+dbName+"?user="+userName+"&password="+userPasswd;
try
...{
Class.forName("org.gjt.mm.mysql.Driver");
Connection connection=DriverManager.getConnection(url);
Statement statement=connection.createStatement();
String sql="SELECT * FROM"+tableName;
ResultSet rs=statement.executeQuery(sql);
while(true)
...{
if(rs.next())
...{
if((this.name.equals(rs.getString(1)))&&(this.pass.equals(rs.getString(2))))
...{
out.print("
"+"Login Success!");
break;
} }else
...{
out.print("
"+"Account or Password Is Invalid!");
break;
} }return;
}catch(SQLException e)
...{
}catch(ClassNotFoundException e)
...{
}
}
publicsynchronizedvoiddoGet(HttpServletRequest request,HttpServletResponse response)
throwsServletException,IOException
...{
doPost(request,response);
}}
在WebRoot文件夹上单击右键点击New→JSP出现Create a new JSP page对话框如下图
在File Name文本框中填写index.jsp单击Finish按钮。更改其内容如下:
...functionlogin_click()
...{
form1.action="Login";
form1.submit();
}
functionregister_click()
...{
form1.action="register.jsp";
form1.submit();
}
Account:
Password: