码迷,mamicode.com
首页 > 其他好文 > 详细

Servlet

时间:2020-06-20 13:07:29      阅读:51      评论:0      收藏:0      [点我收藏+]

标签:ping   print   otherwise   sql   cep   contex   which   get   tps   

ServletConfig class

<!-- a part of web.xml -->
<servlet>
        <servlet-name>HelloServlet</servlet-name>
        <servlet-class>com.truman.servlet.HelloServlet</servlet-class>
        
        <init-param>
            <param-name>username</param-name>
            <param-value>root</param-value>
        </init-param>
        <init-param>
            <param-name>url</param-name>
            <param-value>jdbc:mysql://localhost:3306/test</param-value>
        </init-param>
        
    </servlet>
    <servlet-mapping>
        <servlet-name>HelloServlet</servlet-name>
        <url-pattern>/hello</url-pattern>  <!--  This is important -->
    </servlet-mapping>

get "servlet-name"

 @Override
    public void init(ServletConfig servletConfig) throws ServletException {
        System.out.println("the servlet-name is:" + servletConfig.getServletName());  // the servlet-name is:HelloServlet
    }

get "init-param"

 @Override
    public void init(ServletConfig servletConfig) throws ServletException {
        System.out.println(servletConfig.getInitParameter("username")); //root
        System.out.println(servletConfig.getInitParameter("url")); //jdbc:mysql://localhost:3306/test
    }

get ServletContext object

  @Override
    public void init(ServletConfig servletConfig) throws ServletException {
        servletConfig.getServletContext();
    }

ServletContext interface

There is only one ServletContext object in a web project
ServletContext is a domain object which means it can work like a map object, the name domain means the effective region

map put() get() remove()
ServletContext setAttribute() getAttribute() removeAttribute()
<?xml version="1.0" encoding="UTF-8"?>
<web-app 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_4_0.xsd"
         version="4.0">
    <context-param>   <!-- The value here belongs to the whole web project -->
        <param-name>username</param-name>
        <param-value>context</param-value>
    </context-param>
    <context-param>
        <param-name>password</param-name>
        <param-value>root</param-value>
    </context-param>
    
    <servlet>
        <servlet-name>HelloServlet</servlet-name>
        <servlet-class>com.truman.servlet.HelloServlet</servlet-class>
        
        <init-param>
            <param-name>username</param-name>
            <param-value>root</param-value>
        </init-param>
        <init-param>
            <param-name>url</param-name>
            <param-value>jdbc:mysql://localhost:3306/test</param-value>
        </init-param>
        
    </servlet>
    <servlet-mapping>
        <servlet-name>HelloServlet</servlet-name>
        <url-pattern>/hello</url-pattern>  <!--  This is important -->
    </servlet-mapping>
    
</web-app>

get context-param from web.xml (which belongs to the whole web project)

  @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        ServletConfig servletConfig = getServletConfig();
        ServletContext context = servletConfig.getServletContext();
//        String username = servletContext.getAttribute("username");
        String username = context.getInitParameter("username");
        System.out.println("value of context-param ‘username‘ is:" + username); //value of context-param ‘username‘ is:context
        System.out.println("value of context-param ‘password‘ is:" + context.getInitParameter("password")); //value of context-param ‘password‘ is:root

    }

get current project path

 @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        ServletConfig servletConfig = getServletConfig();
        ServletContext context = servletConfig.getServletContext();
//        String username = servletContext.getAttribute("username");
//        String username = context.getInitParameter("username");
//        System.out.println("value of context-param ‘username‘ is:" + username);
//        System.out.println("value of context-param ‘password‘ is:" + context.getInitParameter("password"));
        System.out.println(context.getContextPath()); //   /thirdTry
    }

get the absolute path of EVERY FILE OR DIRECTORY in the project (Which means the DEPLOYMENT ADDRESS in the DISK)

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        ServletConfig servletConfig = getServletConfig();
        ServletContext context = servletConfig.getServletContext();
           // here, "/" means the root of the project
        System.out.println(context.getRealPath("/")); // /Users/nedrain/IdeaProjects/thirdTry/target/thirdTry/
    }

the Attribute (which can be seen through the whole project AFTER the INITIALIZATION of the <key, value> otherwise will be null)

// from HelloServlet2.java
  @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        ServletConfig servletConfig = getServletConfig();
        ServletContext context = servletConfig.getServletContext();
        context.setAttribute("key1","value1");
        System.out.println(context.getAttribute("key1"));  // output is: value1
    }

// from HelloServlet3.java
 protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        ServletContext context = getServletContext();
        System.out.println("Here is a test for helloservlet3");
        System.out.println(context.getAttribute("key1"));  // the output is also: value1
    }

Servlet

标签:ping   print   otherwise   sql   cep   contex   which   get   tps   

原文地址:https://www.cnblogs.com/nedrain/p/13167815.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!