(个人学习笔记)JSP 数据库连接池,上传文件

连接池

连接池是创建和管理一个连接的缓冲池的技术,这些连接准备好被任何需要它们的线程使用。

什么是数据库连接池?

数据库连接池(Connection pooling)是程序启动时建立足够的数据库连接,并将这些连接组成一个连接池,由程序动态地对池中的连接进行申请,使用,释放。

测试:
在Tomcat的conf目录下找到context.xml并编辑,复制以下代码:
<Context>
<Environment name="tjndi" value="hello JNDI" type="java.lang.String"/>
</Context>
在jsp文件里写上以下代码调用,输出结果为”hello JNDI”:
Context context=new InitialContext();
String message = (String)context.lookup("java:comp/env/tjndi");
out.print(message);
在Tomcat的conf目录下找到context.xml并编辑,复制以下代码:
<Context>
   <Resource name="jdbc/dept" auth="Container" type="javax.sql.DataSource"
      maxActive="100" maxIdle="30" maxWait="10000" username="root"
      password="root" driverClassName="com.mysql.jdbc.Driver"
      url="jdbc:mysql://127.0.0.1:3306/dept?
              useUnicode=true&amp;characterEncoding=utf-8" />
</Context>

在项目的WEB-INF目录下找到web.xml并编辑,复制以下代码:

<resource-ref>

    <res-ref-name>jdbc/dept</res-ref-name>

    <res-type>javax.sql.DataSource</res-type>

    <res-auth>Container</res-auth>

</resource-ref>

在BaseDao中复制以下代码

public Connection getConnection() {
        Context ctx;
        try {
            ctx = new InitialContext();
            DataSource ds=(DataSource)ctx.lookup("java:comp/env/jdbc/dept");
            conn=ds.getConnection();        
        } catch (NamingException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }  
        return conn;
    }

文件上传

在index.jsp文件里复制以下代码,设置一个form表单,提交到doupload.jsp文件:

<body>
    <form action="doupload.jsp" enctype="multipart/form-data" method="post">
        用户名 <input type="text" name="name"> 文件: <input type="file"
            name="ufile"> <input type="submit" value="确定">
    </form>

</body>

在doupload.jsp文件里复制以下代码:

<%
    //设置编码格式
    request.setCharacterEncoding("UTF-8");
    //上传的地址
    String uploadPath = request.getSession().getServletContext()
            .getRealPath("upload");
    //检查请求类型
    boolean flag = ServletFileUpload.isMultipartContent(request);
    if (flag) {
        FileItemFactory fileItemFactory = new DiskFileItemFactory();
        ServletFileUpload fileUpload = new ServletFileUpload(
                fileItemFactory);
        List<FileItem> list = fileUpload.parseRequest(request);
        //转化为迭代器
        Iterator<FileItem> iterator = list.iterator();
        while (iterator.hasNext()) {
          // 上传的数据
            FileItem fileItem = iterator.next();
            //是否是文件类型
            if (fileItem.isFormField()) {

            } else {
                File saveFile = new File(fileItem.getName());
                File uploadFile = new File(uploadPath,saveFile.getName());
                fileItem.write(uploadFile);
            }
        }
    } else {
        out.print("上传不合法");
    }
%>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值