// MVC:强制性的使应用的输入、处理和输出分开
// M:模型,包括JavaBean,业务逻辑处理和数据访问应该放在Model层
// V:视图,用户看到并与之交互的界面,负责展示数据,由html、jsp等来做
// C:控制,负责转发,将用户请求转发给模型层,经过处理后把结果返回到界面层,由servlet来做
一、视图层
1. 登陆界面login.jsp
<body bgcolor="pink">
<center>
用户登陆 <br> <hr>
<form action="LoginClServlet"> <!-- 提交给控制层,在web.xml里配置的url -->
账 号:<input type="text" name = "username"><br>
密 码:<input type="text" name = "passwd"><br>
<input type="submit" value="登陆">
<input type = "reset" value="重置">
</form>
</center>
</body>
2. 欢迎界面wel.jsp
<body>
登陆成功!<%=request.getParameter("username")%><br>
<a href="login.jsp">退出</a><hr>
</body>
二、控制层
1. 登陆转发Servlet:LoginClServlet.java
package com.jiang.controller;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.jiang.model.UserBeanCl;
public class LoginClServlet extends HttpServlet {
private static final long serialVersionUID = -2923205347859728847L;
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
//得到用户名和密码
String u = request.getParameter("username");
String p = request.getParameter("passwd");
//使用模型(userBeanCl),完成对用户的验证
UserBeanCl ubc = new UserBeanCl();
if(ubc.checkUser(u, p)){
request.getRequestDispatcher("wel.jsp").forward(request, response);
}else{
request.getRequestDispatcher("login.jsp").forward(request, response);
}
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
this.doGet(request, response);
}
}
2. LoginClServlet.java配置
<servlet>
<description>This is the description of my J2EE component</description>
<display-name>This is the display name of my J2EE component</display-name>
<servlet-name>LoginClServlet</servlet-name>
<servlet-class>com.jiang.controller.LoginClServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>LoginClServlet</servlet-name>
<url-pattern>/LoginClServlet</url-pattern>
</servlet-mapping>
三、模型层
1. 用户Bean: UserBean.java
package com.jiang.model;
public class UserBean {
private String username;
private String password;
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
2. 用户登陆业务处理 UserBeanCl.java
package com.jiang.model;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
public class UserBeanCl {
private Connection ct = null;
private PreparedStatement ps = null;
private ResultSet rs = null;
// 验证用户
public boolean checkUser(String u, String p) {
boolean b = false;
try {
ConnDB cd = new ConnDB();
ct = cd.getConn();
ps = ct.prepareStatement("select password from t_user where username ='"
+ u + "'");
rs = ps.executeQuery();
if (rs.next()) {
String dbPasswd = rs.getString("password");
if (dbPasswd.equals(p)) {
b = true;
}
}
} catch (Exception ex) {
ex.printStackTrace();
} finally {
this.close();
}
return b;
}
// 关闭资源
public void close() {
try {
if (rs != null) {
rs.close();
rs = null;
}
}catch(Exception e){
e.printStackTrace();
}
try{
if (ps != null) {
ps.close();
rs = null;
}
}catch(Exception e){
e.printStackTrace();
}
try{
if (ct != null) {
ct.close();
ct = null;
}
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
3. 数据库连接逻辑处理 ConnDB.java
package com.jiang.model;
import java.sql.Connection;
import java.sql.DriverManager;
public class ConnDB {
private Connection ct = null;
public Connection getConn() {
try {
Class.forName("com.mysql.jdbc.Driver");
ct = DriverManager.getConnection(
"jdbc:mysql://localhost:3306/aaa", "root", null);
} catch (Exception ex) {
// TODO Auto-generated catch block
ex.printStackTrace();
}
return ct;
}
}