模擬用戶登錄功能
阿新 • • 發佈:2018-01-20
href sheet ima handlers from right tchar for getc
1 html部分代碼
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html> <html> <head> <meta name="viewport" content="width=device-width, initial-scale=1"> <title>會員登錄</title> <link rel="stylesheet" href="css/bootstrap.min.css" type="text/css" /> <script src="js/jquery-1.11.3.min.js" type="text/javascript"></script> <script src="js/bootstrap.min.js" type="text/javascript"></script> <!-- 引入自定義css文件 style.css --> <link rel="stylesheet" href="css/style.css" type="text/css"/> <style> body { margin-top: 20px; margin: 0 auto; } .carousel-inner .item img { width: 100%; height: 300px; } .container .row div { /* position:relative; float:left; */ } font { color: #666; font-size: 22px; font-weight: normal; padding-right: 17px; } </style> </head> <body> <!-- 引入header.jsp --> <jsp:include page="/header.jsp"></jsp:include> <div class="container" style="width: 100%; height: 460px; background: #FF2C4C url(‘images/loginbg.jpg‘) no-repeat;"> <div class="row"> <div class="col-md-7"> <!--<img src="./image/login.jpg" width="500" height="330" alt="會員登錄" title="會員登錄">--> </div> <div class="col-md-5"> <div style="width: 440px; border: 1px solid #E7E7E7; padding: 20px 0 20px 30px; border-radius: 5px; margin-top: 60px; background: #fff;"> <font>會員登錄</font>USER LOGIN <div><%=request.getAttribute("loginInfo")==null?"":request.getAttribute("loginInfo")%></div> <form class="form-horizontal" action="/WEB15/login" method="post"> <div class="form-group"> <label for="username" class="col-sm-2 control-label">用戶名</label> <div class="col-sm-6"> <input type="text" class="form-control" id="username" name="username" placeholder="請輸入用戶名"> </div> </div> <div class="form-group"> <label for="inputPassword3" class="col-sm-2 control-label">密碼</label> <div class="col-sm-6"> <input type="password" class="form-control" id="inputPassword3" name="password" placeholder="請輸入密碼"> </div> </div> <div class="form-group"> <label for="inputPassword3" class="col-sm-2 control-label">驗證碼</label> <div class="col-sm-3"> <input type="text" class="form-control" id="inputPassword3" placeholder="請輸入驗證碼"> </div> <div class="col-sm-3"> <img src="./image/captcha.jhtml" /> </div> </div> <div class="form-group"> <div class="col-sm-offset-2 col-sm-10"> <div class="checkbox"> <label> <input type="checkbox"> 自動登錄 </label> <label> <input type="checkbox"> 記住用戶名 </label> </div> </div> </div> <div class="form-group"> <div class="col-sm-offset-2 col-sm-10"> <input type="submit" width="100" value="登錄" name="submit" style="background: url(‘./images/login.gif‘) no-repeat scroll 0 0 rgba(0, 0, 0, 0); height: 35px; width: 100px; color: white;"> </div> </div> </form> </div> </div> </div> </div> <!-- 引入footer.jsp --> <jsp:include page="/footer.jsp"></jsp:include> </body> </html>
2 LoginServlet代碼
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; import com.ithiema.register.User; import com.ithiema.utils.DataSourceUtils; public class LoginServlet extends HttpServlet { protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { request.setCharacterEncoding("UTF-8"); //1、獲得用戶名和密碼 String username = request.getParameter("username"); String password = request.getParameter("password"); //2、調用一個業務方法進行該用戶查詢 User login = null; try { login = login(username,password); } catch (SQLException e) { e.printStackTrace(); } //3、通過user是否為null判斷用戶名和密碼是否正確 if(login!=null){ //用戶名和密碼正確 //登錄成功 跳轉到網站的首頁 response.sendRedirect(request.getContextPath()); }else{ //用戶名或密碼錯誤 //跳回當前login.jsp //使用轉發 轉發到login.jsp 向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; } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } }
模擬用戶登錄功能