zoukankan      html  css  js  c++  java
  • java读取WEBINF目录下文件

    demo程序代码如下:

    HelloServlet.java:

    package com.horizon.servlet;
    
    import java.io.BufferedReader;
    import java.io.File;
    import java.io.FileReader;
    import java.io.IOException;
    
    import javax.servlet.ServletException;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    
    public class HelloServlet extends HttpServlet {
        private static final long serialVersionUID = 1L;
    
        public HelloServlet() {
            super();
        }
    
        public void doGet(HttpServletRequest request, HttpServletResponse response)
                throws ServletException, IOException {
            String output = "";
            String fileName = request.getSession().getServletContext()
                    .getRealPath("/")
                    + "WEB-INF/conf/ff.txt";
            fileName = fileName.replace("\\", "/");
            System.out.println(fileName);
            File file = new File(fileName);
    
            try {
                BufferedReader input = new BufferedReader(new FileReader(file));
                StringBuffer buffer = new StringBuffer();
                String text;
    
                while ((text = input.readLine()) != null)
                    buffer.append(text + "\n");
    
                output = buffer.toString();
            } catch (IOException ioException) {
                System.err.println("File Error!");
            }
            request.setAttribute("output", output);
            request.getRequestDispatcher("/WEB-INF/jsp/FileDemo.jsp").forward(
                    request, response);
    
        }
    
        protected void doPost(HttpServletRequest request,
                HttpServletResponse response) throws ServletException, IOException {
            doGet(request, response);
        }
    
    }

    web.xml:

    <?xml version="1.0" encoding="UTF-8"?>
    <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
        xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
        id="WebApp_ID" version="2.5">
        <display-name>TestServlet</display-name>
        <servlet>
            <description></description>
            <display-name>HelloServlet</display-name>
            <servlet-name>HelloServlet</servlet-name>
            <servlet-class>com.horizon.servlet.HelloServlet</servlet-class>
        </servlet>
        <servlet-mapping>
            <servlet-name>HelloServlet</servlet-name>
            <url-pattern>/HelloServlet</url-pattern>
        </servlet-mapping>
    </web-app>

    FileDemo.jsp:

    <%@ page language="java" contentType="text/html; charset=UTF-8"
        pageEncoding="UTF-8"%>
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>FileDemo</title>
    </head>
    <body>
        <%=request.getAttribute("output")%>
    </body>
    </html>

    访问:http://localhost:8080/TestServlet/HelloServlet

    OK!

  • 相关阅读:
    TCP/IP的确认号,序列号和超时重传的学习笔记
    Linux进程的虚拟内存
    Linux内存:物理内存管理概述
    Linux进程: task_struct结构体成员
    Linux进程:管理和调度
    Golang基础(8):go interface接口
    技术管理:团队建设
    从分布式一致性谈到CAP理论、BASE理论
    技术管理:项目管理概要
    [译]深入 NGINX: 为性能和扩展所做之设计
  • 原文地址:https://www.cnblogs.com/tv151579/p/2877829.html
Copyright © 2011-2022 走看看