訪問:瀏覽器輸入:http://localhost:8080 回車訪問本身
http://別人的ip:8080 訪問別人php
<Connector port="8888" protocol="HTTP/1.1" connectionTimeout="20000" redirectPort="8445" />
在<Host>標籤體中配置 <Context docBase="D:\hello" path="/hehe" /> <!-- docBase:項目存放的路徑 --> <!-- path:虛擬目錄 -->
<Context docBase="D:\hello" /> <!-- * 虛擬目錄:即爲xml文件的名稱 -->
Servlet就是一個接口,定義了Java類被瀏覽器訪問到(tomcat識別)的規則。css
配置Servlet
在web.xml中配置:html
<!--配置Servlet --> <servlet> <servlet-name>demo1</servlet-name> <servlet-class>cn.web.servlet.ServletDemo1</servlet-class> </servlet> <servlet-mapping> <servlet-name>demo1</servlet-name> <url-pattern>/demo1</url-pattern> </servlet-mapping>
Servlet中的生命週期方法:java
//註解實現 @Target({ElementType.TYPE}) @Retention(RetentionPolicy.RUNTIME) @Documented public @interface WebServlet { String name() default "";//至關於<Servlet-name> String[] value() default {};//表明urlPatterns()屬性配置 String[] urlPatterns() default {};//至關於<url-pattern> int loadOnStartup() default -1;//至關於<load-on-startup> WebInitParam[] initParams() default {}; boolean asyncSupported() default false; String smallIcon() default ""; String largeIcon() default ""; String description() default ""; String displayName() default ""; }
//源碼 protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { String method = req.getMethod(); long lastModified; if (method.equals("GET")) { lastModified = this.getLastModified(req); if (lastModified == -1L) { this.doGet(req, resp); } else { long ifModifiedSince; try { ifModifiedSince = req.getDateHeader("If-Modified-Since"); } catch (IllegalArgumentException var9) { ifModifiedSince = -1L; } if (ifModifiedSince < lastModified / 1000L * 1000L) { this.maybeSetLastModified(resp, lastModified); this.doGet(req, resp); } else { resp.setStatus(304); } } } else if (method.equals("HEAD")) { lastModified = this.getLastModified(req); this.maybeSetLastModified(resp, lastModified); this.doHead(req, resp); } else if (method.equals("POST")) { this.doPost(req, resp); } else if (method.equals("PUT")) { this.doPut(req, resp); } else if (method.equals("DELETE")) { this.doDelete(req, resp); } else if (method.equals("OPTIONS")) { this.doOptions(req, resp); } else if (method.equals("TRACE")) { this.doTrace(req, resp); } else { String errMsg = lStrings.getString("http.method_not_implemented"); Object[] errArgs = new Object[]{method}; errMsg = MessageFormat.format(errMsg, errArgs); resp.sendError(501, errMsg); } }
tomcat真正訪問的是「tomcat部署的web項目」,對應着"工做空間項目" 的web目錄下的全部資源web
WEB-INF目錄下的資源不能被瀏覽器直接訪問。apache