Packagecontext;Importjava.io.IOException;ImportJavax.servlet.ServletContext;Importjavax.servlet.ServletException;ImportJavax.servlet.http.HttpServlet;Importjavax.servlet.http.HttpServletRequest;ImportJavax.servlet.http.HttpServletResponse;/*** Get Web App path *@authorAdministrator **/ Public classContextDemo1extendsHttpServlet { Public voiddoget (httpservletrequest request, httpservletresponse response)throwsservletexception, IOException {//1. Get ServletContext Object//ServletContext context = This.getservletconfig (). Getservletcontext ();ServletContext context = This. Getservletcontext ();//Recommended Use//2. Get the Web application path/day10 /** Web App Path: * Web App name deployed to a Tomcat server*/String ContextPath=Context.getcontextpath (); System.out.println (ContextPath); /** Case: Apply to request redirect*/Response.sendredirect (ContextPath+ "/index.jsp"); }}
Servlet gets web App path