The directory structure of the platform is as follows:
Put all the static resources, jsp and other information in resources/META-INF directory, which is not very understood. I used to put them in webapp/WEB-INF directory, so they can not be accessed directly through url. I can see a blog on the Internet clearly, which is recorded as follows:
In the Servlet3 protocol specification, resources contained in the JAR file/META-INFO/resources/path are directly accessible.In other words, you may not feel any benefit at all. Previous JSP or HTML pages can only exist in the site's directory, or in the WEB-INF directory (they can only be accessed directly).
The specification says, ${jar}/META-INF/resources/is considered the root directory, assuming that home.jsp is placed in ${jar}/META-INF/resources/home.jsp, and users can access it directly through http://domain name/home.jsp.
Presents a common snippet of code:
/** * Simple demonstration * @author yongboy * @date 2011-1-16 * @version 1.0 */ @WebServlet("/jarHome") public class HelloJarServlet extends HttpServlet { private static final long serialVersionUID = 6177346686921106540L; protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { request.setAttribute("date", new Date()); request.getRequestDispatcher("/jarpage/jarhome.jsp").forward(request, response); } }
The path to the jarhome.jsp file is in ${jar}/META-INF/resources/jarpage/jarhome.jsp, although the jarhome.jsp file is nothing special:
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <link rel="stylesheet" href="css/style.css" type="text/css" /> <title>Welcome to jar home</title> </head> <body> <img alt="j2ee" src="img/j2ee.png" /><br/> <br/> now date : <%=((java.util.Date)request.getAttribute("date")).toString()%> </body> </html>
The files css/style.css and img/j2ee.png referenced by the jarhome.jsp file are located in the ${jar}/META-INF/resources/css/style.css and ${jar}/META-INF/resources/img/j2ee.png directories, respectively.Store the generated jar file in WEB-INF/lib/Below is a running demonstration diagram:
Styles, pictures, and so on can be accessed normally.Sometimes you may need to use path information, etc. Just take a look at a demonstration:
/** * Demo jarDemo * * @author yongboy * @date 2011-1-16 * @version 1.0 */ @WebServlet("/jarDemo") public class DemoWebINFPagesServlet extends HttpServlet { private static final long serialVersionUID = -1040850432869481349L; private static final Log log = LogFactory .getLog(DemoWebINFPagesServlet.class); @SuppressWarnings("deprecation") @Override protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { log.info("the /jarDemo is accessed now!"); log.info("getRealPath : " + request.getRealPath("/")); log.info("ServletContext : getRealPath : " + getServletContext().getRealPath("/")); log.info("getPathTranslated : " + request.getPathTranslated()); log.info("get jar's resource:"); InputStream is = getServletContext().getResourceAsStream( "/jarfile/demo.txt"); log.info("the JAR/META-INF/resources/jarfile/demo.txt's content is :\n" + IOUtils.toString(is)); request.getRequestDispatcher("/WEB-INF/pages/notaccess.jsp"); } }
The output command line information is:
[framework] 2011-01-03 11:45:16,664 - com.learn.servlet3.jardync.DemoWebINFPagesServlet -798292 [http-8080-exec-6] INFO com.learn.servlet3.jardync.DemoWebINFPagesServlet - the /jarDemo is accessed now! [framework] 2011-01-03 11:45:16,664 - com.learn.servlet3.jardync.DemoWebINFPagesServlet -798292 [http-8080-exec-6] INFO com.learn.servlet3.jardync.DemoWebINFPagesServlet - getRealPath : /home/yongboy/workspace/.metadata/.plugins/org.eclipse.wst.server.core/tmp0/wtpwebapps/d/ [framework] 2011-01-03 11:45:16,664 - com.learn.servlet3.jardync.DemoWebINFPagesServlet -798292 [http-8080-exec-6] INFO com.learn.servlet3.jardync.DemoWebINFPagesServlet - ServletContext : getRealPath : /home/yongboy/workspace/.metadata/.plugins/org.eclipse.wst.server.core/tmp0/wtpwebapps/d/ [framework] 2011-01-03 11:45:16,665 - com.learn.servlet3.jardync.DemoWebINFPagesServlet -798293 [http-8080-exec-6] INFO com.learn.servlet3.jardync.DemoWebINFPagesServlet - getPathTranslated : null [framework] 2011-01-03 11:45:16,665 - com.learn.servlet3.jardync.DemoWebINFPagesServlet -798293 [http-8080-exec-6] INFO com.learn.servlet3.jardync.DemoWebINFPagesServlet - get jar's resource: [framework] 2011-01-03 11:45:16,665 - com.learn.servlet3.jardync.DemoWebINFPagesServlet -798293 [http-8080-exec-6] INFO com.learn.servlet3.jardync.DemoWebINFPagesServlet - the ${JAR}/META-INF/resources/jarfile/demo.txt's content is : haha,the demo.s's content! haha,haha,haha!
You can see that getRealPath ('/') gets the root path of the project (refer to the JAR unpacked path).Requ.getPathTranslated returns null because it is contained in a jar file (the specification states that getPathTranslated returns null in remote hosts, databases, and JAR archives).
Resource files included in jar can also be obtained using getResourceAsStream.Let's look at the file structure in the jar file META-INF/resources directory:
- css/style.css
- img/j2ee.png
- jarfile/demo.txt
- jarpage/jarhome.jsp
- jsp/h.jsp
- jsp/helloWorld.jsp
Clearly, this is a small site directory structure.Each module establishes a WEB site application, automatically packages into jar files using ANT scripts, and copies to the real site WEB-INF/lib.Assuming that a JAR file contains a specific module, the deployment and loading of the module will be very convenient.
Reference resources:
https://openhome.cc/Gossip/ServletJSP/DirectoryStructure.html