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

相关推荐
liulilittle4 小时前
OPENPPP2 —— IP标准校验和算法深度剖析:从原理到SSE2优化实现
网络·c++·网络协议·tcp/ip·算法·ip·通信
北极光SD-WAN组网6 小时前
从0到1搭建某铝箔智慧工厂网络:5G与WiFi 6助力智能制造
网络·5g·制造
阿昭L7 小时前
HTTP原理
网络·网络协议·http
hazy1k7 小时前
STM32H750 RTC介绍及应用
网络·stm32·实时音视频
没书读了8 小时前
考研复习-计算机网络-第三章-数据链路层
网络·计算机网络·考研
zhao3266857518 小时前
2025年代理IP三强横评:LoongProxy、神龙海外动态IP代理、全民HTTP怎么选?看完这篇不踩坑
网络协议·tcp/ip·http
on the way 1238 小时前
多线程之HardCodedTarget(type=OssFileClient, name=file, url=http://file)异常
网络·网络协议·http
WhoisXMLAPI8 小时前
WhoisXML API再次荣登2025年美国Inc. 5000快速成长企业榜单
网络·安全
阿sir1989 小时前
ZYNQ 自定义IP
服务器·网络·tcp/ip
星马梦缘10 小时前
计算机网络4 第四章 网络层——网络间的通信问题(省际之间如何规划信件运输路线)
网络·计算机网络·路由·ip地址·子网掩码·icmp·ipv4/ipv6