4.13
程序员文章站
2022-07-15 08:48:05
...
package demo;
import javax.servlet.ServletConfig;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Enumeration;
public class Demo01 extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
ServletConfig servletConfig = getServletConfig();
Enumeration<String> initParameterNames = servletConfig.getInitParameterNames();
System.out.println(servletConfig.getServletName());
while (initParameterNames.hasMoreElements()){
String s = initParameterNames.nextElement();
String initParameter = servletConfig.getInitParameter(s);
System.out.println("value = " + initParameter + " name = " + s);
}
ServletContext servletContext = servletConfig.getServletContext();
Enumeration<String> initParameterNames1 = servletContext.getInitParameterNames();
while (initParameterNames1.hasMoreElements()){
String s = initParameterNames1.nextElement();
String initParameter = servletContext.getInitParameter(s);
System.out.println("value = " + initParameter + " name = " + s);
}
String upload = servletContext.getRealPath("upload");
System.out.println(upload);
Integer count = (Integer) servletContext.getAttribute("count");
if(count==null){
count = 1;
servletContext.setAttribute("count",count);
System.out.println("count = " + count);
}else {
count++;
System.out.println("count = " + count);
servletContext.setAttribute("count",count);
}
System.out.println("demo01 doGet");
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doGet(req, resp);
}
}
<?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">
<!-- -->
<servlet>
<servlet-name>Demo01</servlet-name>
<servlet-class>demo.Demo01</servlet-class>
<init-param>
<param-name>name</param-name>
<param-value>tom</param-value>
</init-param>
<init-param>
<param-name>password</param-name>
<param-value>1234</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>Demo01</servlet-name>
<url-pattern>/demo01</url-pattern>
</servlet-mapping>
<context-param>
<param-name>name</param-name>
<param-value>jack</param-value>
</context-param>
</web-app>
上一篇: 企业研发发展的方向