Servlet case 5: User Logon Failure Information echo, servlet echo
Logon failure information is displayed on the logon page instead of a new page.
One method is:
Add the <span> tag write failure information after each field in the form on the login page, using ajax technology
Add Failure Information by changing the display: none attribute of the <span> label
A similar method is used here:
Database preparation,
Note: Write the corresponding user class for 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 doG Et (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {request. setCharacterEncoding ("UTF-8"); // obtain the username and password String username = request. getParameter ("username"); String password = request. getParameter ("password"); // query User login = null in the database; try {login = login (username, password);} catch (SQLException e) {// TODO Auto-generated catch block e. printStackT Race ();} if (login! = Null) {// success, jump to the home page response. sendRedirect (request. getContextPath ();} else {// error // Save the error message request in the request domain. setAttribute ("loginInfo", "incorrect user name or password"); 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 u Ser 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
Extraction connection pool class:
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 performanceutils {private static DataSource dataSource = new ComboPooledDataSource (); private static ThreadLocal <Connection> tl = new ThreadLocal <Connection> (); // you can directly obtain a connection pool public static DataSource getDataSource () {return dataSource;} // gets the Connection object public static Connection getConnection () throws SQLException {Connection con = tl. get (); if (con = null) {con = dataSource. getConnection (); tl. set (con) ;}return con ;}// enable the public static void startTransaction () throws SQLException {Connection con = getConnection (); if (con! = Null) {con. setAutoCommit (false) ;}// transaction rollback public static void rollback () throws SQLException {Connection con = getConnection (); if (con! = Null) {con. rollback () ;}/// submit and close resources and release public static void commitAndRelease () throws SQLException {Connection con = getConnection (); if (con! = Null) {con. commit (); // transaction commit con. close (); // close the resource tl. remove (); // remove from thread binding} // close the resource method 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
Be sure to write the c3p0-config.xml, web. xml configuration file
Add this code on the logon page.
<div><%=request.getAttribute("loginInfo")==null?"":request.getAttribute("loginInfo")%></div>
You can achieve simple information echo.