}
3. 实现业务层(Service)
编写业务层接口:
/**
- 用户管理的业务接口
*/
public interface UserService {
}
实现业务层接口:
public class UserServiceImpl implements UserService {
private UserDao dao = new UserDaoImpl();
}
4.实现表现层功能
编写表现层:
@WebServlet(“/loginServlet”)
public class LoginServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
/*
- Code
*/
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doPost(request, response);
}
}
5.由于表现层Servlet太多,我们可以做简单的提取
编写BaseServlet类,然后由其他servlet继承
public class BaseServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
public void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
try {
// 获取请求标识
String methodName = request.getParameter(“method”);
// 获取指定类的字节码对象
Class<? extends BaseServlet> clazz = this.getClass();//这里的this指的是继承BaseServlet对象
// 通过类的字节码对象获取方法的字节码对象
Method method = clazz.getMethod(methodName, HttpServletRequest.class, HttpServletResponse.class);
// 让方法执行
method.invoke(this, request, response);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
5.编写对应的前端页面:以user_login.jsp为例
<%@ page contentType=“text/html;charset=UTF-8” language=“java” %>
<%@taglib prefix=“c” uri=“http://java.sun.com/jsp/jstl/core” %>
管理员登录
×