Servlet案例5:使用者登入失敗資訊回顯,servlet回顯
登入失敗資訊回顯不會的新的一個頁面,而是顯示在登入頁面
一種方法是:
登入頁面表單中每個欄位後添加<span>標籤寫入失敗資訊,利用ajax技術
通過改變<span>標籤的display:none屬性添加失敗資訊
這裡用的類似的方法:
資料庫準備略,
注意寫好對應的user類供BeanHandler使用
sevlet:
package login;import java.io.IOException;import java.sql.SQLException;import javax.servlet.ServletException;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import org.apache.commons.dbutils.QueryRunner;import org.apache.commons.dbutils.handlers.BeanHandler;public class LoginServlet extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { request.setCharacterEncoding("UTF-8"); // 獲得使用者名稱密碼 String username = request.getParameter("username"); String password = request.getParameter("password"); // 在資料庫中查詢 User login = null; try { login = login(username, password); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } if (login != null) { // 成功,跳轉首頁 response.sendRedirect(request.getContextPath()); } else { // 錯誤 // 在request域中存入錯誤資訊 request.setAttribute("loginInfo", "使用者名稱或密碼錯誤"); request.getRequestDispatcher("/login.jsp").forward(request, response); } } public User login(String username, String password) throws SQLException { QueryRunner runner = new QueryRunner(DataSourceUtils.getDataSource()); String sql = "select * from user where username=? and password=?"; User user = runner.query(sql, new BeanHandler<User>(User.class), username, password); return user; } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); }}View Code
抽取串連池類:
package login;import java.sql.Connection;import java.sql.ResultSet;import java.sql.SQLException;import java.sql.Statement;import javax.sql.DataSource;import com.mchange.v2.c3p0.ComboPooledDataSource;public class DataSourceUtils { private static DataSource dataSource = new ComboPooledDataSource(); private static ThreadLocal<Connection> tl = new ThreadLocal<Connection>(); // 直接可以擷取一個串連池 public static DataSource getDataSource() { return dataSource; } // 擷取連線物件 public static Connection getConnection() throws SQLException { Connection con = tl.get(); if (con == null) { con = dataSource.getConnection(); tl.set(con); } return con; } // 開啟事務 public static void startTransaction() throws SQLException { Connection con = getConnection(); if (con != null) { con.setAutoCommit(false); } } // 交易回復 public static void rollback() throws SQLException { Connection con = getConnection(); if (con != null) { con.rollback(); } } // 提交並且 關閉資源及從ThreadLocall中釋放 public static void commitAndRelease() throws SQLException { Connection con = getConnection(); if (con != null) { con.commit(); // 事務提交 con.close();// 關閉資源 tl.remove();// 從線程綁定中移除 } } // 關閉資源方法 public static void closeConnection() throws SQLException { Connection con = getConnection(); if (con != null) { con.close(); } } public static void closeStatement(Statement st) throws SQLException { if (st != null) { st.close(); } } public static void closeResultSet(ResultSet rs) throws SQLException { if (rs != null) { rs.close(); } }}View Code
注意寫好c3p0-config.xml、web.xml設定檔
在登入頁面加上這一句代碼
<div><%=request.getAttribute("loginInfo")==null?"":request.getAttribute("loginInfo")%></div>
即可實現簡單的資訊回顯