涓€銆佺櫥褰曢〉闈?span style="white-space:pre;"> /jsp/login.jsp
<%@ page contentType="text/html; charset=GBK" import="java.util.*" %> <html> <head><title>hsl</title></head> <script language="JavaScript"> function vaildate(f){ var regex = /^\w{5,15}$/; // 瀹氫箟姝e垯 if(!(regex.test(f.userid.value))){ alert("鐢ㄦ埛 ID 蹇呴』鏄?5~15浣嶏紒"); f.userid.focus(); return false; } if(!(regex.test(f.userpass.value))){ alert("瀵嗙爜蹇呴』鏄?5~15浣嶏紒"); f.userpass.focus(); return false; } return true; } </script> <body> <h2>鐢ㄦ埛鐧诲綍绋嬪簭</h2> <% request.setCharacterEncoding("GBK"); // 璁剧疆缂栫爜 %> <% List<String> info = (List<String>)request.getAttribute("info"); //鍙栧緱灞炴€? if(info!=null){ // 鍒ゆ柇鏄惁鏈夊唴瀹? Iterator<String> iter = info.iterator(); while(iter.hasNext()){ %> <h4><%=iter.next()%></h4> <!-- 杈撳嚭鐧诲綍楠岃瘉淇℃伅 --> <% } } %> <form action="LoginServlet" method="post" onSubmit="return vaildate(this)"> 鐢ㄦ埛鍚嶏細<input type="text" name="userid"/><br/> 瀵?nbsp; 鐮侊細<input type="password" name="userpass"/><br/> <input type="submit" value="鐧诲綍"/> <input type="reset" value="閲嶇疆"/> </form> </body> </html>
浜屻€侀厤缃?web.xml
<servlet> <servlet-name>login</servlet-name> <servlet-class>hsl.mvc.servlet.LoginServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>login</servlet-name> <url-pattern>/jsp/LoginServlet</url-pattern> </servlet-mapping>涓夈€佸畾涔?Servlet---LoginServlet.java
package hsl.mvc.servlet; import hsl.mvc.factory.DAOFactory; import hsl.mvc.vo.User; import java.io.IOException; import java.util.ArrayList; import java.util.List; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class LoginServlet extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String path = "login.jsp"; String userid = request.getParameter("userid"); //鎺ユ敹 userid 鍙傛暟 String userpass = request.getParameter("userpass"); //鎺ユ敹 userpass 鍙傛暟 List<String> info = new ArrayList<String>(); // 淇濆瓨杩斿洖淇℃伅 if(userid==null || "".equals(userid)){ // 濡傛灉 userid 涓?null info.add("鐢ㄦ埛id涓嶈兘涓虹┖锛?); } if(userpass==null || "".equals(userpass)){ // 濡傛灉 userpass 涓?null info.add("瀵嗙爜涓嶈兘涓虹┖锛?); } if(info.size()==0){ // 鐢ㄦ埛鍚嶅拰瀵嗙爜楠岃瘉閫氳繃 User user = new User(); // 瀹炰緥鍖?VO user.setUserid(userid); // 璁剧疆 userid user.setPassword(userpass); // 璁剧疆 userpass try { if(DAOFactory.getIUserDAOInstance().findLogin(user)){ info.add("鐢ㄦ埛鐧诲綍鎴愬姛锛?娆㈣繋"+user.getName()+"鍏変复锛?); }else{ info.add("鐢ㄦ埛鐧诲綍澶辫触锛岄敊璇殑鐢ㄦ埛鍚嶆垨瀵嗙爜锛?); } } catch (Exception e) { e.printStackTrace(); } } request.setAttribute("info", info); // 淇濆瓨閿欒淇℃伅 request.getRequestDispatcher(path).forward(request, response); // 璺宠浆 } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { this.doGet(request, response); //doPost銆乨oGet鐩镐簰璋冪敤 } }鍥涖€佸畾涔夊伐鍘傜被锛屽彇寰?DAO 瀹炰緥----DAOFactory.java
package hsl.mvc.factory; import hsl.mvc.dao.IUserDAO; import hsl.mvc.dao.proxy.UserDAOProxy; public class DAOFactory { public static IUserDAO getIUserDAOInstance(){ // 鍙栧緱 DAO 瀹炰緥 return new UserDAOProxy(); // 杩斿洖浠g悊瀹炰緥 } }浜斻€佸畾涔?DAO 浠g悊鎿嶄綔绫?span style="white-space:pre;">----UserDAOProxy.java
package hsl.mvc.dao.proxy; import hsl.mvc.dao.IUserDAO; import hsl.mvc.dao.UserDAOImpl; import hsl.mvc.dbc.DatabaseConnection; import hsl.mvc.vo.User; public class UserDAOProxy implements IUserDAO { private DatabaseConnection dbc = null; // 瀹氫箟鏁版嵁搴撳疄渚?dbc private IUserDAO dao = null; // 瀹氫箟杩炴帴 dao public UserDAOProxy(){ // 鏋勯€? try { this.dbc = new DatabaseConnection(); // 瀹炰緥鍖栨暟鎹簱杩炴帴 dbc } catch (Exception e) { e.printStackTrace(); } this.dao = new UserDAOImpl(this.dbc.getConnection()); // 鍙栧緱杩炴帴 dao } public boolean findLogin(User user) throws Exception { boolean flag = false; try { flag = this.dao.findLogin(user); // 璋冪敤鐪熷疄涓婚 } catch (Exception e) { throw e; }finally{ this.dbc.close(); } return flag; // 杩斿洖鏍囪 } }鍏€佸畾涔?DAO 鎺ュ彛----IUserDAO.java
package hsl.mvc.dao; import hsl.mvc.vo.User; /** * DAO 鎿嶄綔鎺ュ彛 */ public interface IUserDAO { /** * 鐢ㄦ埛鐧诲綍楠岃瘉 * @param user * @return 楠岃瘉鐨勬搷浣滅粨鏋? * @throws Exception */ public boolean findLogin(User user)throws Exception; }涓冦€佸畾涔?DAO 瀹炵幇绫?span style="white-space:pre;">----UserDAOImpl.java
package hsl.mvc.dao; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import hsl.mvc.dao.IUserDAO; import hsl.mvc.vo.User; public class UserDAOImpl implements IUserDAO { private Connection conn = null; // 瀹氫箟鏁版嵁搴撹繛鎺ュ璞? private PreparedStatement pstmt = null; // 瀹氫箟鏁版嵁搴撴搷浣滃璞? public UserDAOImpl(Connection conn) { // 璁剧疆鏁版嵁搴撹繛鎺? this.conn = conn; } public boolean findLogin(User user) throws Exception { boolean flag = false; String sql = "select name from user where userid=? and password=?"; try { this.pstmt = this.conn.prepareStatement(sql); // 瀹炰緥鍖栨搷浣? this.pstmt.setString(1, user.getUserid()); // 璁剧疆 id this.pstmt.setString(2, user.getPassword()); // 璁剧疆 password ResultSet rs = this.pstmt.executeQuery(); // 鍙栧緱鏌ヨ缁撴灉 if (rs.next()) { user.setName(rs.getString(1)); // 鍙栧緱鐢ㄦ埛鍚? flag = true; // 鐧诲綍鎴愬姛 } } catch (Exception e) { throw e; } finally { if (this.pstmt != null) { try { this.pstmt.close(); // 鍏抽棴鎿嶄綔 } catch (Exception e) { throw e; } } } return flag; } }鍏€佸畾涔夋暟鎹簱鎿嶄綔绫?span style="white-space:pre;"> ----DatabaseConnection.java
package hsl.mvc.dbc; import java.sql.Connection; import java.sql.DriverManager; public class DatabaseConnection { private static final String DBDRIVER = "org.gjt.mm.mysql.Driver"; private static final String DBURL = "jdbc:mysql://localhost:3336/mvctest"; private static final String DBUSER = "root"; private static final String DBPASSWORD = "myoa888"; private Connection conn = null; public DatabaseConnection() throws Exception { try { Class.forName(DBDRIVER); // 鍔犺浇椹卞姩绋嬪簭 this.conn = DriverManager.getConnection(DBURL, DBUSER, DBPASSWORD); // 杩炴帴鏁版嵁搴? } catch (Exception e) { throw e; } } public Connection getConnection() { // 鍙栧緱鏁版嵁搴撹繛鎺? return this.conn; } public void close() throws Exception { // 鏁版嵁搴撳叧闂搷浣? if (this.conn != null) { try { this.conn.close(); } catch (Exception e) { throw e; } } } }
涔濄€?瀹氫箟 VO 绫?span style="white-space:pre;"> ----User.java
package hsl.mvc.vo; public class User { private String userid ; // 瀵瑰簲 userid 鍒? private String name ; // 瀵瑰簲 name 鍒? private String password ; public String getUserid(){ return userid; } public void setUserid(String userid){ this.userid = userid; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } }鍗併€佹暟鎹簱鍒涘缓 ----mvctest (user)
/*============浣跨敤 mvctest 鏁版嵁搴?============*/ USE mvctest; /*============鍒犻櫎 user 鏁版嵁琛?============*/ DROP TABLE IF EXISTS user; /*============鍒涘缓 user 鏁版嵁琛?============*/ CREATE TABLE user( userid VARCHAR(30) PRIMARY KEY, name VARCHAR(30) NOT NULL, password VARCHAR(32) NOT NULL ); /*============鎻掑叆娴嬭瘯鏁版嵁=============*/ INSERT INTO user(userid,name,password)VALUES('admin','绠$悊鍛?,'admin');
鍗佷竴銆乀omcat 閰嶇疆 server.xml (</Host>鍓嶈竟鍔犲叆浠g爜濡備笅)
<Context path="/Emp" reloadable="true" docBase="D:\WorkspaceJ2ee\EmpProject\WebRoot" workDir="D:\WorkspaceJ2ee\EmpProject\WebRoot" />杩愯缁撴灉鏌ョ湅锛?