Dish management system summary data transfer between jsp and servlet

The system is built based on MVC, and the most typical MVC pattern is the pattern of JSP + servlet + javabean .

  • Model (model) represents the core of the application (such as a list of database records).
  • View displays data (database records).
  • Controller (controller) processing input (writing database records)

The main knowledge used are:

1. The operation of the file upload class (add several jar packages) PS: mainly to display the menu photos here, you need to use the file upload

/********************Create a new file upload factory************************ ****************/
            Map<String,String> map = new HashMap<String,String>();
            //1. Create a disk file item factory object
            DiskFileItemFactory diskFileItemFactory = new DiskFileItemFactory ();
            //2. Create a core parsing class
            ServletFileUpload servletFileUpload = new ServletFileUpload(diskFileItemFactory);
            //3. Parse the request request and return a List request, the List collection stores the FileItem object
            List<FileItem> list1 = servletFileUpload .parseRequest(request);
            //Define a List collection    
            String url = null;
            for (FileItem fileItem : list1) {
                // Determine whether it is a form item or a file upload item
                if(fileItem.isFormField()){
                    //Ordinary form item;
                    //Receive the value of the form parameter:
                    String name = fileItem.getFieldName();//Get the value of the name attribute of the form item
                    String value = fileItem.getString("UTF-8");//Get The value of the form item
                    System.out.println(name+" "+value);
                    // Store the data in the map collection
                    map.put(name, value);
                                                            }                  
            else{
                // File upload item
                //File upload function
                // Get the name of the file upload
                String fileName = fileItem.getName();
                if(fileName !=null && !"".equals(fileName)){
                    // Get the unique file name through the tool class
                    String uuidFileName = UploadUtils.getUUIDFileName(fileName);
                    //Get file upload data
                    InputStream is = fileItem.getInputStream();
                    //Get file upload path
                    String path = this.getServletContext().getRealPath("/upload");
                    // Just connect the input stream to the output stream
                    url = path+"\\"+uuidFileName;
                    OutputStream os = new FileOutputStream(url);
                    int len ​​= 0;
                    byte[] b = new byte[1024];
                    while((len = is.read(b))!=-1){
                        os.write(b, 0, len);
                    }
                    is.close();
                    os.close();
                }
            }

2. Set the basic path in jsp

<%
    String basePath = request.getScheme()+":"+"//"+request.getServerName()+":"+request.getServerPort()
    +request.getServletContext().getContextPath()+"/";
%>

  主程序:      <form action="<%=basePath%>FoodDeleteServlet" method="post">

3. Data interaction between servlet and jsp

servlet to jsp

a. Redirection (PS: session data will not be lost immediately when it runs out)

        List<Food> list = (List<Food>) this.getServletContext().getAttribute("list");
        for(Food food:list){
            if(foodName.equals(food.getFoodName()))
            {
                //跳转到菜品查询
                    request.getSession().setAttribute("id", food.getFoodId());
                    request.getSession().setAttribute("foodName", food.getFoodName());
                    request.getSession().setAttribute("taste", food.getFoodTaste());
                    request.getSession().setAttribute("price", food.getFoodPrice());
                    request.getSession().setAttribute("description", food.getFoodDescrip());
                    request.getSession().setAttribute("url", food.getFoodPath());
                    food.getFoodDescrip();
                flag=1;
                break;
            }
        }
        request.getSession().setAttribute("flag", flag);

         response.sendRedirect(request.getContextPath()+"/showFoodList.jsp");

b. Relocation (PS: The data in the request is lost immediately when it runs out)

            for(Food u :foodList){
                if(u.getFoodId().equals(map.get("id"))||u.getFoodName().equals(map.get("foodName"))){
                    request.setAttribute("msg", "用户已经存在!");
                    request.getRequestDispatcher("/showFoodList.jsp").forward(request, response);
                    return ;
                }

c.jsp pass data to servlet

In the form of a form (see: data transfer between jsp and servlet )

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325734672&siteId=291194637