爱心技术专栏专题

jsp+java类+servlet实现文件读取、写入的功能(二)

摘录:java基础 来源:java基础 加入时间:2007年03月10日
摘要:
jsp+java类+servlet实现文件读取、写入的功能(二)

本文是根据tomcat平台下实现而做,文件目录为:

 tom…

转载:转载请保留本信息,本文来自
http://www.51dibs.com
/html/2006/article/info8/a_da1b07dd3b564393.htm

jsp+java类+servlet实现文件读取、写入的功能(二)

站点:爱心种子小博士 关键字:jsp+java类+servlet实现文

   
jsp+java类+servlet实现文件读取、写入的功能(二)

本文是根据tomcat平台下实现而做,文件目录为:

 tom_home\webapps\news下:

└html
└WEB-INF
└classes
    └com
      └FileMan.class
      └FileServlet.class
 └web.xml

接着我们先实现servlet:FileServlet .java

/*
 * FileServlet.java
 *
 * Created on 2005年6月19日, 下午3:03
 */

package com;

import java.io.*;
import java.net.*;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.jsp.PageContext;

/**
 *
 * @author  淘特网
 * @version
 */
public class FileServlet extends HttpServlet {
   
    ServletContext sc;
    /** Initializes the servlet.
     */
    public void init(ServletConfig config) throws ServletException {
        super.init(config);
        sc=config.getServletContext();
       
    }
   
    /** Destroys the servlet.
     */
    public void destroy() {
       
    }
   
    /** Processes requests for both HTTP GET and POST methods.
     * @param request servlet request
     * @param response servlet response
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        response.setContentType("text/html");
        PrintWriter out = response.getWriter();
        //* TODO output your page here
        out.println("");
        out.println("");
        out.println("");
        out.println("");
        out.println("");
        out.println("Hello");
        out.println(sc.getRealPath("/"));
        FileMan fm=new FileMan();
 try
 {
 fm.WriteFile(sc.getRealPath("/")+"/html/test.htm","asf");
 out.println(fm.ReadFile(sc.getRealPath("/")+"/html/test.htm"));
 }
 catch(FileNotFoundException e){}
        out.println("");
        out.println("");
        // */
        out.close();
    }
   
    /** Handles the HTTP GET method.
     * @param request servlet request
     * @param response servlet response
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    }
   
    /** Handles the HTTP POST method.
     * @param request servlet request
     * @param response servlet response
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    }
   
    /** Returns a short description of the servlet.
     */
    public String getServletInfo() {
        return "Short description";
    }
   
}