会员可以在此提问,百战程序员老师有问必答
对大家有帮助的问答会被标记为“推荐”
看完课程过来浏览一下别人提的问题,会帮你学得更全面
截止目前,同学们一共提了 132376个问题
JAVA 全系列/第五阶段:JavaWeb开发/Servlet技术详解(旧) 707楼

image.png

image.png


image.png

我理解是有三个context:

第一个访问是http://wait:8080/forget/index.html

第二个访问不是应该是http://localhost:8080/getKey/User.html

第三个只有一个path他的docBase是什么?


正确的访问路径是http://localhost:8080/demo1/User.html

image.png

/demo1/User.html这两个没有什么关联


这三个东西搞不明白,应该怎么去理解和区分

JAVA 全系列/第五阶段:JavaWeb开发/Servlet技术详解(旧) 709楼
JAVA 全系列/第五阶段:JavaWeb开发/Servlet技术详解(旧) 711楼

老师我的运行不出来

package FistServet01;
//下载本地文件
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;

public class DownloadFileServlet07 extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doPost(req,resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //获取ServletContext对象
        ServletContext servletContext = this.getServletContext();
        //路径转换 ServletContext第一个作用相对路径转化为绝对路径
        String realPath = servletContext.getRealPath("image/cat.jpg");
        System.out.println(realPath);

        //获取文件
        File file = new File(realPath);
        FileInputStream fis = new FileInputStream(file);
        byte[] buff = new byte[fis.available()];
        fis.read(buff);
        //在响应中添加信息
        resp.addHeader("Content-Disposition","attachment;file+"+new String(file.getName().getBytes("gbk"),"iso-8859-1")); //实现文件下载要修改响应头添加附加信息
        OutputStream os = resp.getOutputStream();
        os.write(buff);
        os.flush();
        os.close();
    }
}

image.png

JAVA 全系列/第五阶段:JavaWeb开发/Servlet技术详解(旧) 712楼
JAVA 全系列/第五阶段:JavaWeb开发/Servlet技术详解(旧) 714楼

1631088693(1).jpg

1631088743(1).jpg

老师,运行完idea后点击按钮 未找到资源,我发的上面的URL的端口号为什么不是8080,您帮我看看是什么原因呢?

JAVA 全系列/第五阶段:JavaWeb开发/Servlet技术详解(旧) 716楼
JAVA 全系列/第五阶段:JavaWeb开发/Servlet技术详解(旧) 719楼

课程分类

百战程序员微信公众号

百战程序员微信小程序

©2014-2025百战汇智(北京)科技有限公司 All Rights Reserved 北京亦庄经济开发区科创十四街 赛蒂国际工业园
网站维护:百战汇智(北京)科技有限公司
京公网安备 11011402011233号    京ICP备18060230号-3    营业执照    经营许可证:京B2-20212637