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

相关推荐
闲人编程1 小时前
Python在网络安全中的应用:编写一个简单的端口扫描器
网络·python·web安全·硬件·端口·codecapsule·扫描器
机器学习之心4 小时前
基于双向时序卷积网络(BiTCN)与支持向量机(SVM)混合模型的时间序列预测代码Matlab源码
网络·支持向量机·matlab
止水编程 water_proof6 小时前
Java-HTTP响应以及HTTPS(下)
网络·网络协议·http
好望角雾眠6 小时前
第四阶段C#通讯开发-9:网络协议Modbus下的TCP与UDP
网络·笔记·网络协议·tcp/ip·c#·modbus
网安小白的进阶之路6 小时前
A模块 系统与网络安全 第四门课 弹性交换网络-5
网络·安全·web安全
8K超高清7 小时前
高校巡展:中国传媒大学+河北传媒学院
大数据·运维·网络·人工智能·传媒
C2H5OH6667 小时前
WebSocket-练习1
网络·websocket·网络协议
狂奔的sherry7 小时前
Socket vs WebSocket
网络·websocket·网络协议
sadandbad9 小时前
[vulhub靶机通关]DC-2(rbash绕过_git提权)
网络·sql·web安全·网络安全
2501_915106329 小时前
App HTTPS 抓包 工程化排查与工具组合实战
网络协议·ios·小程序·https·uni-app·php·iphone