• 吴裕雄--天生自然JAVA开发JSP-Servlet学习笔记:访问Servlet的配置参数


    /*
     * To change this license header, choose License Headers in Project Properties.
     * To change this template file, choose Tools | Templates
     * and open the template in the editor.
     */
    
    import javax.servlet.http.*;
    import javax.servlet.*;
    import javax.servlet.annotation.*;
    import java.io.*;
    import java.sql.*;
    
    /**
     *
     * @author Administrator
     */
    @WebServlet(name = "testServlet",
            urlPatterns = {"/testServlet"},
            initParams = {
                @WebInitParam(name = "driver", value = "com.mysql.cj.jdbc.Driver")
                ,
            @WebInitParam(name = "url", value = "jdbc:mysql://localhost:3306/taobao")
                ,
            @WebInitParam(name = "user", value = "root")
                ,
            @WebInitParam(name = "pass", value = "admin")})
    public class TestServlet extends HttpServlet {
            // 重写init方法
        public void init(ServletConfig config) throws ServletException {
            // 重写该方法,应该首先调用父类的init()方法
            super.init(config);
        }
        // 响应客户端请求的方法
        public void service(HttpServletRequest request,HttpServletResponse response)
                throws ServletException, java.io.IOException {
            try {
                // 获取ServletConfig对象
                ServletConfig config = getServletConfig();
                // 通过ServletConfig对象获取配置参数:dirver
                String driver = config.getInitParameter("driver");
                // 通过ServletConfig对象获取配置参数:url
                String url = config.getInitParameter("url");
                // 通过ServletConfig对象获取配置参数:user
                String user = config.getInitParameter("user");
                // 通过ServletConfig对象获取配置参数:pass
                String pass = config.getInitParameter("pass");
                // 注册驱动
                Class.forName(driver);
                // 获取数据库连接
                Connection conn = DriverManager.getConnection(url, user, pass);
                // 创建Statement对象
                Statement stmt = conn.createStatement();
                // 执行查询,获取ResuletSet对象
                ResultSet rs = stmt.executeQuery("select * from person");
                response.setContentType("text/html;charSet=UTF-8");
                // 获取页面输出流
                PrintStream out = new PrintStream(response.getOutputStream());
                // 输出HTML标签
                out.println("<html>");
                out.println("<head>");
                out.println("<title>访问Servlet初始化参数测试</title>");
                out.println("</head>");
                out.println("<body>");
                out.println("<table bgcolor="#9999dd" border="1""
                        + "width="480">");
                // 遍历结果集
                while (rs.next()) {
                    // 输出结果集内容
                    out.println("<tr>");
                    out.println("<td>" + rs.getInt(1) + "</td>");
                    out.println("<td>" + rs.getString(2) + "</td>");
                    out.println("<td>" + rs.getInt(3) + "</td>");
                    out.println("</tr>");
                }
                out.println("</table>");
                out.println("</body>");
                out.println("</html>");
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
    <?xml version="1.0" encoding="UTF-8"?>
    <web-app metadata-complete="true" version="3.1" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee                       http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd">
        <!-- 配置Servlet的名字 -->
        <servlet>
            <!-- 指定Servlet的名字,
            相当于指定@WebServlet的name属性 -->
            <servlet-name>firstServlet</servlet-name>
            <!-- 指定Servlet的实现类 -->
            <servlet-class>FirstServlet</servlet-class>
        </servlet>
        <!-- 配置Servlet的URL -->
        <servlet-mapping>
            <!-- 指定Servlet的名字 -->
            <servlet-name>firstServlet</servlet-name>
            <!-- 指定Servlet映射的URL地址,
            相当于指定@WebServlet的urlPatterns属性-->
            <url-pattern>/aa</url-pattern>
        </servlet-mapping>
        <servlet>
            <!-- Servlet名 -->
            <servlet-name>timerServlet</servlet-name>
            <!-- Servlet的实现类 -->
            <servlet-class>TimerServlet</servlet-class>
            <!-- 配置应用启动时,创建Servlet实例
            ,相当于指定@WebServlet的loadOnStartup属性-->
            <load-on-startup>1</load-on-startup>
        </servlet>
        <servlet>
            <!-- 配置Servlet名 -->
            <servlet-name>testServlet</servlet-name>
            <!-- 指定Servlet的实现类 -->
            <servlet-class>TestServlet</servlet-class>
            <!-- 配置Servlet的初始化参数:driver -->
            <init-param>
                <param-name>driver</param-name>
                <param-value>com.mysql.cj.jdbc.Driver</param-value>
            </init-param>
            <!-- 配置Servlet的初始化参数:url -->
            <init-param>
                <param-name>url</param-name>
                <param-value>jdbc:mysql://localhost:3306/taobao</param-value>
            </init-param>
            <!-- 配置Servlet的初始化参数:user -->
            <init-param>
                <param-name>user</param-name>
                <param-value>root</param-value>
            </init-param>
            <!-- 配置Servlet的初始化参数:pass -->
            <init-param>
                <param-name>pass</param-name>
                <param-value>admin</param-value>
            </init-param>
        </servlet>
        <servlet-mapping>
            <!-- 确定Servlet名 -->
            <servlet-name>testServlet</servlet-name>
            <!-- 配置Servlet映射的URL -->
            <url-pattern>/testServlet</url-pattern>
        </servlet-mapping>
        <welcome-file-list>
            <welcome-file>index.html</welcome-file>
            <welcome-file>index.htm</welcome-file>
            <welcome-file>index.jsp</welcome-file>
        </welcome-file-list>
    </web-app>

  • 相关阅读:
    Jquery 公告 滚动+AJAX后台得到数据。
    图片上添加文字。
    javascript的window.onload与jquery的$(document).ready()
    后台JS写法
    Ajax xmlhttprequest原理(一)
    SQL语句二次排序。先根据是否置顶字段。再根据最后更新时间排序。
    引以为戒的SQL语句写法
    C#中var关键字怎么用 ~
    上传文件 解析
    简易公告
  • 原文地址:https://www.cnblogs.com/tszr/p/12685407.html
Copyright © 2020-2023  润新知