servlet获取资源路径

导读:本篇文章讲解 servlet获取资源路径,希望对大家有帮助,欢迎收藏,转发!站点地址:www.bmabk.com

String  getRealPath(String path);//根据资源名称得到资源的绝对路径.

可以得到当前应用任何位置的任何资源。

servletcontextdemo4.java源码:

package com.haidi8.servletcontent;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
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;

import com.sun.xml.internal.fastinfoset.sax.Properties;

public class servletcontextdemo4 extends HttpServlet {

	/**
	 * The doGet method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to get.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		test1();
		test2();
		test3();
		
		response.setContentType("text/html");
		PrintWriter out = response.getWriter();
		out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");
		out.println("<HTML>");
		out.println("  <HEAD><TITLE>A Servlet</TITLE></HEAD>");
		out.println("  <BODY>");
		out.print("    This is ");
		out.print(this.getClass());
		out.println(", using the GET method");
		out.println("  </BODY>");
		out.println("</HTML>");
		out.flush();
		out.close();
	}

	private void test3() throws IOException, FileNotFoundException {
		String path=this.getServletContext().getRealPath("/WEB-INF/classes/b.properties");
		System.out.println(path);
		java.util.Properties pro=new java.util.Properties();
		pro.load(new FileInputStream(path));
		System.out.println(pro.get("key"));
	}

	private void test2() throws IOException, FileNotFoundException {
		String path=this.getServletContext().getRealPath("/WEB-INF/classes/com/haidi8/servletcontent/a.properties");
		System.out.println(path);
		java.util.Properties pro=new java.util.Properties();
		pro.load(new FileInputStream(path));
		System.out.println(pro.get("key"));
	}

	private void test1() throws IOException, FileNotFoundException {
		//获取WEB-INF下的文件
		String path=this.getServletContext().getRealPath("/WEB-INF/b.properties");
		//输出b.properties这个文件的目录路径
		System.out.println(path);
		//创建一个properties
		java.util.Properties pro=new java.util.Properties();
		pro.load(new FileInputStream(path));
		System.out.println(pro.getProperty("key"));
	}

	/**
	 * The doPost method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to post.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		response.setContentType("text/html");
		PrintWriter out = response.getWriter();
		out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");
		out.println("<HTML>");
		out.println("  <HEAD><TITLE>A Servlet</TITLE></HEAD>");
		out.println("  <BODY>");
		out.print("    This is ");
		out.print(this.getClass());
		out.println(", using the POST method");
		out.println("  </BODY>");
		out.println("</HTML>");
		out.flush();
		out.close();
	}

}

web.xml源码:

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" 
	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_2_5.xsd">
  <display-name></display-name>	
  
  <context-param>
  	<param-name>encoding</param-name>
  	<param-value>utf-8</param-value>
  </context-param>
  <!-- 这个是要创建一个实例 -->
  <servlet>
    <servlet-name>servletcontextdemo4</servlet-name>
    <servlet-class>com.haidi8.servletcontent.servletcontextdemo4</servlet-class>
  </servlet>


 <!--  创建一个客户端可以访问的映射 -->

  <servlet-mapping>
    <servlet-name>servletcontextdemo4</servlet-name>
    <url-pattern>/servletcontextdemo4</url-pattern>
  </servlet-mapping>
  
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
</web-app>

结果:

servlet获取资源路径

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/94188.html

(0)
小半的头像小半

相关推荐

极客之音——专业性很强的中文编程技术网站,欢迎收藏到浏览器,订阅我们!