Android Java Web實現登入註冊(二)——伺服器端
阿新 • • 發佈:2019-02-19
Eclipse新建Dynamic Web Project,新建index.html,執行看見沒有提示404錯誤的頁面即Tomcat伺服器部署成功。這是搭建Java Web環境時需要完成的工作。詳細內容請看點選開啟連結
涉及的檔案與專案結構:
一、Servlet類
接收客戶端發來的請求,(Post方法),呼叫Service類中的方法來實現功能。
程式碼:LoginServlet.java
RegisterrServlet類和LoginServlet類基本相同,不同在於取/傳資料和幾個變數名,就省略了public class LoginServlet extends HttpServlet { private static final long serialVersionUID = 1L; static int LOGIN_FAILED = 0; static int LOGIN_SUCCEEDED = 1; public LoginServlet() { super(); } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doPost(request, response); response.getWriter().append("Served at: ").append(request.getContextPath()); } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // 返回字串 String responseMsg="FAILED"; // 輸出流 PrintWriter out = response.getWriter(); // 設定編碼形式 request.setCharacterEncoding("utf-8"); // 獲取傳入資料 String id = request.getParameter("id"); String password = request.getParameter("password"); System.out.println("id:" + id + " --try to login"); // 訪問資料庫 int value = Service.login(id, password); if(value == LOGIN_SUCCEEDED) { responseMsg = "SUCCEEDED"; } System.out.println("login servlet responseMsg:" + responseMsg); out.print(responseMsg); } }
用到的兩個Servlet都要在web.xml中如下注冊一下:
<servlet> <servlet-name>servlet.LoginServlet</servlet-name> <servlet-class> servlet.LoginServlet </servlet-class> </servlet> <servlet-mapping> <servlet-name>servlet.LoginServlet</servlet-name> <url-pattern>/LoginServlet</url-pattern> </servlet-mapping>
二、Service類
處理Servlet傳來的資料並執行相應sql語句。
程式碼:MyService.java
public class MyService { static PreparedStatement preparedStatement = null; static ResultSet resultSet = null; static int updateRowCnt = 0; static int LOGIN_FAILED = 0; static int LOGIN_SUCCEEDED = 1; static int REGISTER_FAILED = 2; static int REGISTER_SUCCEEDED = 3; public static int login(String id, String password) { int result = LOGIN_FAILED; resultSet = null; // 執行 SQL 查詢語句 String sql = "select * from user_list where id='" + id +"'"; try { Connection con = DBManager.getConnection(); preparedStatement = con.prepareStatement(sql); try{ resultSet = preparedStatement.executeQuery(); // 查詢結果 if(resultSet.next()){ if(resultSet.getString("password").equals(password)){ result = LOGIN_SUCCEEDED; System.out.println("id:" + id + " username:" + resultSet.getString("username") + " --login"); } } preparedStatement.close(); con.close(); }catch(Exception e){ e.printStackTrace(); } }catch(Exception e){ e.printStackTrace(); } System.out.println("login service result:" + result); return result; } public static int register(String id, String username, String password) { int result = REGISTER_FAILED; updateRowCnt = 0; // 執行 SQL 插入語句 String sql = "insert into user_list(`id`, `username`,`password`) values ('" + id + "', '" + username + "', '" + password + "')"; try { Connection con = DBManager.getConnection(); preparedStatement = con.prepareStatement(sql); try{ updateRowCnt = preparedStatement.executeUpdate(); // 插入結果 if(updateRowCnt != 0){ result = REGISTER_SUCCEEDED; System.out.println("id:" + id + " username:" + resultSet.getString("username") + " --register"); } preparedStatement.close(); con.close(); }catch(Exception e){ e.printStackTrace(); } }catch(Exception e){ e.printStackTrace(); } System.out.println("register service result:" + result); return result; } }
三、DBManager類
被Service類呼叫來連線資料庫,包括說明連線的資料庫的地址、使用者名稱和密碼。
public class DBManager {
// 載入的驅動
private static final String DRIVER_CLASS = "com.mysql.jdbc.Driver";
// 資料庫URL
private static final String DATABASE_URL = "jdbc:mysql://localhost:3306/db_test?useUnicode=true&characterEncoding=utf8";
// 資料庫連線使用者名稱
private static final String DATABASE_USERNAME = "admin";
// 資料庫連線密碼
private static final String DATEBASE_PASSWORD = "123456";
public static Connection getConnection() throws SQLException {
try {
Class.forName(DRIVER_CLASS);
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
Connection con = DriverManager.getConnection(DATABASE_URL,DATABASE_USERNAME, DATEBASE_PASSWORD);
return con;
}
}
最後,連線資料庫和執行sql語句成功的前提是:已經MySQl中建立相應資料庫和表格
在左下角SCHEMAS欄中可以看到建立資料庫的情況,右鍵表格名->Select Rows可以查看錶格內容
至此,伺服器端的工作也完成了。
程式碼下載:
客戶端 http://download.csdn.net/detail/qq_24833289/9620063