当前位置:编程学习 > JAVA >>

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

出自:淘特网

作者:淘特网
声明:转载请注明出处。

本文是根据tomcat(一个很好用的JSP运行平台)平台下实现而做,文件目录为:

 tom_homewebapps ews下:

└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(SUN企业级应用的首选).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

补充:Web开发 , Jsp ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,