"Java Servlet Programming" notes (get information)

xiaoxiao2021-03-06  80

To create a successful servlet program, you usually need to know a lot of information about the running environment, you might want to understand information running the Servlet server or the information that is sending the requested client. Servlet provides many ways to access this information, each method returns a specific result .

■ servlet □ get servlet servlet with initialization parameters getInitParmeter () method to access its initialization parameters: public String servletConfig.getInitParameter (String name) GeneicServlet class implements the interface servletConfig and provides direct access to getInitParameter method So you can call:. Public void init () throws ServletException {string greeting = getInitParameter ( "greeting");} □ servlet initialization parameters acquired by the getInitParameterNames servlet name () to initialize the detection parameters: public enumeration servletConfig.getInitParameterNames () this method returns a string object to include the servlet All initialization parameters, or no parameters, return empty pairs. //Printinit.java // Print all initialization parameter names and value import java.io. *; import java.utilt. *; Public. class PrintInit extends GenericServlet {public void service (ServletRequest req, ServletResponse res) throws ServletException, IOException {res.setContentType ( "text / plain"); PrintWriter out = res.getWriter (); out.println ( "init parameters"); Enumeration enum = getinitParameterNames (); while (enum.hasmoreElements ()) {string name = (String) enum.nexTelement (); // get the parameter name Out.println (Name ": GetInitParameter (Name)); // Get parameter value}}}}}} □ Get a servlet name to return a servlet registration name in ServletConfig: public string servletconfig.getServletName () Returns the class name if the servlet is not registered. This method is written in a servlet instance status in a shared method like a database or like a database. when information is useful for or servlet SessionContext public void doGet (HttpServeltRequest req, HttpServetResponse res) throws ServletException, IOException {String name = getServletName ();. servletContext context = getServletContext (); Object value = context.getAttribute (name " .state ");} to obtain server information> obtained server name public String servletRequest.getServerName ()> server port number obtained public int servletRequest.getServerPort () method is called by the following servletContext objects, for example: servletContext context = getServletContext (); out. PRINTLN (CONTEXT.GetServletInfo ());>

Get Server Information Public String ServletContext.getServerInfo () Get Properties PUBLIC Object ServletContext.getaTRibute (String Name)

转载请注明原文地址:https://www.9cbs.com/read-121762.html

New Post(0)