Servlet--getContextPath(四)获取项目文件

举报
brucexiaogui 发表于 2021/12/30 01:08:26 2021/12/30
【摘要】 Servlet--getContextPath(四)获取项目文件 一、getContextPath获取项目运行路径 1、servlet无论在哪个路径中,获取的项目的根都是一样的。        即:      ...

Servlet--getContextPath(四)获取项目文件


一、getContextPath获取项目运行路径

1、servlet无论在哪个路径中,获取的项目的根都是一样的。

       即:

       有以下路径:

       http://127.0.0.1:8080/test07_5/Show- 则在这个代码中获取 req.getContextPath() = /test-7

       http://127.0.0.1:8080/test07_5/one/dayservle - 则获取的项目的根也是 -= /test07

2、根据获取到项目路径可以配置动态获取项目中的文件,例如获取项目中的图片。

二、获取图片实例

1、获取图片代码


      package testshow;
      import java.io.File;
      import java.io.IOException;
      import java.io.PrintWriter;
      import javax.servlet.ServletException;
      import javax.servlet.http.HttpServlet;
      import javax.servlet.http.HttpServletRequest;
      import javax.servlet.http.HttpServletResponse;
      public class Show extends HttpServlet {
     	public void doGet(HttpServletRequest request, HttpServletResponse response)
     			throws ServletException, IOException {
      		response.setContentType("text/html;charset=UTF-8");
     		PrintWriter out = response.getWriter();
     		//1、获取imgs真实路径
     				String path = getServletContext().getRealPath("/imgs");
      				out.println("imgs文件夹所在的项目路径:"+path+"<br/>");
     				File file = new File(path);
      				out.println("是否存在"+file.exists()+"<br/>");
     				//列出所有文件
      				String [] fs = file.list();
     				for(String f:fs){
      					out.println("文件名为:"+f+"<br/>");
     					//获取项目运行的根目录
     					String contextpath = request.getContextPath();
     					//获取图片路径+图片名称
      					f = contextpath+"/imgs/"+f;
      					out.print("<img src='"+f+"'/>");
      				}
      	}
      }
  
 

2、web.xml配置文件


      <?xml version="1.0" encoding="UTF-8"?>
      <web-app version="3.0"
         xmlns="http://java.sun.com/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
       <servlet>
         <description>This is the description of my J2EE component</description>
         <display-name>This is the display name of my J2EE component</display-name>
         <servlet-name>Show</servlet-name>
         <servlet-class>testshow.Show</servlet-class>
       </servlet>
        <servlet-mapping>
         <servlet-name>Show</servlet-name>
         <url-pattern>/Show</url-pattern>
       </servlet-mapping>
      </web-app>
  
 


      <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
      <%
      String path = request.getContextPath();
      String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
      %>
      <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
      <html>
       <head>
         <base href="<%=basePath%>">
         <title>My JSP 'index.jsp' starting page</title>
     	<meta http-equiv="pragma" content="no-cache">
     	<meta http-equiv="cache-control" content="no-cache">
     	<meta http-equiv="expires" content="0">
     	<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
     	<meta http-equiv="description" content="This is my page">
     	<!--
       <link rel="stylesheet" type="text/css" href="styles.css">
       -->
       </head>
       <body>
          This is my JSP page. <br>
         <a href="Show">显示风景</a>
       </body>
      </html>
  
 

4、新建imgs文件夹,并放入图片

A、在WebRoot文件夹右击选择New--Folder  输入文件夹名字imgs。

B、将事先准备好的图片放到imgs文件夹中

5、测试

在浏览器中输入  http://127.0.0.1:8080/test07_5/  点击回车。在首页点击【显示风景】页面跳转在浏览器中显示图片。

文章来源: brucelong.blog.csdn.net,作者:Bruce小鬼,版权归原作者所有,如需转载,请联系作者。

原文链接:brucelong.blog.csdn.net/article/details/76215916

【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。