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对象传递给目标页面,使得目标页面可以访问和处理这些请求和响应对象。通过转发请求,可以将处理逻辑委托给其他组件来处理,并在处理完成后返回结果给客户端。

相关推荐
zm21 分钟前
TCP套接字通信核心要点
网络·windows
无名之逆32 分钟前
Hyperlane: Unleash the Power of Rust for High-Performance Web Services
java·开发语言·前端·后端·http·rust·web
en48137 分钟前
路由策略和策略路由的区别以及配置案例
网络·网络协议·tcp/ip·计算机网络
网络空间小黑2 小时前
渗透测试行业术语2
服务器·网络·安全·网络安全·中间件
海尔辛6 小时前
学习黑客三次握手快速熟悉
网络·学习·tcp/ip
小刘|6 小时前
对称加密以及非对称加密
linux·运维·网络
腾飞的信仰7 小时前
51单片机同一个timer 作为定时器和波特率发生器么?
网络·单片机·51单片机
中科岩创7 小时前
某公园楼栋自由曲面薄壳结构自动化监测
大数据·网络·物联网·自动化
买辣椒用券8 小时前
ESP32开发入门(七):HTTP开发实践
网络协议·嵌入式
咸鱼2333号程序员9 小时前
Linux ifconfig命令详解
linux·服务器·网络