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

相关推荐
MaximusCoder5 小时前
等保测评命令——Anolis Linux
linux·运维·服务器·网络·经验分享·安全·php
线束线缆组件品替网5 小时前
Adam Tech NPC-6-007-BU网线组件详解
服务器·网络·数码相机·智能路由器·电脑·51单片机·电视盒子
Striver-Diligent5 小时前
您的解决方案准确吗?一种用于增强通信网络可靠性的、面向故障的性能预测方法
网络·深度学习·机器学习·网络性能估计·数字孪生网络·网络预测
cheems95275 小时前
[网络原理]http协议理论基础以及wireshark抓包分析(二)
网络·http·wireshark
双星系统5 小时前
ABB机器人DSQC 679示教器电缆选型与故障排查(附原装型号对照表)
网络·数据库·机器人·工业4.0·工业机器人
炸炸鱼.5 小时前
Nginx 代理与缓存实战:正向、反向及网络层级详解
网络·nginx·缓存
5G行业应用6 小时前
6G未来网络架构ATHENA愿景——《2026韩国SK电讯6G白皮书》
网络·架构
一去不复返的通信er6 小时前
5G系统级仿真
网络·5g
北京耐用通信7 小时前
耐达讯自动化CC-Link IE转DeviceNet网关:破解三菱与欧姆龙PLC协同壁垒的工业实践
人工智能·科技·物联网·网络协议·自动化
cheems95278 小时前
[网络原理]http协议理论基础以及wireshark抓包分析(一)
网络·http·wireshark