1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
| package com.xiaoguan.learn; import jakarta.servlet.GenericServlet; import jakarta.servlet.ServletException; import jakarta.servlet.ServletRequest; import jakarta.servlet.ServletResponse;
import java.io.IOException; import java.io.PrintWriter; import java.util.Enumeration; import java.util.Objects;
public class ServletContext extends GenericServlet { @Override public void service(ServletRequest req, ServletResponse res) throws ServletException, IOException { res.setContentType("text/html"); PrintWriter out=res.getWriter(); jakarta.servlet.ServletContext application=this.getServletContext(); Enumeration<String> strs=application.getInitParameterNames(); while (strs.hasMoreElements()){ String name= strs.nextElement(); String value=application.getInitParameter(name); out.print(name+","+value+"<br>"); } String contextPath= application.getContextPath(); out.print(contextPath+"<br>"); String realPath = application.getRealPath("/index.html"); out.print(realPath+"<br>"); application.log("大家好!"); int age=17; if(age<18){ application.log("错误",new RuntimeException("go!")); } application.setAttribute("test0",12345); application.removeAttribute("test0"); int test0 = (int) application.getAttribute("test0"); out.print(test0);
} }
|