?? loginservlet.java
字號(hào):
package testserv;
/**
* 用servlet登陸程序
*/
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.util.*;
import java.sql.*;
public class loginServlet extends HttpServlet {
static final private String CONTENT_TYPE = "text/html; charset=GBK";
Connection connection; // 創(chuàng)建連接對(duì)象
Vector userinfo; // 定義向量存入個(gè)人注冊(cè)信息
Vector userbaseinfo; // 定義向量存入用戶(hù)名及密碼
public void init() throws ServletException {
connection = null;
userinfo = new Vector();
userbaseinfo = new Vector();
}
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doPost(request, response);
}
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String name = request.getParameter("username"); // 獲得登陸用戶(hù)名
String password = request.getParameter("password"); // 獲得登陸密碼
response.setContentType(CONTENT_TYPE);
PrintWriter out = response.getWriter();
out.println("<html>");
out.println("<head><title>登錄界面</title></head>");
out.println("<body>");
boolean loginning= false;
if ((loginning=login(name, password))==true) // 判斷數(shù)據(jù)庫(kù)中是否有用戶(hù)名
{
boolean right_pass = validate(password); // 檢測(cè)用戶(hù)密碼是否正確
if (right_pass)
{
getUserInfo(name, password); // 調(diào)用個(gè)人注冊(cè)信息,分別賦值給字符串變量
String truename = String.valueOf(userinfo.elementAt(0));
String age = String.valueOf(userinfo.elementAt(1));
String sex = String.valueOf(userinfo.elementAt(2));
String mail = String.valueOf(userinfo.elementAt(3));
String tel = String.valueOf(userinfo.elementAt(4));
// 顯示個(gè)人注冊(cè)信息
out.println("<p align=center>用戶(hù)名:" + name + "</p>");
out.println("<p align=center>用戶(hù)姓名:" + truename + "</p>");
out.println("<p align=center>年齡:" + age + "</p>");
out.println("<p align=center>性別:" + sex + "</p>");
out.println("<p align=center>郵件地址:" + mail + "</p>");
out.println("<p align=center>電話(huà):" + tel + "</p>");
}
else // 處理密碼不正確情況
{
out.println("密碼不正確!<br>");
out.println("<p align=center><a href='login.htm'>重新登錄</a></p>");
}
}
else // 處理用戶(hù)名不存在情況
{
out.println("用戶(hù)名不存在!<br>");
out.println("<p align=center><a href='login.htm'>重新登錄</a></p>");
}
out.println("</body></html>");
}
public void destroy() {
}
public boolean login(String name, String password)
{
boolean loginning= false;
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); // 實(shí)例化JDBC-ODBC橋的驅(qū)動(dòng)
String url = "jdbc:odbc:login"; // 設(shè)置連接字符串
connection = DriverManager.getConnection(url); // 連接數(shù)據(jù)庫(kù)
Statement statement = connection.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_READ_ONLY);
String sql = "select * from userbase where user_name = '" + name + "'";
ResultSet resultset = statement.executeQuery(sql);
int recordCount = 0;
while(resultset.next())
{
recordCount++;
}
if (recordCount>0) // 判斷數(shù)據(jù)庫(kù)表userbase中是否有用戶(hù)信息
{
userbaseinfo.clear();
resultset.first();
userbaseinfo.addElement(resultset.getString("user_name")); // 將個(gè)人信息存入向量對(duì)象userbase_Vec中
userbaseinfo.addElement(resultset.getString("user_password"));
loginning= true;
}
}
catch(Exception ex)
{
ex.printStackTrace();
}// 捕捉異常
return loginning;
}
public void getUserInfo(String name, String password)
{
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); // 實(shí)例化JDBC-ODBC橋的驅(qū)動(dòng)
String url = "jdbc:odbc:login"; // 設(shè)置連接字符串
connection = DriverManager.getConnection(url); // 連接數(shù)據(jù)庫(kù)
// 創(chuàng)建Statement接口對(duì)象
Statement statement = connection.createStatement();
String sql = "select * from userinfo where user_name = '" + name + "' and user_password = '" + password + "'";
ResultSet resultset = statement.executeQuery(sql);
resultset.next();
userinfo.clear();
// 將個(gè)人注冊(cè)信息存入向量用戶(hù)信息中
userinfo.addElement(resultset.getString("user_truename"));
userinfo.addElement(resultset.getString("user_age"));
userinfo.addElement(resultset.getString("user_sex"));
userinfo.addElement(resultset.getString("user_mail"));
userinfo.addElement(resultset.getString("user_tel"));
}
catch(Exception ex) // 捕捉異常
{
ex.printStackTrace();
}
}
public boolean validate(String password) // 定義驗(yàn)證密碼函數(shù)
{
boolean pass = false;
if (password.equals(String.valueOf(userbaseinfo.elementAt(1))))
{
pass = true;
}
return pass;
}
}
?? 快捷鍵說(shuō)明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -