1、servlet开发
2、model I模式
客户端通过访问JSP,调用里面的javabean,而通过javabean调用处理数据库的操作,javabean中有专门处理数据库的操作,数据库主要以DAO为主,出去了数据库外也包含业务处理。
3、model II 开发模式
servlet特点:可以接收用户的请求,还可以调用Java程序,由于本身就是以Java程序运行的,所以肯定要比jsp性能高,而且安全性能高。
最终,servlet所有的操作内容都有交个jsp显示,这样一来,servlet缺点就避免了。
JavaBean完成具体的单个功能,jsp完成显示功能,servlet负责连接jsp和javabean。
jsp+servlet+javabean,这样的设计非常适合大型的项目开发,而且维护方便。
3、MVC设计模式
4、MVC 设计模式的应用
5.1 sql实现
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‘,‘administrator‘,‘admin‘) ;
5.2 数据库连接
public class DatabaseConnection { private static final String DBDRIVER = "com.mysql.jdbc.Driver"; private static final String DBURL = "jdbc:mysql://localhost/struts?useUnicode=true&characterEncoding=utf-8"; private static final String DBUSER = "root"; private static final String DBPASSWORD = "alice"; private Connection conn; private static int count = 0; public DatabaseConnection() { System.out.println("************* count = " + this.count++); try { Class.forName(DBDRIVER); this.conn = DriverManager.getConnection(DBURL, DBUSER, DBPASSWORD); } catch (ClassNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } public Connection getConnection() { return this.conn; } public void close() { if (this.conn != null) { try { this.conn.close(); } catch (SQLException e) { e.printStackTrace(); } } } }
5.3 vo类
public class User { private String userid ; private String name ; private String password ; public void setUserid(String userid){ this.userid = userid ; } public void setName(String name){ this.name = name ; } public void setPassword(String password){ this.password = password ; } public String getUserid(){ return this.userid ; } public String getName(){ return this.name ; } public String getPassword(){ return this.password ; } }
5.4 IUSERDAO接口
public interface IUserDAO { // 现在完成的是登陆验证,那么登陆操作只有两种返回结果 public boolean findLogin(User user) throws Exception ; }
5.5 DAO接口实现类
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=?" ; this.pstmt = this.conn.prepareStatement(sql) ; this.pstmt.setString(1,user.getUserid()) ; this.pstmt.setString(2,user.getPassword()) ; ResultSet rs = this.pstmt.executeQuery() ; if(rs.next()){ user.setName(rs.getString(1)) ; // 取出一个用户的真实姓名 flag = true ; } this.pstmt.close() ; return flag ; } }
5.6 UserDAOProxy:定义代理,负责数据库的打开和关闭并调用真实主体
public class UserDAOProxy implements IUserDAO { private DatabaseConnection dbc = null ; private IUserDAO dao = null ; public UserDAOProxy(){ try{ this.dbc = new DatabaseConnection() ; }catch(Exception e){ e.printStackTrace() ; } this.dao = new UserDAOImpl(dbc.getConnection()) ; } 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 ; } }
5.7 Factory工厂类:取得DAO接口的实例化
public class DAOFactory { public static IUserDAO getIUserDAOInstance(){ return new UserDAOProxy() ; } }
5.8 servlet类
接收请求参数,进行参数验证,调用DAO 完成具体登录验证,并根据DAO验证结果返回登录信息。
public class LoginServlet extends HttpServlet { public void doGet(HttpServletRequest req,HttpServletResponse resp) throws ServletException,IOException{ String path = "login.jsp" ; String userid = req.getParameter("userid") ; String userpass = req.getParameter("userpass") ; List<String> info = new ArrayList<String>() ; // 收集错误 if(userid==null || "".equals(userid)){ info.add("用户id不能为空!") ; } if(userpass==null || "".equals(userpass)){ info.add("密码不能为空!") ; } if(info.size()==0){ // 里面没有记录任何的错误 User user = new User() ; user.setUserid(userid) ; user.setPassword(userpass) ; try{ if(DAOFactory.getIUserDAOInstance().findLogin(user)){ info.add("用户登陆成功,欢迎" + user.getName() + "光临!") ; } else { info.add("用户登陆失败,错误的用户名和密码!") ; } }catch(Exception e){ e.printStackTrace() ; } } req.setAttribute("info",info) ; req.getRequestDispatcher(path).forward(req,resp) ; } public void doPost(HttpServletRequest req,HttpServletResponse resp) throws ServletException,IOException{ this.doGet(req,resp) ; }
<script language="javascript"> function validate(f){ if(!(/^\w{5,15}$/.test(f.userid.value))){ alert("用户ID必须是5~15位!") ; f.userid.focus() ; return false ; } if(!(/^\w{5,15}$/.test(f.userpass.value))){ alert("密码必须是5~15位!") ; f.userpass.focus() ; return false ; } } </script> <% 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 validate(this)"> 用户ID:<input type="text" name="userid"><br> 密 码:<input type="password" name="userpass"><br> <input type="submit" value="登陆"> <input type="reset" value="重置"> </form> </body> </html>
时间: 2024-10-27 08:23:08