一个简单的MVC的例子
MVC,Model-View-Controller。
本次的例子有网上copy的代码,也有自己写的。Java类不类,抽象之类的,设计模式什么的我一直都觉得很抽象。这个简单的小例子。
1.为了熟悉一下Java Web开发
2.为了了解一下Java的框架啊,设计模式之类的。
3.Java忘的差不多了,书看不下去,找个例子练练手。
4.为了搭建发送天气邮件服务器作基础。
这次的文字描述比较少,我觉得代码都不难(代码贼辣鸡)。主要是设计模式的了解,实现接口啊,这些东西。
首先项目截图
数据库
1.Dao
Date Access Object(数据存取对象),用于隔离业务逻辑代码和数据库访问操作。Dao模式分为接口和实现类。
(1)Dao接口
public interface EmailDao {
public int addEmail(EmailBean email);
public EmailBean getEmail(String email);
public int updateEmail(String email,EmailBean emailBean);
public int deleteEmail(String email);
}
(2)Dao接口的实现
也就是调用JDBC工具类,进行增、删、改、插。
public class EmailMySqlDaoImpl implements EmailDao {
Connection conn = null;
PreparedStatement st = null;
@Override
public int addEmail(EmailBean email) {
String sql = "insert into emailtb(email,time,state) values(?,?,?)";
try {
conn = JDBCUtils.getConnection();
st = conn.prepareStatement(sql);
st.setString(1, email.getEmail());
st.setString(2, email.getTime());
st.setInt(3, email.getState());
st.execute();
} catch (SQLException e) {
e.printStackTrace();
}
return 0;
}
@Override
public EmailBean getEmail(String email) {
EmailBean emailBean = new EmailBean();
ResultSet rs = null;
String sql = "select * from emailtb WHERE email = ?";
try {
conn = JDBCUtils.getConnection();
st = conn.prepareStatement(sql);
st.setString(1, email);
rs = st.executeQuery();
while (rs.next()) {
emailBean.setEmail(rs.getString("email"));
emailBean.setTime(rs.getString("time"));
emailBean.setState(rs.getInt("state"));
}
} catch (SQLException e) {
e.printStackTrace();
}
return emailBean;
}
@Override
public int updateEmail(String email, EmailBean emailBean) {
int state = 0;
String sql = "UPDATE emailtb set email = ?,time = ?,state=? where email = ?";
try {
conn = JDBCUtils.getConnection();
st = conn.prepareStatement(sql);
st.setString(1, emailBean.getEmail());
st.setString(2, emailBean.getTime());
st.setInt(3, emailBean.getState());
st.setString(4, email);
state = st.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
}
return state;
}
@Override
public int deleteEmail(String email) {
EmailBean emailBean = new EmailBean();
int state = 0;
try {
conn = JDBCUtils.getConnection();
String sql = "DELETE from emailtb WHERE email = ?";
st = conn.prepareStatement(sql);
st.setString(1, email);
state = st.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
}
return state;
}
}
2.Service层
还是分为接口和实现类
(1)Service接口
public interface EmailService {
public int addEmail(EmailBean emailBean);
public int deleteEmail(String email);
public int updateEmail(String email,EmailBean emailBean);
public EmailBean getEmail(String email);
}
(2)实现类
public class EmailServiceImpl implements EmailService {
private EmailDao dao = new EmailMySqlDaoImpl();
@Override
public int addEmail(EmailBean emailBean) {
return dao.addEmail(emailBean);
}
@Override
public int deleteEmail(String email) {
return dao.deleteEmail(email);
}
@Override
public int updateEmail(String email, EmailBean emailBean) {
return dao.updateEmail(email,emailBean);
}
@Override
public EmailBean getEmail(String email) {
return dao.getEmail(email);
}
}
3.JavaBean
我并不是很了解这个,可能代码会简洁点。
定义一些属性,然后生成构造方法和Get and Set,这个类就不放代码了。
4.Servlet
就不多解释了,并不是很明白,只能说会用。
public class EmailServlet extends HttpServlet {
protected void doPost(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response) throws javax.servlet.ServletException, IOException {
doGet(request, response);
}
protected void doGet(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response) throws javax.servlet.ServletException, IOException {
// 解决浏览器乱码问题
request.setCharacterEncoding("UTF-8");
response.setContentType("text/html;charset= UTF-8");
// 获取一个输出流对象
PrintWriter out = response.getWriter();
// 获取请求界面参数名为“op”的参数值;
// 并根据该值调用相应的函数进行操作;
String op = request.getParameter("op");
if (op.equals("insert")) {
EmailBean emailBean = new EmailBean();
String email = request.getParameter("email");
if(select(email).getEmail() == null){
emailBean.setEmail(email);
emailBean.setTime(new SimpleDateFormat("yyyy.MM.dd HH.mm.ss").format(new Date()));
emailBean.setState(Integer.valueOf(request.getParameter("state")));
insert(emailBean);
out.write("<script language='javascript'>alert('成功')</script>");
}
} else if (op.equals("delete")) {
String email = request.getParameter("email");
int i = delete(email);
out.write("<script language='javascript'>alert('"+i+"')</script>");
} else if (op.equals("update")) {
String semail = request.getParameter("semail");
EmailBean emailBean = new EmailBean(
request.getParameter("email"),
new SimpleDateFormat("yyyy.MM.dd HH.mm.ss").format(new Date()),
Integer.valueOf(request.getParameter("state"))
);
int i = update(semail,emailBean);
out.write("<script language='javascript'>alert('"+i+"')</script>");
} else if (op.equals("select")) {
String email = request.getParameter("email");
EmailBean emailBean = select(email);
StringBuffer sb = new StringBuffer();
sb.append(emailBean.getEmail()+"\n");
sb.append(emailBean.getTime()+"\n");
sb.append(emailBean.getState()+"\n");
out.write("<script language='javascript'>alert('"+sb.toString()+"')</script>");
}
}
private int insert(EmailBean emailBean) {
return new EmailMySqlDaoImpl().addEmail(emailBean);
}
private int delete(String eamil) {
return new EmailMySqlDaoImpl().deleteEmail(eamil);
}
private int update(String email, EmailBean emailBean) {
return new EmailMySqlDaoImpl().updateEmail(email, emailBean);
}
private EmailBean select(String email) {
return new EmailMySqlDaoImpl().getEmail(email);
}
}
5.工具类
(1)JDBCUtils
连接数据库,也没什么好解释的
public class JDBCUtils {
private static String Driver = "com.mysql.jdbc.Driver";
private static String url = "jdbc:mysql://127.0.0.1:3306/weatherserver?characterEncoding=utf-8";
private static String user = "root";
private static String password = "123456";
//获取数据库连接
public static Connection getConnection() {
Connection conn = null;
try {
Class.forName(Driver);
System.out.println(url);
System.out.println(user);
System.out.println(password);
conn = DriverManager.getConnection(url, user, password);
} catch (SQLException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
return conn;
}
public static void close(Connection conn, Statement stat, ResultSet rs){
if(conn != null){
try {conn.close();} catch (SQLException e) {e.printStackTrace();}
}
if(stat != null){
try {stat.close();} catch (SQLException e) {e.printStackTrace();}
}
if(rs != null){
try {rs.close();} catch (SQLException e) {e.printStackTrace();}
}
}
6.index.jsp
四个方法,请求Servlet
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>$Title$</title>
</head>
<body>
<form action="servlet/EmailServlet?op=insert" method="post">
email:<input type="email" name="email"><br>
state:<input type="number" name="state"><br>
<input type="submit" value="Insert"><br>
</form>
<form action="servlet/EmailServlet?op=delete" method="post">
email:<input type="text" name="email"><br>
<input type="submit" value="Delete"><br>
</form>
<form action="servlet/EmailServlet?op=update" method="post">
email:<input type="text" name="semail"><br>
email:<input type="text" name="email"><br>
state:<input type="text" name="state"><br>
<input type="submit" value="Update"><br>
</form>
<form action="servlet/EmailServlet?op=select" method="post">
email:<input type="text" name="email"><br>
<input type="submit" value="Update"><br>
</form>
</body>
</html>
7.web.xml
<servlet>
<servlet-name>EmailServlet</servlet-name>
<servlet-class>com.example.servlet.EmailServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>EmailServlet</servlet-name>
<url-pattern>/servlet/EmailServlet</url-pattern>
</servlet-mapping>
当然,有代码有什么问题的请评论
谢谢!观看!
网友评论