本人想用JSP+tomcat+Apache做一个个人网站,遇到两个问题,一个是记录访问量问题,刷新不能改变访问量,还有我准备放个下载区,可以让访问者可以下载上面的资源,请问怎么做啊?有谁给我个完整的方案.我定高分送出.发我Email就行了,我的Email为:[email protected]

解决方案 »

  1.   

    1.要是我就用登陆计数
    2.有组件 baidu下
      

  2.   

    刷新不能改变访问量?使用session纪录。
      

  3.   

    用一个简单的方法,可以用个文件或者什么可以持久化的东西将访问量保存起来,在页面加载的时候读取这个保存量(servlet 中的 init()),在销毁的时候再将改变的数据存进去(servlet 中的 destroy())
      

  4.   

    Re:dr_lou
    能不能说详细一点?特别是第二个问题:你说的那个组件叫什么?Re:x424a
    你的意见不错,不过你说在实际中怎么实施呢?我要在首页(.html)中显示,你如何做到页面加载时可以读取这个保存量?
      

  5.   

    我记得好象是IP能绑定到session里 刷新的话不好用
      

  6.   

    你可以得到客户端的IP,根据IP来控制.
      

  7.   

    服务器启动时(实现ServletContextListener监听器contextInitialized方法),读取数据库,并将其用一个计数变量保存在application范围内session创建时(实现HttpSessionListener监听器sessionCreated方法),读取计数变量加1并重新保存服务器关闭时(实现ServletContextListener监听器contextDestroyed方法),更新数据库
      

  8.   

    最简单的下载,不能限速
    要限制速度的话,据我所知,要用到ftp服务器
    package com.yilin.download;import javax.servlet.ServletException;
    import javax.servlet.ServletOutputStream;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    import javax.servlet.*;
    import javax.servlet.http.*;
    import java.io.*;/*实现下载功能*/
    public class DownLoad extends HttpServlet{
        
    public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException {

    String fileName = request.getParameter("fileName");
    System.out.println(fileName); if(fileName!=null&&!fileName.equals(""))
    {
    String filePath = this.getServletContext().getRealPath("/")+fileName;
    System.out.println(filePath);  //d:\Program Files\Tomcat 5.5\webapps\download\hdxs1.rmvb try{
    File file=new File(filePath);
    FileInputStream fis=new FileInputStream(file);

    response.setCharacterEncoding("GBK");
    response.setContentType("application/x-msdownload");
    response.setContentLength((int)file.length()); String str="attachment;filename="+file.getName();
    System.out.println("filepath:"+str);
    response.setHeader("Content-Disposition",str);

    ServletOutputStream sos=response.getOutputStream(); byte[]data=new byte[1024];
    int length=0;
    while((length=fis.read(data))>0)
    {
    sos.write(data,0,length);
    }
    sos.close();
    fis.close(); }catch(IOException ioe){
    ioe.printStackTrace();
    }
    }

    }

    //Process the HTTP Post request
        public void doPost(HttpServletRequest request, HttpServletResponse response) throws
                ServletException, IOException {
            doGet(request, response);
        }    //Clean up resources
        public void destroy() {
        }

    }====================jsp=================
    <br><br><h1><a href="down?fileName=hdxs1.mp3">下载</a></h1>===================web.xml==================<servlet>
        <servlet-name>downLoad</servlet-name>
        <servlet-class>com.yilin.download.DownLoad</servlet-class>
      </servlet>  <servlet-mapping>
        <servlet-name>downLoad</servlet-name>
        <url-pattern>/down</url-pattern>
      </servlet-mapping>
      

  9.   

    String dir = getServletContext().getRealPath("/");
    if (session.isNew()) {
    String nameOfTextFile = "peopleCount.txt";
    nameOfTextFile = dir+  nameOfTextFile;
    BufferedReader file = new BufferedReader(new FileReader(nameOfTextFile));
    String readStr = null;
    try {
    readStr = file.readLine();
    } catch (IOException e) {
    System.out.println("读取数据失败!"+e.getMessage());
    }
    if(readStr!=null){
    try{
    int number = Integer.parseInt(readStr);
    number++;
    String str = String.valueOf(number);
    writeStr=str;
    session.setAttribute("count", str);
    try{
    PrintWriter pw = new PrintWriter(new FileOutputStream(nameOfTextFile));
    pw.println(number);
    pw.close();
    } catch (IOException e) {
         //out.println(e.getMessage());
          System.out.println("写数据失败!");
    }
    }catch(NumberFormatException e){
    System.out.print("数据类型转换错误!");
    }
    }
    }
      

  10.   

    Re:yilinhust
    第二个问题已解决,对于第一个问题,实现ServletContextListener监听器在哪实现?
    我没用过这个接口,能不能说详细一点?还有用IP实现倒是个不错的选择,但实现起来好像挺麻烦的,有没人能就此解决一下吗?因为我还做了一个留言板,要记住已注册者的Session,否则可能会出现页面的混乱.
      

  11.   

    package com.jspdev.ch8;import javax.servlet.ServletContextEvent;
    import javax.servlet.ServletContextListener;
    import javax.servlet.ServletContext;
    import javax.servlet.ServletContextAttributeEvent;
    import javax.servlet.ServletContextAttributeListener;
    import java.io.*;
    public final class MyServletContextListener 
    implements ServletContextListener,HttpSessionListener {
       
        /**
         *服务器关闭时
         */
        public void contextDestroyed(ServletContextEvent sce) {

    ServletContext context = sce.getServletContext(); int count = ((Integer)context.getAttribute("count")).intValue(); //将count保存到数据库或其他持久化...
        }

    /**
         *服务器启动时
         */
        public void contextInitialized(ServletContextEvent sce) { ServletContext context = sce.getServletContext(); int count = 0; //读取数据库或其他持久化的数据...

    if(context.getAttribute("count")==null)
    context.setAttribute("count",new Integer(count));    }
    // 实现HttpSessionListener接口,完成session创建事件控制 public void sessionCreated(HttpSessionEvent arg0) { HttpSession session = arg0.getSession(); ServletContext context = session.getServletContext();

    Object o = context.getAttribute("count"); if(o!=null){
    int count = ((Integer)o).intValue();
    context.setAttribute("count",new Integer(count + 1));
    } }
        
        }