zoukankan      html  css  js  c++  java
  • Servlet获取当前服务器的实际路径

    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 }
  • 相关阅读:
    自定义Python枚举
    解决Django跨域访问的问题
    BBS项目细节总结
    面向对象进阶
    面向对象
    三级菜单
    常用模块
    内置函数与匿名函数及递归
    迭代器和生成器
    函数
  • 原文地址:https://www.cnblogs.com/tdcqma/p/4757215.html
Copyright © 2011-2022 走看看