JavaWeb课程设计-学生信息管理系统(Jsp+Servlet+MySql

wuchangjian2021-11-13 01:46:30编程学习
  • 用户操作的DAO

*/

public interface UserDao {

}

实现持久层接口:

public class UserDaoImpl implements UserDao {

private JdbcTemplate template = new JdbcTemplate(JDBCUtils.getDataSource());

}

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” %>

管理员登录

管理员登录


×

${login_msg}

运行截图:

在这里插入图片描述

测试登录功能,发现中文乱码问题(直接继承HttpServlet不会出现,继承BaseServlet会出现)

6.编写过滤器解决中文乱码问题

@WebFilter("/*")

public class CharchaterFilter implements Filter {

protected String encoding;

@Override

public void destroy() {

// TODO 自动生成的方法存根

}

@Override

public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException {

// TODO 自动生成的方法存根

HttpServletRequest request=(HttpServletRequest)req;

HttpServletResponse response=(HttpServletResponse)res;

String method=request.getMethod();

if(method.equalsIgnoreCase(“post”)){

request.setCharacterEncoding(“utf-8”);

}

response.setContentType(“text/html;charset=utf-8”);

chain.doFilter(request, response);

}

@Override

public void init(FilterConfig arg0) throws ServletException {

// TODO 自动生成的方法存根

}

}

7.编写列表页面,并在后端代码上实现响应的功能

<%@ page language=“java” contentType=“text/html; charset=UTF-8”

pageEncoding=“UTF-8”%>

<%@ taglib uri=“http://java.sun.com/jsp/jstl/core” prefix=“c”%>

网站后台管理

学生信息管理

发表评论    

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。