Servlet3规范目录结构

平台的目录结构如下:

把静态资源、jsp等信息全部放到resources/META-INF目录下,这块不是很理解,我之前都是放到webapp/WEB-INF目录下,这样不能通过url直接访问,网上查看到一片博客讲的比较清楚,记录如下:

         在Servlet3协议规范中,包含在JAR文件/META-INFO/resources/路径下的资源可以直接访问了。这么说一说,可能感觉不到到底有什么好处,以往的JSP或者HTML页面只能存在站点的目录下,或者在WEB-INF目录下(只是不能直接访问)。

规范说,${jar}/META-INF/resources/被视为根目录,假设home.jsp被放在${jar}/META-INF/resources/home.jsp,用户可以直接通过 http://域名/home.jsp 访问了。

呈现一个常见的代码片段:

/**
 * 简单示范
 * @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);
 }
}

而jarhome.jsp文件路径则位于 ${jar}/META-INF/resources/jarpage/jarhome.jsp,当然jarhome.jsp文件则没有什么特别之处:

<%@ 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>

jarhome.jsp文件所引用css/style.css和img/j2ee.png等文件分别位于 ${jar}/META-INF/resources/css/style.css${jar}/META-INF/resources/img/j2ee.png目录下。把生成的jar文件存放在 WEB-INF/lib/ 中,下面为一个运行示范图:

样式和图片等都可以正常访问。有时候可能需要使用路径信息等,再看一个示范吧:

/**
 * 演示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");
 }
}

输出命令行信息为:

[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!

可以看到getRealPath("/")得到的是项目的根路径(可以参照JAR解压后的路径)。而request.getPathTranslated则因为是包含在jar文件中(规范中说包含在远程主机、数据库、JAR存档文件中,getPathTranslated都会返回null),则返回null。

包含在jar中的资源文件,一样可以使用getResourceAsStream获取到。再来看看jar文件META-INF/resources目录下文件结构:

  • css/style.css
  • img/j2ee.png
  • jarfile/demo.txt
  • jarpage/jarhome.jsp
  • jsp/h.jsp
  • jsp/helloWorld.jsp

很显然,就是一个小型站点目录结构。每一个模块,建立一个WEB站点应用,使用ANT脚本自动打包成jar文件,拷贝到真正站点WEB-INF/lib下。假设一个JAR文件包含一个具体的模块,那么模块的部署与装载将是十分方便的。

参考:

https://openhome.cc/Gossip/ServletJSP/DirectoryStructure.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值