Servlet3.0 Http函数 介绍 + upload file 源码阅读

java 复制代码
import java.io.File;
import java.io.IOException;
 
import javax.servlet.ServletException;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.Part;
 
@WebServlet("/FileUploadServlet")
@MultipartConfig(fileSizeThreshold=1024*1024*10, 	// 10 MB 
                 maxFileSize=1024*1024*50,      	// 50 MB
                 maxRequestSize=1024*1024*100)   	// 100 MB
public class FileUploadServlet extends HttpServlet {
 
    private static final long serialVersionUID = 205242440643911308L;
	
    /**
     * Directory where uploaded files will be saved, its relative to
     * the web application directory.
     */
    private static final String UPLOAD_DIR = "uploads";
     
    protected void doPost(HttpServletRequest request,
            HttpServletResponse response) throws ServletException, IOException {
        // gets absolute path of the web application
        String applicationPath = request.getServletContext().getRealPath("");	
        // constructs path of the directory to save uploaded file
        String uploadFilePath = applicationPath + File.separator + UPLOAD_DIR;
         
        // creates the save directory if it does not exists
        File fileSaveDir = new File(uploadFilePath);
        if (!fileSaveDir.exists()) {
            fileSaveDir.mkdirs();
        }
        System.out.println("Upload File Directory="+fileSaveDir.getAbsolutePath());
        
        String fileName = null;
        //Get all the parts from request and write it to the file on server
        for (Part part : request.getParts()) {
            fileName = getFileName(part);
            part.write(uploadFilePath + File.separator + fileName);
        }
 
        request.setAttribute("message", fileName + " File uploaded successfully!");
        getServletContext().getRequestDispatcher("/response.jsp").forward(
                request, response);
    }
 
    /**
     * Utility method to get file name from HTTP header content-disposition
     */
    private String getFileName(Part part) {
        String contentDisp = part.getHeader("content-disposition");
        System.out.println("content-disposition header= "+contentDisp);
        String[] tokens = contentDisp.split(";");
        for (String token : tokens) {
            if (token.trim().startsWith("filename")) {
                return token.substring(token.indexOf("=") + 2, token.length()-1);
            }
        }
        return "";
    }
}
  1. getRequestDispatcher的作用是什么?

getRequestDispatcher方法是Servlet中的一个方法,它返回一个RequestDispatcher对象。RequestDispatcher对象用于将请求转发到另一个Servlet、JSP页面或其他资源。它可以在同一个web应用程序内部进行转发,也可以将请求转发到另一个web应用程序中。
通过getRequestDispatcher方法,可以将请求和响应对象传递给其他组件,使得其他组件能够对请求进行处理并生成响应。这样可以实现模块化和复用性,将不同的处理逻辑分散在不同的组件中。
getRequestDispatcher方法接受一个参数,即转发的目标路径。可以是相对路径或绝对路径,相对路径是相对于当前的Servlet的路径。转发后,目标组件就可以获取原始请求和响应对象,并继续处理请求。

  1. getServletContext().getRequestDispatcher("/response.jsp").forward(
    request, response);
    }
    的作用是啥?

这段代码的作用是将当前的servlet请求转发给另一个servlet或JSP页面进行处理。具体来说,它将请求转发给了名为"/response.jsp"的JSP页面。在转发时,它还将HttpServletRequest对象和HttpServletResponse对象传递给目标页面,使得目标页面可以访问和处理这些请求和响应对象。通过转发请求,可以将处理逻辑委托给其他组件来处理,并在处理完成后返回结果给客户端。这段代码的作用是将当前的servlet请求转发给另一个servlet或JSP页面进行处理。具体来说,它将请求转发给了名为"/response.jsp"的JSP页面。在转发时,它还将HttpServletRequest对象和HttpServletResponse对象传递给目标页面,使得目标页面可以访问和处理这些请求和响应对象。通过转发请求,可以将处理逻辑委托给其他组件来处理,并在处理完成后返回结果给客户端。

相关推荐
CiLerLinux12 小时前
第四十九章 ESP32S3 WiFi 路由实验
网络·人工智能·单片机·嵌入式硬件
摩羯座-1856903059414 小时前
爬坑 10 年!京东店铺全量商品接口实战开发:从分页优化、SKU 关联到数据完整性闭环
linux·网络·数据库·windows·爬虫·python
YoungLime15 小时前
DVWA靶场之十三:CSP 绕过(Content Security Policy (CSP) Bypass)
网络·安全·web安全
2301_7720935615 小时前
tuchuang_后端_前端_注册登录
数据库·后端·网络协议·mysql·wireshark
芝士小宇16 小时前
tcp 服务器的设计思路
服务器·网络·tcp/ip
智能化咨询16 小时前
【深度学习计算机视觉】10:转置卷积实战进阶——破解棋盘效应与工业级应用
网络
cililin16 小时前
第4章 文件管理
linux·服务器·网络·操作系统·unix
驰羽18 小时前
C++网络编程(三)TCP通信流程
服务器·网络·tcp/ip
shylyly_18 小时前
Linux-> TCP 编程1
linux·网络·tcp/ip·echo·tcp编程
夏日漱石_18 小时前
tcp 服务器的设计思路
服务器·网络·tcp/ip