标签:ping print otherwise sql cep contex which get tps
<!-- 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>
@Override
public void init(ServletConfig servletConfig) throws ServletException {
System.out.println("the servlet-name is:" + servletConfig.getServletName()); // the servlet-name is:HelloServlet
}
@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
}
@Override
public void init(ServletConfig servletConfig) throws ServletException {
servletConfig.getServletContext();
}
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>
@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
}
@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
}
@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/
}
// 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
}
标签:ping print otherwise sql cep contex which get tps
原文地址:https://www.cnblogs.com/nedrain/p/13167815.html