• 练习&复习


    一、Tomatcat数据源的配置

      1,配置server.xml。

    View Code
     1 <Context path="/web" docBase="E:\webstudy" reloadable="true" debug="0">
    2 <Resource name="jdbc/study"
    3 auth="Container"
    4 type="javax.sql.DataSource"
    5 maxActive="100"
    6 maxIdle="30"
    7 maxWait="10000"
    8 username="root"
    9 password="mysqladmin"
    10 driverClassName="org.gjt.mm.mysql.Driver"
    11 url="jdbc:mysql://localhost:3306/study_java"/>
    12 </Context>

      2,配置项目目录下的web.xml

    <resource-ref>
    <res-ref-name>jdbc/study</res-ref-name>
    <res-type>javax.sql.DataSource</res-type>
    <res-auth>Container</res-auth>
    </resource-ref>

      3,重启Tomcat服务器,编写测试练习

         a:编写Servlet。DataSourceTest.java

    View Code
     1 package org.xiong.tomcat.datasource;
    2 import java.util.List;
    3 import java.util.ArrayList;
    4 import java.util.Date;
    5 import java.io.IOException;
    6 import java.sql.Connection;
    7 import java.sql.DriverManager;
    8 import java.sql.PreparedStatement;
    9 import java.sql.ResultSet;
    10
    11 import javax.naming.Context;
    12 import javax.naming.InitialContext;
    13 import javax.sql.DataSource;
    14
    15 import javax.servlet.ServletException;
    16 import javax.servlet.http.HttpServlet;
    17 import javax.servlet.http.HttpServletRequest;
    18 import javax.servlet.http.HttpServletResponse;
    19
    20 import org.xiong.demo.vo.BookInfo;
    21
    22 public class DataSourceTest extends HttpServlet
    23 {
    24 public static final String DATASOURCE_NAME="java:comp/env/jdbc/study";
    25
    26 public void doGet(HttpServletRequest req,HttpServletResponse resp) throws ServletException,IOException
    27 {
    28 this.doPost(req,resp);
    29
    30 }
    31 public void doPost(HttpServletRequest req,HttpServletResponse resp) throws ServletException,IOException
    32 {
    33 List<BookInfo> listBook=new ArrayList<BookInfo>();
    34 Connection conn=null;
    35 PreparedStatement psmt=null;
    36 String sql="select book_id,book_name,book_author,book_publishers,book_price,book_shelves_date from BookInfo";
    37 try
    38 {
    39 Context cxt=new InitialContext();
    40 DataSource ds=(DataSource)cxt.lookup(DATASOURCE_NAME);
    41 conn=ds.getConnection();
    42 psmt=conn.prepareStatement(sql);
    43 ResultSet rst=psmt.executeQuery();
    44 while(rst.next())
    45 {
    46 BookInfo book=new BookInfo();
    47 book.setBook_id(rst.getInt(1));;
    48 book.setBook_name(rst.getString(2));
    49 book.setBook_author(rst.getString(3));
    50 book.setBook_publishers(rst.getString(4));
    51 book.setBook_price(rst.getFloat(5));
    52 book.setBook_shelves_date(new Date(rst.getDate(6).getTime()));
    53 listBook.add(book);
    54 }
    55 }
    56 catch(Exception ex)
    57 {
    58 ex.printStackTrace();
    59 }
    60 finally
    61 {
    62 try
    63 {
    64
    65 conn.close();
    66 }
    67 catch(Exception ex)
    68 {
    69 ex.printStackTrace();
    70 }
    71 }
    72 req.setAttribute("list",listBook);
    73 req.getRequestDispatcher("DataSourceTest1.jsp").forward(req,resp);
    74 }
    75 }

         b:编写输出页面

     

    View Code
     1 <%@ page contentType="text/html" pageEncoding="GBK"%>
    2 <%@ page import="java.util.*"%>
    3
    4 <center>
    5 <table border="1" width="80%">
    6 <tr>
    7 <th>图书编号</th>
    8 <th>图书名称</th>
    9 <th>&nbsp;&nbsp;&nbsp;&nbsp;</th>
    10 <th>&nbsp;&nbsp;</th>
    11 <th>&nbsp;&nbsp;&nbsp;&nbsp;</th>
    12 <th>上架日期</th>
    13 </tr>
    14 <%
    15 List lst=(List)request.getAttribute("list");
    16 Iterator iter=lst.iterator();
    17 while(iter.hasNext())
    18 {
    19 pageContext.setAttribute("book",iter.next());
    20 %>
    21 <tr>
    22 <td>${book.book_id}</td>
    23 <td>${book.book_name}</td>
    24 <td>${book.book_author}</td>
    25 <td>${book.book_publishers}</td>
    26 <td>${book.book_price}</td>
    27 <td>${book.book_shelves_date}</td>
    28 </tr>
    29 <%
    30 }
    31 %>
    32 </table>
    33 </center>

           c:配置servlet

    View Code
    1 <servlet>
    2 <servlet-name>datasource</servlet-name>
    3 <servlet-class>org.xiong.tomcat.datasource.DataSourceTest</servlet-class>
    4 </servlet>
    5 <servlet-mapping>
    6 <servlet-name>datasource</servlet-name>
    7 <url-pattern>/datasource_demo/DataSourceTest</url-pattern>
    8 </servlet-mapping>

    二,Filter练习,通过过滤器设置编码格式

      1,编写setCharachterEncode.java

    View Code
     1 package org.xiong.demo.filtertest;
    2
    3 import java.io.IOException;
    4 import javax.servlet.ServletException;
    5 import javax.servlet.ServletRequest;
    6 import javax.servlet.ServletResponse;
    7 import javax.servlet.Filter;
    8 import javax.servlet.FilterConfig;
    9 import javax.servlet.FilterChain;
    10 import javax.servlet.http.HttpServlet;
    11
    12 public class SetCharacterEncode implements Filter
    13 {
    14 private String str=null;
    15 public void init(FilterConfig filterConfig) throws ServletException
    16 {
    17 str=filterConfig.getInitParameter("ref");
    18 }
    19 public void doFilter(ServletRequest request,ServletResponse response,FilterChain chain) throws IOException,ServletException
    20 {
    21 request.setCharacterEncoding(str);
    22 chain.doFilter(request,response);
    23 }
    24 public void destroy()
    25 {
    26
    27 }
    28 }

      2,配置Filter

    View Code
     1 <filter>
    2 <filter-name>SetEncoding</filter-name>
    3 <filter-class>org.xiong.demo.filtertest.SetCharacterEncode</filter-class>
    4 <init-param>
    5 <param-name>ref</param-name>
    6 <param-value>GBK</param-value>
    7 </init-param>
    8 </filter>
    9 <filter-mapping>
    10 <filter-name>SetEncoding</filter-name>
    11 <url-pattern>/bookdemo/*</url-pattern>
    12 </filter-mapping>









  • 相关阅读:
    Flutter & Dart 安装在window系统
    HAWQ配置之客户端访问
    HAWQ配置之HDFS HA
    HAWQ集成Yarn HA作为资源管理服务
    ambari 安装HDP3.0.1后,启动服务的问题记录
    【Clojure 基本知识】小技巧s
    [转帖]Loading Data into HAWQ
    【Clojure 基本知识】 关于函数参数的各种高级用法
    【Clojure 基本知识】 ns宏的 指令(关键字) requrie的用法
    Linux系统解析域名的先后顺序【转帖】
  • 原文地址:https://www.cnblogs.com/xiongyu/p/2336213.html
Copyright © 2020-2023  润新知