Resin路径与JSP路径
时间:2010-05-20 来源:hkebao
Resin路径与JSP路径
时间:2010-5-20
1. Resin配置内容如下
<host id="">
<web-app id="/" document-directory="D:\bjproj">
<welcome-file-list>login.jsp</welcome-file-list>
<character-encoding>UTF-8</character-encoding>
<stderr-log path='D:\bjproj\syslog\stderr.log' rollover-size="2mb" />
<stdout-log path='D:\bjproj\syslog\stdout.log' rollover-size='2mb' />
</web-app>
<web-app id="/test" document-directory="D:\myweb\proj1">
<welcome-file-list>login.jsp</welcome-file-list>
<character-encoding>UTF-8</character-encoding>
<stderr-log path='D:\myweb\proj1\stderr.log' rollover-size="2mb" />
<stdout-log path='D:\myweb\proj1\stdout.log' rollover-size='2mb' />
</web-app>
<web-app id="/test2" document-directory="D:\myweb\proj2">
<welcome-file-list>login.jsp</welcome-file-list>
<character-encoding>UTF-8</character-encoding>
<stderr-log path='D:\myweb\proj2\stderr.log' rollover-size="2mb" />
<stdout-log path='D:\myweb\proj2\stdout.log' rollover-size='2mb' />
</web-app>
</host>
现在我来写第一个WEB应用并且将代码布置到/test1 目录下面去
布置之后我的电脑上面的内容
D:\myweb\proj1
里面存放了META-INF WEB-INF index.jsp main.jsp
OK:然后我的index.jsp里面写
<form action="/servlet/UserServlts" method="post" name="form1">
<input type="input" name="names">
<input type="submit">
</form>
然后运行看下:
http://localhost:8080/test/index.jsp 带上了虚拟目录的名称哦!
好了。我们来提交这个表单提交的URL为:http://localhost:8080/servlet/UserServlts
怪事来了!怎么会跑到这个目录下面去呢?
针对这个问题我咨询了相关的系统配置工程师。得到的结论是将JSP代码修改成:
<form action="/test/servlet/UserServlts" method="post" name="form1">
<input type="input" name="names">
<input type="submit">
</form>
然后我的servlets里面这样来写
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String name = request.getParameter("names");
response.sendRedirect("/test/main.jsp");
}
结论:
就是代码里面将 当前的WEB名称写死。
我想这样肯定是不对了。试想要是我的WEB站点名称修改那不是全报 404 了???
解决办法:依据程序员的身份就是修改代码了!
表单的JSP我们修改为:
String path = request.getContextPath();
<form action="<%=path%>/servlet/UserServlts" method="post" name="form1">
<input type="input" name="names">
<input type="submit">
</form>
Servlets代码
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String path = request.getContextPath();
System.out.println(path);
String name = request.getParameter("names");
response.sendRedirect(path+"/main.jsp");
}
思路:就是说我们将当前跑的JSP的RESIN中的站点根目录提取出来!放入到一个变量path里面去!
然后在任何一个需要做交换的地方将此变量添加进来
优势:有利于站点的移植!
网上有位同仁:
我在tomcat中的部署多个应用,这样会导致不同的应用有不同的上下文路径,可以通过request.getContextPath()方法得 到。在写jsp页面的时候,为了能够导入js,图片,css等资源文件,我们都在路径前加上request.getContextPath(),用来动态 的得到这些资源的绝对路径。但是在使用<%@ include file=""%>标签的时候,类似的加上request.getContextPath(),这里由于不能动态的得到,所以我直接写的应用根目录 如:<%@ include file="/hb/common/head.inc"%>,居然报找不到include文件"/hb/common/head.inc。我看了半 天,还把上述路径打到浏览器中,都能正常访问,这个让我很不能理解。后来尝试去掉应用的上下文路径,直接为<%@ include file="/common/head.inc"%>,编译成功,原来include标签的路径中是不需要包含上下文路径的,编译时会自动识别。后 来想想,由于标签中不能嵌入<%=reqeust.getContextPath()%>的写法,如果要包含上下文路径,只能硬编码,如果遇 到应用名称更换的话,要对所有的上述标签做查找替换工作,这样的开发方式肯定是存在问题的。
注意:JSP的include标签是不用写什么相对路径的。
直接为<%@ include file="/common/head.inc"%>原来include标签的路径中是不需要包含上下文路径的,编译时会自动识别。因为标签中不可以动态说把<%=reqeust.getContextPath()%> 添加进来所以编译 器会自动识别并处理掉!
解决的结论:
1、处理这种带有上下文站点的情况我们使用String path = request.getContextPath();
获取其上下文名称如这里面为:/test 让它来配置形成我们需要的URL值!
2、Resin可以配置多个虚拟目录。
二、resin里面的主机头设置 即
<host id="gryffindor.caucho.com">
这个里面的id 其实这个ID的意思与IIS里面的主机头配置原理 是一样的。来我们现在来测试一下
我现在配置了这个ID值了。然后我输入
http://gryffindor.caucho.com:8080/test/main.jsp
发现报 404
为啥呢?因为这个域名解析不到。所以需要设置一下HOSTS文件
127.0.0.1 gryffindor.caucho.com 现在再打开就可以出来了!
然后我们再这样输入
http://127.0.0.1:8080/test/main.jsp 发现报404了为啥呢?
因为我们设置了主机头。不可以再输入这样的URL了!