码迷,mamicode.com
首页 > 其他好文 > 详细

Servlet获取当前服务器的实际路径

时间:2015-08-25 16:13:08      阅读:117      评论:0      收藏:0      [点我收藏+]

标签:

Servlet/Jsp需要操作服务器所拥有的资源,为此需要得到其绝对路径或实际路径,

在Servlet的doGet方法中加入以下代码可以查看服务器的实际路径。

 1 package com.mhb;
 2 
 3 import java.io.IOException;
 4 import java.io.PrintWriter;
 5 
 6 import javax.servlet.ServletContext;
 7 import javax.servlet.ServletException;
 8 import javax.servlet.http.HttpServlet;
 9 import javax.servlet.http.HttpServletRequest;
10 import javax.servlet.http.HttpServletResponse;
11 
12 public class getRealPathServletDemo extends HttpServlet {
13 
14     public void init() throws ServletException {
15     }
16 
17     public void doGet(HttpServletRequest request, HttpServletResponse response)
18             throws ServletException, IOException {
19         response.setContentType("text/html;charset=utf-8");
20         ServletContext context = this.getServletContext();
21         PrintWriter out = response.getWriter();
22         out.print("当前服务器的实际路径为:"+context.getRealPath(""));
23     }
24 
25     public void doPost(HttpServletRequest request, HttpServletResponse response)
26             throws ServletException, IOException {
27     }
28 
29     public void destroy() {
30         super.destroy(); 
31     }
32 }

 

Servlet获取当前服务器的实际路径

标签:

原文地址:http://www.cnblogs.com/tdcqma/p/4757215.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!