Java教程

java bootstrap+servlet(无框架)+mysql 实现增删改查(一)

本文主要是介绍java bootstrap+servlet(无框架)+mysql 实现增删改查(一),对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

登入功能(只截取重要代码以及流程说明):

1.设计编写数据库

2.编写dao层及service层

3.(重点)servlet(control)层编写:

package stu.adam.control;

import org.omg.CORBA.Request;
import stu.adam.entity.Classinfo;
import stu.adam.service.ClassinfoService;
import stu.adam.service.Impl.ClassInfoServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.xml.ws.Response;
import java.io.IOException;
import java.time.Year;
import java.util.List;

@WebServlet(name = "classinfo", urlPatterns = "/classinfo")
public class ClassinfoServlet extends HttpServlet {
    ClassinfoService classinfoService=new ClassInfoServiceImpl();

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");
        response.setContentType("text/html;charset=UTF-8");
        // type:客户端提交的请求类型
        String type = request.getParameter("type");
        switch (type) {
            case "show":
                showClass(request, response);
            break;
        }

    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request, response);
    }

    public void showClass(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        List<Classinfo> classinfoList=classinfoService.checkAllClass();
        request.setAttribute("classList",classinfoList);
        request.getRequestDispatcher("classAction.jsp").forward(request,response);
    }



}

重要代码:

response.sendRedirect("classinfo?type=show");

在获取用户不为空之后请求重定向,切记此处不可以直接跳转jsp,而是应该发出请求,此处请求对应显示班级信息
登入完

显示班级信息开始:

1.设计编写数据库

2.编写dao层及service层

3.servlet层编写:

@WebServlet(name = "classinfo", urlPatterns = "/classinfo")
public class ClassinfoServlet extends HttpServlet {
    ClassinfoService classinfoService=new ClassInfoServiceImpl();

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");
        response.setContentType("text/html;charset=UTF-8");
        // type:客户端提交的请求类型
        String type = request.getParameter("type");
        switch (type) {
            case "show":
                showClass(request, response);
            break;
        }

    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request, response);
    }

    public void showClass(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        List<Classinfo> classinfoList=classinfoService.checkAllClass();
        request.setAttribute("classList",classinfoList);
        request.getRequestDispatcher("classAction.jsp").forward(request,response);
    }

}

重点代码:

这句话将classinfoList放到jsp里面
request.setAttribute("classList",classinfoList);
这句话只能转发,不能重定向
request.getRequestDispatcher("classAction.jsp").forward(request,response);
目前进度:

 

 










 

这篇关于java bootstrap+servlet(无框架)+mysql 实现增删改查(一)的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!