﻿<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/"><channel><title>BlogJava-向东技术博客-随笔分类-开发技巧</title><link>http://www.blogjava.net/meil/category/16039.html</link><description>非澹泊无以明志,非宁静无以致远. </description><language>zh-cn</language><lastBuildDate>Fri, 02 Mar 2007 06:52:11 GMT</lastBuildDate><pubDate>Fri, 02 Mar 2007 06:52:11 GMT</pubDate><ttl>60</ttl><item><title>java(Web)中相对路径，绝对路径问题总结</title><link>http://www.blogjava.net/meil/archive/2006/10/10/73908.html</link><dc:creator>向东博客</dc:creator><author>向东博客</author><pubDate>Tue, 10 Oct 2006 00:58:00 GMT</pubDate><guid>http://www.blogjava.net/meil/archive/2006/10/10/73908.html</guid><wfw:comment>http://www.blogjava.net/meil/comments/73908.html</wfw:comment><comments>http://www.blogjava.net/meil/archive/2006/10/10/73908.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/meil/comments/commentRss/73908.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/meil/services/trackbacks/73908.html</trackback:ping><description><![CDATA[前言：<br /> 前一段时间，由于在处理Web应用下的文件创建与移动等，因此涉及到很多关于java<br />中相对路径，绝对路径等问题。同时，对于Web应用中的相对路径，绝对路径，以及Java.io.File<br />类学习了一下。也找了一些资料。希望大家遇到类似的问题，可以更有效的解决。 
<p>=================================================================================<br />1.基本概念的理解</p><p>　　绝对路径：绝对路径就是你的主页上的文件或目录在硬盘上真正的路径，(URL和物理路径)例如：<br />C:\xyz\test.txt 代表了test.txt文件的绝对路径。<a href="http://www.sun.com/index.htm">http://www.sun.com/index.htm</a>也代表了一个<br />URL绝对路径。</p><p>　　相对路径：相对与某个基准目录的路径。包含Web的相对路径（HTML中的相对目录），例如：在<br />Servlet中，"/"代表Web应用的跟目录。和物理路径的相对表示。例如："./" 代表当前目录,<br />"../"代表上级目录。这种类似的表示，也是属于相对路径。</p><p>另外关于URI，URL,URN等内容，请参考RFC相关文档标准。</p><p>RFC 2396: Uniform Resource Identifiers (URI): Generic Syntax, <br />(<a href="http://www.ietf.org/rfc/rfc2396.txt">http://www.ietf.org/rfc/rfc2396.txt</a>)</p><p><br />2.关于JSP/Servlet中的相对路径和绝对路径。</p><p>2.1服务器端的地址 </p><p>　　 服务器端的相对地址指的是相对于你的web应用的地址，这个地址是在服务器端解析的<br />（不同于html和javascript中的相对地址，他们是由客户端浏览器解析的）也就是说这时候<br />在jsp和servlet中的相对地址应该是相对于你的web应用，即相对于<a href="http://192.168.0.1/webapp/">http://192.168.0.1/webapp/</a>的。 </p><p>　　其用到的地方有： <br /> forward：servlet中的request.getRequestDispatcher(address);这个address是<br />在服务器端解析的，所以，你要forward到a.jsp应该这么写：<br />request.getRequestDispatcher(“/user/a.jsp”)这个/相对于当前的web应用webapp，<br />其绝对地址就是：<a href="http://192.168.0.1/webapp/user/a.jsp">http://192.168.0.1/webapp/user/a.jsp</a>。 <br />sendRedirect：在jsp中&lt;%response.sendRedirect("/rtccp/user/a.jsp");%&gt; </p><p>2.22、客户端的地址 <br /> <br />       所有的html页面中的相对地址都是相对于服务器根目录(<a href="http://192.168.0.1/">http://192.168.0.1/</a>)的，<br />而不是(跟目录下的该Web应用的目录)http://192.168.0.1/webapp/的。 <br /> Html中的form表单的action属性的地址应该是相对于服务器根目录(<a href="http://192.168.0.1/">http://192.168.0.1/</a>)的，<br />所以，如果提交到a.jsp为：action＝"/webapp/user/a.jsp"或action="&lt;%=request.getContextPath()%&gt;"/user/a.jsp；<br />　　提交到servlet为actiom＝"/webapp/handleservlet"  <br /> 　　Javascript也是在客户端解析的，所以其相对路径和form表单一样。 <br /> </p><p>　　因此，一般情况下，在JSP/HTML页面等引用的CSS,Javascript.Action等属性前面最好都加上<br />&lt;%=request.getContextPath()%&gt;,以确保所引用的文件都属于Web应用中的目录。<br />另外，应该尽量避免使用类似".","./","../../"等类似的相对该文件位置的相对路径，这样<br />当文件移动时，很容易出问题。</p><p><br />3. JSP/Servlet中获得当前应用的相对路径和绝对路径<br />3.1 JSP中获得当前应用的相对路径和绝对路径<br /> 根目录所对应的绝对路径:request.getRequestURI()<br /> 文件的绝对路径    　:application.getRealPath(request.getRequestURI());<br /> 当前web应用的绝对路径 :application.getRealPath("/");<br /> 取得请求文件的上层目录:new File(application.getRealPath(request.getRequestURI())).getParent()</p><p>3.2 Servlet中获得当前应用的相对路径和绝对路径<br /> 根目录所对应的绝对路径:request.getServletPath();<br /> 文件的绝对路径    :request.getSession().getServletContext().getRealPath<br />(request.getRequestURI())   <br /> 当前web应用的绝对路径 :servletConfig.getServletContext().getRealPath("/");<br />     (ServletContext对象获得几种方式：<br />       javax.servlet.http.HttpSession.getServletContext() <br />       javax.servlet.jsp.PageContext.getServletContext() <br />       javax.servlet.ServletConfig.getServletContext() <br />     )</p><p>4.java 的Class中获得相对路径，绝对路径的方法<br />4.1单独的Java类中获得绝对路径<br />　　根据java.io.File的Doc文挡，可知:<br /> 默认情况下new File("/")代表的目录为：System.getProperty("user.dir")。<br /> 一下程序获得执行类的当前路径<br />package org.cheng.file;<br />import java.io.File;</p><p>public class FileTest {<br />    public static void main(String[] args) throws Exception {      </p><p>  System.out.println(Thread.currentThread().getContextClassLoader().getResource(""));     </p><p>  System.out.println(FileTest.class.getClassLoader().getResource(""));        </p><p>　　System.out.println(ClassLoader.getSystemResource(""));        <br />  System.out.println(FileTest.class.getResource(""));        <br />  System.out.println(FileTest.class.getResource("/")); //Class文件所在路径  <br />  System.out.println(new File("/").getAbsolutePath());        <br />  System.out.println(System.getProperty("user.dir"));    <br /> }<br />}</p><p>4.2服务器中的Java类获得当前路径（来自网络）<br />(1).Weblogic</p><p>WebApplication的系统文件根目录是你的weblogic安装所在根目录。<br />例如：如果你的weblogic安装在c:\bea\weblogic700.....<br />那么，你的文件根路径就是c:\.<br />所以，有两种方式能够让你访问你的服务器端的文件：<br />a.使用绝对路径：<br />比如将你的参数文件放在c:\yourconfig\yourconf.properties，<br />直接使用 new FileInputStream("yourconfig/yourconf.properties");<br />b.使用相对路径：<br />相对路径的根目录就是你的webapplication的根路径，即WEB-INF的上一级目录，将你的参数文件放</p><p>在yourwebapp\yourconfig\yourconf.properties，<br />这样使用：<br />new FileInputStream("./yourconfig/yourconf.properties");<br />这两种方式均可，自己选择。</p><p>(2).Tomcat</p><p>在类中输出System.getProperty("user.dir");显示的是%Tomcat_Home%/bin</p><p>(3).Resin</p><p>不是你的JSP放的相对路径,是JSP引擎执行这个JSP编译成SERVLET<br />的路径为根.比如用新建文件法测试File f = new File("a.htm");<br />这个a.htm在resin的安装目录下 </p><p>(4).如何读相对路径哪？</p><p>在Java文件中getResource或getResourceAsStream均可</p><p>例：getClass().getResourceAsStream(filePath);//filePath可以是"/filename",这里的/代表web</p><p>发布根路径下WEB-INF/classes</p><p>默认使用该方法的路径是：WEB-INF/classes。已经在Tomcat中测试。</p><p>5.读取文件时的相对路径，避免硬编码和绝对路径的使用。（来自网络）<br />5.1 采用Spring的DI机制获得文件，避免硬编码。<br />   参考下面的连接内容：<br />   <a href="http://www.javajia.net/viewtopic.php?p=90213">http://www.javajia.net/viewtopic.php?p=90213</a>&amp;<br />5.2 配置文件的读取<br /> 参考下面的连接内容：<br /> <a href="http://dev.csdn.net/develop/article/39/39681.shtm">http://dev.csdn.net/develop/article/39/39681.shtm</a><br />5.3 通过虚拟路径或相对路径读取一个xml文件，避免硬编码<br /> 参考下面的连接内容：<br /> <a href="http://club.gamvan.com/club/clubPage.jsp?iPage=1&amp;tID=10708&amp;ccID=8">http://club.gamvan.com/club/clubPage.jsp?iPage=1&amp;tID=10708&amp;ccID=8</a><br /> <br />6.Java中文件的常用操作（复制，移动，删除，创建等）（来自网络）<br /> 常用 java File 操作类 <br /> <a href="http://www.easydone.cn/014/200604022353065155.htm">http://www.easydone.cn/014/200604022353065155.htm</a><br /> <br /> Java文件操作大全（JSP中）<br /> <a href="http://www.pconline.com.cn/pcedu/empolder/gj/java/0502/559401.html">http://www.pconline.com.cn/pcedu/empolder/gj/java/0502/559401.html</a></p><p> java文件操作详解（Java中文网）<br /> <a href="http://www.51cto.com/html/2005/1108/10947.htm">http://www.51cto.com/html/2005/1108/10947.htm</a></p><p> JAVA 如何创建\删除\修改\复制目录及文件<br /> <a href="http://www.gamvan.com/developer/java/2005/2/264.html">http://www.gamvan.com/developer/java/2005/2/264.html</a></p><p>总结：<br /> 通过上面内容的使用，可以解决在Web应用服务器端，移动文件，查找文件，复制<br /> 删除文件等操作，同时对服务器的相对地址，绝对地址概念更加清晰。<br />建议参考URI,的RFC标准文挡。同时对Java.io.File. Java.net.URI.等内容了解透彻<br />对其他方面的理解可以更加深入和透彻。<br />==================================================================================</p><p>参考资料:<br />java/docs/</p><p>java.io.File<br />java.io.InputStream<br />java.io.OutputStream<br />java.io.FileInputStream<br />java.io.FileReader;<br />java.io.FileOutputStream<br />java.io.FileWriter;<br />java.net.URI<br />java.net.URL</p><p><br />绝对路径与相对路径祥解<br /><a href="http://www.webjx.com/htmldata/2005-02-26/1109430310.html">http://www.webjx.com/htmldata/2005-02-26/1109430310.html</a></p><p>[『J道习练』]JSP和Servlet中的绝对路径和相对路径<br /><a href="http://w3china.org/blog/more.asp?name=pcthomas&amp;id=9122&amp;commentid=12376">http://w3china.org/blog/more.asp?name=pcthomas&amp;id=9122&amp;commentid=12376</a></p><p>JSP,Servlet,Class获得当前应用的相对路径和绝对路径<br /><a href="http://cy.lzu.edu.cn/cy/club/clubPage.jsp?ccStyle=0&amp;tID=886&amp;ccID=77">http://cy.lzu.edu.cn/cy/club/clubPage.jsp?ccStyle=0&amp;tID=886&amp;ccID=77</a></p><p>如何获得当前文件路径<br /><a href="http://www.matrix.org.cn/resource/article/44/44113_java.html">http://www.matrix.org.cn/resource/article/44/44113_java.html</a></p><p>通过Spring注入机制，取得文件<br /><a href="http://www.javajia.net/viewtopic.php?p=90213">http://www.javajia.net/viewtopic.php?p=90213</a>&amp;</p><p>配置文件的读取 <br /><a href="http://dev.csdn.net/develop/article/39/39681.shtm">http://dev.csdn.net/develop/article/39/39681.shtm</a></p><p>读取配置文件,通过虚拟路径或相对路径读取一个xml文件，避免硬编码！ <br /><a href="http://club.gamvan.com/club/clubPage.jsp?iPage=1&amp;tID=10708&amp;ccID=8">http://club.gamvan.com/club/clubPage.jsp?iPage=1&amp;tID=10708&amp;ccID=8</a></p><p>常用 java File 操作类<br /><a href="http://www.easydone.cn/014/200604022353065155.htm">http://www.easydone.cn/014/200604022353065155.htm</a></p><p>Java文件操作大全<br /><a href="http://www.pconline.com.cn/pcedu/empolder/gj/java/0502/559401.html">http://www.pconline.com.cn/pcedu/empolder/gj/java/0502/559401.html</a></p><p>Java文件操作详解<br /><a href="http://www.51cto.com/html/2005/1108/10947.htm">http://www.51cto.com/html/2005/1108/10947.htm</a></p><br /><img src ="http://www.blogjava.net/meil/aggbug/73908.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/meil/" target="_blank">向东博客</a> 2006-10-10 08:58 <a href="http://www.blogjava.net/meil/archive/2006/10/10/73908.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>java文件路径</title><link>http://www.blogjava.net/meil/archive/2006/10/10/74165.html</link><dc:creator>向东博客</dc:creator><author>向东博客</author><pubDate>Tue, 10 Oct 2006 00:58:00 GMT</pubDate><guid>http://www.blogjava.net/meil/archive/2006/10/10/74165.html</guid><wfw:comment>http://www.blogjava.net/meil/comments/74165.html</wfw:comment><comments>http://www.blogjava.net/meil/archive/2006/10/10/74165.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/meil/comments/commentRss/74165.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/meil/services/trackbacks/74165.html</trackback:ping><description><![CDATA[首先说可以用下面的方法查看当前的目录，你的文件路径就可以以此为据：<br />System.out.println(System.getProperty("user.dir"));<br /><br />如果你用这个方法来杳看你JSP页面，可以发现它的路径很奇怪，其实它是JSP引擎路径。所以当你用<br />    new File(String path);<br />时，如果用的是相对路径，就得相对真实的当前路径，而不是任何你想当然的路径 。<br />当然对于这个方法用”绝对路径“一般是不会出错的。只是这样，程序的灵活性就受到了限制。下面就是一个绝对路径 的例子：<br />    String xmlPath = "D:\\PublicFiles\\WCI\\navigation.xml";<br /><br />Part 2<br />FileStream file = this.getClass().getClassLoader().getResourceAsStream(String xmlPath);<br />这个有点复杂，我了解的也不多，这里就说说现在我所了解的吧，以后再补充！<br />System.out.println(this.getClass().getClassLoader().getResource("/").getPath());<br />如此就可以看到相对“/”的根路径。<br />对   FileStream fileStream = this.getClass().getClassLoader().getResourceAsStream(filePath);<br />这里的filePath似乎只能用相对路径，至少我不知道用绝对路径怎么表示：<br />下面取个相对路径的例子：<br />    String filePath = "/../../Resources/XML/navigation.xml"; //表达规则和LINUX一样。<br /><br />System.out.println(this.getClass().getClassLoader().getResource(".").getPath());<br />System.out.println(this.getClass().getClassLoader().getResource("/").getPath());<br />System.out.println(this.getClass().getClassLoader().getResource("").getPath());<br />System.out.println(this.getClass().getClassLoader().getResource("..").getPath());<br />相信，看过这四个路径结果就应该知道在哪放置自己的文件了，用什么样的语句能找到。<br /><br />今天在写一个写JSP时，碰到上面的文件路径的问题，简单的了解下，相信其中定有不少的错误，希望你能提出并改正，我在此谢过了！<br /><br /><img src ="http://www.blogjava.net/meil/aggbug/74165.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/meil/" target="_blank">向东博客</a> 2006-10-10 08:58 <a href="http://www.blogjava.net/meil/archive/2006/10/10/74165.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>JSP中的路径问题</title><link>http://www.blogjava.net/meil/archive/2006/10/10/74166.html</link><dc:creator>向东博客</dc:creator><author>向东博客</author><pubDate>Tue, 10 Oct 2006 00:58:00 GMT</pubDate><guid>http://www.blogjava.net/meil/archive/2006/10/10/74166.html</guid><wfw:comment>http://www.blogjava.net/meil/comments/74166.html</wfw:comment><comments>http://www.blogjava.net/meil/archive/2006/10/10/74166.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/meil/comments/commentRss/74166.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/meil/services/trackbacks/74166.html</trackback:ping><description><![CDATA[
		<p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt 21pt; TEXT-INDENT: -21pt">
				<font size="3">
						<span lang="EN-US">
								<span>
										<font face="Courier New">一．</font>
								</span>
						</span>
						<span style="FONT-FAMILY: 宋体">问题</span>
				</font>
		</p>
		<p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt">
				<font size="3">
						<span lang="EN-US">
								<font face="Courier New">
										<span>    </span>JSP</font>
						</span>
						<span style="FONT-FAMILY: 宋体">中究竟采用绝对路径还是采用相对路径随着所采用技术的越来越复杂，这个问题也变得越来越难以解决。</span>
				</font>
		</p>
		<p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt 21pt; TEXT-INDENT: -21pt">
				<font size="3">
						<span lang="EN-US">
								<span>
										<font face="Courier New">１）</font>
								</span>
						</span>
						<span style="FONT-FAMILY: 宋体">采用相对路径遇到的问题</span>
				</font>
		</p>
		<p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt 42pt; TEXT-INDENT: -21pt">
				<span lang="EN-US" style="FONT-FAMILY: Wingdings">
						<span>
								<font size="3">l</font>
								<span style="FONT: 7pt 'Times New Roman'; font-size-adjust: none; font-stretch: normal">         </span>
						</span>
				</span>
				<span style="FONT-FAMILY: 宋体">
						<font size="3">相对路径固然比较灵活，但如果想复制页面内的代码却变得比较困难，因为不同的页面具有不同的相对路径，复制后必须修改每一个连接的路径。</font>
				</span>
		</p>
		<p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt 42pt; TEXT-INDENT: -21pt">
				<span lang="EN-US" style="FONT-FAMILY: Wingdings">
						<span>
								<font size="3">l</font>
								<span style="FONT: 7pt 'Times New Roman'; font-size-adjust: none; font-stretch: normal">         </span>
						</span>
				</span>
				<span style="FONT-FAMILY: 宋体">
						<font size="3">如果页面被多于一个的页面所包含，那么被包含页面中的相对路径将是不正确的。</font>
				</span>
		</p>
		<p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt 42pt; TEXT-INDENT: -21pt">
				<span lang="EN-US" style="FONT-FAMILY: Wingdings">
						<span>
								<font size="3">l</font>
								<span style="FONT: 7pt 'Times New Roman'; font-size-adjust: none; font-stretch: normal">         </span>
						</span>
				</span>
				<font size="3">
						<span style="FONT-FAMILY: 宋体">如果采用</span>
						<span lang="EN-US">
								<font face="Courier New">Struts</font>
						</span>
						<span style="FONT-FAMILY: 宋体">的</span>
						<span lang="EN-US">
								<font face="Courier New">Action</font>
						</span>
						<span style="FONT-FAMILY: 宋体">返回页面，那么由于页面路径与</span>
						<span lang="EN-US">
								<font face="Courier New">Action</font>
						</span>
						<span style="FONT-FAMILY: 宋体">路径不同，使得浏览器无法正确解释页面中的路径，如页面为</span>
						<span lang="EN-US">
								<font face="Courier New">/pages/cust/cust.jsp</font>
						</span>
						<span style="FONT-FAMILY: 宋体">，图片所有目录为</span>
						<span lang="EN-US">
								<font face="Courier New">/images/title.gif</font>
						</span>
						<span style="FONT-FAMILY: 宋体">，这时在</span>
						<span lang="EN-US">
								<font face="Courier New">/pages/cust/cust.jsp</font>
						</span>
						<span style="FONT-FAMILY: 宋体">中的所用的路径为</span>
						<span lang="EN-US">
								<font face="Courier New">”../../images/title.gif”</font>
						</span>
						<span style="FONT-FAMILY: 宋体">，但是如果某一个</span>
						<span lang="EN-US">
								<font face="Courier New">Action</font>
						</span>
						<span style="FONT-FAMILY: 宋体">的</span>
						<span lang="EN-US">
								<font face="Courier New">Forward</font>
						</span>
						<span style="FONT-FAMILY: 宋体">指向这个</span>
						<span lang="EN-US">
								<font face="Courier New">JSP</font>
						</span>
						<span style="FONT-FAMILY: 宋体">文件，而这个</span>
						<span lang="EN-US">
								<font face="Courier New">Action</font>
						</span>
						<span style="FONT-FAMILY: 宋体">的路径为</span>
						<span lang="EN-US">
								<font face="Courier New">/cust/manage.do</font>
						</span>
						<span style="FONT-FAMILY: 宋体">，那么页面内容中</span>
						<span lang="EN-US">
								<font face="Courier New">”../../images/title.gif”</font>
						</span>
						<span style="FONT-FAMILY: 宋体">就不再指向正确的路径了。</span>
				</font>
		</p>
		<p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt 21pt">
				<span style="FONT-FAMILY: 宋体">
						<font size="3">解决以上问题似乎只有使用绝对路径了。</font>
				</span>
		</p>
		<p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt 21pt; TEXT-INDENT: -21pt">
				<font size="3">
						<span lang="EN-US">
								<span>
										<font face="Courier New">２）</font>
								</span>
						</span>
						<span style="FONT-FAMILY: 宋体">采用绝对路径遇到的问题</span>
				</font>
		</p>
		<p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt 42pt; TEXT-INDENT: -21pt">
				<span lang="EN-US" style="FONT-FAMILY: Wingdings">
						<span>
								<font size="3">l</font>
								<span style="FONT: 7pt 'Times New Roman'; font-size-adjust: none; font-stretch: normal">         </span>
						</span>
				</span>
				<font size="3">
						<span style="FONT-FAMILY: 宋体">随着不同的</span>
						<span lang="EN-US">
								<font face="Courier New">Web</font>
						</span>
						<span style="FONT-FAMILY: 宋体">应用发布方式，绝对路径的值也不同。如</span>
						<span lang="EN-US">
								<font face="Courier New">Web</font>
						</span>
						<span style="FONT-FAMILY: 宋体">应用发布为</span>
						<span lang="EN-US">
								<font face="Courier New">MyApp</font>
						</span>
						<span style="FONT-FAMILY: 宋体">，则路径</span>
						<span lang="EN-US">
								<font face="Courier New">”/MyApp/images/title.gif”</font>
						</span>
						<span style="FONT-FAMILY: 宋体">是正确的，但发布为另一应用时如</span>
						<span lang="EN-US">
								<font face="Courier New">MyApp2</font>
						</span>
						<span style="FONT-FAMILY: 宋体">，这个路径就不对了，也许这个情况比较少，但以</span>
						<span lang="EN-US">
								<font face="Courier New">default</font>
						</span>
						<span style="FONT-FAMILY: 宋体">方式发布</span>
						<span lang="EN-US">
								<font face="Courier New">Web</font>
						</span>
						<span style="FONT-FAMILY: 宋体">应用时以上绝对路径也不同：</span>
						<span lang="EN-US">
								<font face="Courier New">”/images/title.gif”</font>
						</span>
						<span style="FONT-FAMILY: 宋体">。</span>
				</font>
		</p>
		<p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt">
				<span lang="EN-US">
						<p>
						</p>
				</span>
				<font size="3">
						<span lang="EN-US">
								<span>
										<font face="Courier New">二．</font>
								</span>
						</span>
						<span style="FONT-FAMILY: 宋体">解决方案</span>
				</font>
		</p>
		<p>
		</p>
		<p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt 39pt; TEXT-INDENT: -18pt">
				<span lang="EN-US">
						<span>
								<font face="Courier New" size="3">１）</font>
								<span style="FONT: 7pt 'Times New Roman'; font-size-adjust: none; font-stretch: normal">  </span>
						</span>
				</span>
				<font size="3">
						<span style="FONT-FAMILY: 宋体">采用绝对路径，但为了解决不同部署方式的差别，在所有非</span>
						<span lang="EN-US">
								<font face="Courier New">struts</font>
						</span>
						<span style="FONT-FAMILY: 宋体">标签的路径前加</span>
						<span lang="EN-US">
								<font face="Courier New">${pageContext.request.contextPath}</font>
						</span>
						<span style="FONT-FAMILY: 宋体">，如原路径为：</span>
				</font>
		</p>
		<p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt 21pt; TEXT-INDENT: 18pt">
				<font size="3">
						<span lang="EN-US">
								<font face="Courier New">”/images/title.gif”</font>
						</span>
						<span style="FONT-FAMILY: 宋体">，改为</span>
				</font>
		</p>
		<p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt 21pt; TEXT-INDENT: 18pt">
				<font size="3">
						<span lang="EN-US">
								<font face="Courier New">“${pageContext.request.contextPath}/images/title.gif”</font>
						</span>
						<span style="FONT-FAMILY: 宋体">。</span>
				</font>
		</p>
		<p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt 39pt">
				<font size="3">
						<span style="FONT-FAMILY: 宋体">代码</span>
						<span lang="EN-US">
								<font face="Courier New">” ${pageContext.request.contextPath}”</font>
						</span>
						<span style="FONT-FAMILY: 宋体">的作用是取出部署的应用程序名，这样不管如何部署，所用路径都是正确的。</span>
				</font>
		</p>
		<p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt 39pt">
				<b>
						<font size="3">
								<span style="FONT-FAMILY: 宋体">缺点：</span>
								<span lang="EN-US">
										<p>
										</p>
								</span>
						</font>
				</b>
		</p>
		<p>
		</p>
		<p>
		</p>
		<p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt 42pt">
				<font size="3">
						<span style="FONT-FAMILY: 宋体">操作不便，其他工具无法正确解释</span>
						<span lang="EN-US">
								<font face="Courier New">${pageContext.request.contextPath}</font>
						</span>
				</font>
		</p>
		<p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt 39pt; TEXT-INDENT: -18pt">
				<span lang="EN-US">
						<span>
								<font face="Courier New" size="3">２）</font>
								<span style="FONT: 7pt 'Times New Roman'; font-size-adjust: none; font-stretch: normal">  </span>
						</span>
				</span>
				<font size="3">
						<span style="FONT-FAMILY: 宋体">采用相对路径，在每个</span>
						<span lang="EN-US">
								<font face="Courier New">JSP</font>
						</span>
						<span style="FONT-FAMILY: 宋体">文件中加入</span>
						<span lang="EN-US">
								<font face="Courier New">base</font>
						</span>
						<span style="FONT-FAMILY: 宋体">标签，如：</span>
				</font>
		</p>
		<p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt 39pt">
				<span lang="EN-US">
						<font face="Courier New" size="3">&lt;base href="http://${header['host']}${pageContext.request.contextPath}/pages/cust/relation.jsp" /&gt;</font>
				</span>
		</p>
		<p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt 39pt">
				<span style="FONT-FAMILY: 宋体">
						<font size="3">这样所有的路径都可以使用相对路径。</font>
				</span>
		</p>
		<p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt 39pt">
				<b>
						<font size="3">
								<span style="FONT-FAMILY: 宋体">缺点：</span>
								<span lang="EN-US">
										<p>
										</p>
								</span>
						</font>
				</b>
		</p>
		<p>
		</p>
		<p>
		</p>
		<p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt 39pt">
				<span lang="EN-US">
						<span>
						</span>
				</span>
				<span style="FONT-FAMILY: 宋体">
						<font size="3">对于被包含的文件依然无效。</font>
				</span>
		</p>
		<p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt">
				<font size="3">
						<span lang="EN-US">
								<span>
										<font face="Courier New">    </font>
								</span>
						</span>
						<span style="FONT-FAMILY: 宋体">真正使用时需要灵活应用１）和２），写出更加健壮的代码。</span>
				</font>
		</p>
<img src ="http://www.blogjava.net/meil/aggbug/74166.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/meil/" target="_blank">向东博客</a> 2006-10-10 08:58 <a href="http://www.blogjava.net/meil/archive/2006/10/10/74166.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>JSP、Servlet中的相对路径和绝对路径</title><link>http://www.blogjava.net/meil/archive/2006/10/10/74167.html</link><dc:creator>向东博客</dc:creator><author>向东博客</author><pubDate>Tue, 10 Oct 2006 00:57:00 GMT</pubDate><guid>http://www.blogjava.net/meil/archive/2006/10/10/74167.html</guid><wfw:comment>http://www.blogjava.net/meil/comments/74167.html</wfw:comment><comments>http://www.blogjava.net/meil/archive/2006/10/10/74167.html#Feedback</comments><slash:comments>1</slash:comments><wfw:commentRss>http://www.blogjava.net/meil/comments/commentRss/74167.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/meil/services/trackbacks/74167.html</trackback:ping><description><![CDATA[
		<p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt">
				<span lang="EN-US" style="FONT-SIZE: 12pt">
						<font face="Times New Roman">JSP</font>
				</span>
				<span style="FONT-SIZE: 12pt; FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">和</span>
				<span lang="EN-US" style="FONT-SIZE: 12pt">
						<font face="Times New Roman">Servlet</font>
				</span>
				<span style="FONT-SIZE: 12pt; FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">中的绝对路径和相对路径问题困扰了我好几天，经过努力之后将其部分心得和大家共享。</span>
				<span lang="EN-US" style="FONT-SIZE: 12pt">
						<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" /?>
						<o:p>
						</o:p>
				</span>
		</p>
		<p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt">
				<span lang="EN-US" style="FONT-SIZE: 12pt">
						<span style="mso-tab-count: 1">
								<font face="Times New Roman">       </font>
						</span>
				</span>
				<span style="FONT-SIZE: 12pt; FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">前提：假设你的</span>
				<span lang="EN-US" style="FONT-SIZE: 12pt">
						<font face="Times New Roman">Http</font>
				</span>
				<span style="FONT-SIZE: 12pt; FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">地址为</span>
				<span lang="EN-US" style="FONT-SIZE: 12pt">
						<a href="http://192.168.0.1/">
								<font face="Times New Roman" color="#000080">http://192.168.0.1/</font>
						</a>
				</span>
				<span style="FONT-SIZE: 12pt; FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">你的</span>
				<span lang="EN-US" style="FONT-SIZE: 12pt">
						<font face="Times New Roman">web</font>
				</span>
				<span style="FONT-SIZE: 12pt; FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">应用为</span>
				<span lang="EN-US" style="FONT-SIZE: 12pt">
						<font face="Times New Roman">webapp</font>
				</span>
				<span style="FONT-SIZE: 12pt; FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">，那么你的</span>
				<span lang="EN-US" style="FONT-SIZE: 12pt">
						<font face="Times New Roman">web</font>
				</span>
				<span style="FONT-SIZE: 12pt; FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">应用</span>
				<span lang="EN-US" style="FONT-SIZE: 12pt">
						<font face="Times New Roman">URL</font>
				</span>
				<span style="FONT-SIZE: 12pt; FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">为</span>
				<span lang="EN-US" style="FONT-SIZE: 12pt">
						<a href="http://192.168.0.1/webapp/">
								<font face="Times New Roman" color="#000080">http://192.168.0.1/webapp/</font>
						</a>
						<o:p>
						</o:p>
				</span>
		</p>
		<p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt">
				<span lang="EN-US" style="FONT-SIZE: 12pt">
						<font face="Times New Roman">
								<span style="mso-tab-count: 1">       </span>web</font>
				</span>
				<span style="FONT-SIZE: 12pt; FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">应用的目录结构：</span>
				<span lang="EN-US" style="FONT-SIZE: 12pt">
						<o:p>
						</o:p>
				</span>
		</p>
		<p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt">
				<span lang="EN-US" style="FONT-SIZE: 12pt">
						<font face="Times New Roman">
								<span style="mso-tab-count: 1">       </span>webapp/<o:p></o:p></font>
				</span>
		</p>
		<p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt">
				<span lang="EN-US" style="FONT-SIZE: 12pt">
						<font face="Times New Roman">
								<span style="mso-tab-count: 2">              </span>web-inf/<o:p></o:p></font>
				</span>
		</p>
		<p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt">
				<span lang="EN-US" style="FONT-SIZE: 12pt">
						<font face="Times New Roman">
								<span style="mso-tab-count: 2">              </span>
								<span style="mso-spacerun: yes">   </span>classes/<o:p></o:p></font>
				</span>
		</p>
		<p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt">
				<span lang="EN-US" style="FONT-SIZE: 12pt">
						<font face="Times New Roman">
								<span style="mso-tab-count: 3">                     </span>lib/<o:p></o:p></font>
				</span>
		</p>
		<p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt">
				<span lang="EN-US" style="FONT-SIZE: 12pt">
						<font face="Times New Roman">
								<span style="mso-tab-count: 3">                     </span>web.xml<o:p></o:p></font>
				</span>
		</p>
		<p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt">
				<span lang="EN-US" style="FONT-SIZE: 12pt">
						<font face="Times New Roman">
								<span style="mso-tab-count: 4">                            </span>
								<span style="mso-spacerun: yes">  </span>&lt;servlet-mapping&gt;<o:p></o:p></font>
				</span>
		</p>
		<p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt">
				<span lang="EN-US" style="FONT-SIZE: 12pt">
						<font face="Times New Roman">
								<span style="mso-spacerun: yes">    </span>
								<span style="mso-tab-count: 4">                           </span>&lt;servlet-name&gt;handleservlet&lt;/servlet-name&gt;<o:p></o:p></font>
				</span>
		</p>
		<p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt 60pt; TEXT-INDENT: -60pt; mso-char-indent-count: -5.0">
				<span lang="EN-US" style="FONT-SIZE: 12pt">
						<font face="Times New Roman">
								<span style="mso-spacerun: yes">    </span>
								<span style="mso-tab-count: 4">                           </span>&lt;url-pattern&gt;/handleservlet&lt;/url-pattern&gt;</font>
				</span>
				<span style="FONT-SIZE: 12pt; FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">此映射是相对于当前</span>
				<span lang="EN-US" style="FONT-SIZE: 12pt">
						<font face="Times New Roman">web</font>
				</span>
				<span style="FONT-SIZE: 12pt; FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">应用的</span>
				<span lang="EN-US" style="FONT-SIZE: 12pt">
						<o:p>
						</o:p>
				</span>
		</p>
		<p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt">
				<span lang="EN-US" style="FONT-SIZE: 12pt">
						<font face="Times New Roman">
								<span style="mso-spacerun: yes">  </span>
								<span style="mso-tab-count: 4">                        </span>
								<span style="mso-spacerun: yes">   </span>&lt;/servlet-mapping&gt;<o:p></o:p></font>
				</span>
		</p>
		<p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt">
				<span lang="EN-US" style="FONT-SIZE: 12pt">
						<font face="Times New Roman">
								<span style="mso-tab-count: 2">              </span>user/<o:p></o:p></font>
				</span>
		</p>
		<p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt 63pt">
				<span lang="EN-US" style="FONT-SIZE: 12pt">
						<font face="Times New Roman">a.jsp<o:p></o:p></font>
				</span>
		</p>
		<p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt 63pt">
				<span lang="EN-US" style="FONT-SIZE: 12pt">
						<font face="Times New Roman">b.jsp<o:p></o:p></font>
				</span>
		</p>
		<p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt">
				<span lang="EN-US" style="FONT-SIZE: 12pt">
						<font face="Times New Roman">
								<span style="mso-tab-count: 2">              </span>images/<o:p></o:p></font>
				</span>
		</p>
		<p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt">
				<span lang="EN-US" style="FONT-SIZE: 12pt">
						<font face="Times New Roman">
								<span style="mso-tab-count: 2">              </span>css/<o:p></o:p></font>
				</span>
		</p>
		<p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt">
				<span lang="EN-US" style="FONT-SIZE: 12pt">
						<font face="Times New Roman">
								<span style="mso-tab-count: 2">              </span>js/<o:p></o:p></font>
				</span>
		</p>
		<p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt">
				<span lang="EN-US" style="FONT-SIZE: 12pt">
						<font face="Times New Roman">
								<span style="mso-tab-count: 2">              </span>
								<o:p>
								</o:p>
						</font>
				</span>
		</p>
		<p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt">
				<span lang="EN-US" style="FONT-SIZE: 12pt">
						<span style="mso-tab-count: 1">
								<font face="Times New Roman">       </font>
						</span>
				</span>
				<span style="FONT-SIZE: 12pt; FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">所有相对路径都是由“</span>
				<span lang="EN-US" style="FONT-SIZE: 12pt">
						<font face="Times New Roman">/</font>
				</span>
				<span style="FONT-SIZE: 12pt; FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">”开头的。如：</span>
				<span lang="EN-US" style="FONT-SIZE: 12pt">
						<font face="Times New Roman">/image/a.gif</font>
				</span>
				<span style="FONT-SIZE: 12pt; FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">，</span>
				<span lang="EN-US" style="FONT-SIZE: 12pt">
						<font face="Times New Roman">/user/main.jsp</font>
				</span>
				<span style="FONT-SIZE: 12pt; FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">，大家知道在</span>
				<span lang="EN-US" style="FONT-SIZE: 12pt">
						<font face="Times New Roman">html</font>
				</span>
				<span style="FONT-SIZE: 12pt; FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">中的相对路径是这样的：</span>
				<span lang="EN-US" style="FONT-SIZE: 12pt">
						<o:p>
						</o:p>
				</span>
		</p>
		<p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt">
				<span lang="EN-US" style="FONT-SIZE: 12pt">
						<span style="mso-tab-count: 1">
								<font face="Times New Roman">       </font>
						</span>
				</span>
				<span style="FONT-SIZE: 12pt; FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">有个</span>
				<span lang="EN-US" style="FONT-SIZE: 12pt">
						<font face="Times New Roman">html</font>
				</span>
				<span style="FONT-SIZE: 12pt; FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">文件：</span>
				<span lang="EN-US" style="FONT-SIZE: 12pt">
						<font face="Times New Roman">a.html</font>
				</span>
				<span style="FONT-SIZE: 12pt; FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">，其中有</span>
				<span lang="EN-US" style="FONT-SIZE: 12pt">
						<font face="Times New Roman">&lt;link href="one.css" rel="stylesheet" type="text/css"&gt;</font>
				</span>
				<span style="FONT-SIZE: 12pt; FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">，其中</span>
				<span lang="EN-US" style="FONT-SIZE: 12pt">
						<font face="Times New Roman">href</font>
				</span>
				<span style="FONT-SIZE: 12pt; FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">属性表示引用的</span>
				<span lang="EN-US" style="FONT-SIZE: 12pt">
						<font face="Times New Roman">css</font>
				</span>
				<span style="FONT-SIZE: 12pt; FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">文件的路径。</span>
				<span lang="EN-US" style="FONT-SIZE: 12pt">
						<o:p>
						</o:p>
				</span>
		</p>
		<p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 21pt">
				<span lang="EN-US" style="FONT-SIZE: 12pt">
						<font face="Times New Roman">one.css</font>
				</span>
				<span style="FONT-SIZE: 12pt; FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">：表示</span>
				<span lang="EN-US" style="FONT-SIZE: 12pt">
						<font face="Times New Roman">one.css</font>
				</span>
				<span style="FONT-SIZE: 12pt; FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">和</span>
				<span lang="EN-US" style="FONT-SIZE: 12pt">
						<font face="Times New Roman">a.hmtl</font>
				</span>
				<span style="FONT-SIZE: 12pt; FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">处于同一个目录</span>
				<span lang="EN-US" style="FONT-SIZE: 12pt">
						<o:p>
						</o:p>
				</span>
		</p>
		<p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 21pt">
				<span lang="EN-US" style="FONT-SIZE: 12pt">
						<font face="Times New Roman">user/one.css</font>
				</span>
				<span style="FONT-SIZE: 12pt; FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">：表示</span>
				<span lang="EN-US" style="FONT-SIZE: 12pt">
						<font face="Times New Roman">one.css</font>
				</span>
				<span style="FONT-SIZE: 12pt; FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">处于</span>
				<span lang="EN-US" style="FONT-SIZE: 12pt">
						<font face="Times New Roman">a.html</font>
				</span>
				<span style="FONT-SIZE: 12pt; FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">所在目录的子目录</span>
				<span lang="EN-US" style="FONT-SIZE: 12pt">
						<font face="Times New Roman">user</font>
				</span>
				<span style="FONT-SIZE: 12pt; FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">中。</span>
				<span lang="EN-US" style="FONT-SIZE: 12pt">
						<o:p>
						</o:p>
				</span>
		</p>
		<p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 21pt">
				<span lang="EN-US" style="FONT-SIZE: 12pt">
						<font face="Times New Roman">../one.css</font>
				</span>
				<span style="FONT-SIZE: 12pt; FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">：表示</span>
				<span lang="EN-US" style="FONT-SIZE: 12pt">
						<font face="Times New Roman">one.css</font>
				</span>
				<span style="FONT-SIZE: 12pt; FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">位于</span>
				<span lang="EN-US" style="FONT-SIZE: 12pt">
						<font face="Times New Roman">a.hmtl</font>
				</span>
				<span style="FONT-SIZE: 12pt; FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">上一级目录下，</span>
				<span lang="EN-US" style="FONT-SIZE: 12pt">
						<o:p>
						</o:p>
				</span>
		</p>
		<p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 21pt">
				<span lang="EN-US" style="FONT-SIZE: 12pt">
						<font face="Times New Roman">../../one.css</font>
				</span>
				<span style="FONT-SIZE: 12pt; FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">：表示</span>
				<span lang="EN-US" style="FONT-SIZE: 12pt">
						<font face="Times New Roman">one.css</font>
				</span>
				<span style="FONT-SIZE: 12pt; FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">位于</span>
				<span lang="EN-US" style="FONT-SIZE: 12pt">
						<font face="Times New Roman">a.hmtl</font>
				</span>
				<span style="FONT-SIZE: 12pt; FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">上一级目录的上一级目录下，</span>
				<span lang="EN-US" style="FONT-SIZE: 12pt">
						<o:p>
						</o:p>
				</span>
		</p>
		<p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 21pt">
				<span lang="EN-US" style="FONT-SIZE: 12pt">
						<font face="Times New Roman">./</font>
				</span>
				<span style="FONT-SIZE: 12pt; FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">：表示和</span>
				<span lang="EN-US" style="FONT-SIZE: 12pt">
						<font face="Times New Roman">a.hmtl</font>
				</span>
				<span style="FONT-SIZE: 12pt; FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">同一目录</span>
				<span lang="EN-US" style="FONT-SIZE: 12pt">
						<o:p>
						</o:p>
				</span>
		</p>
		<p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt; TEXT-INDENT: 21pt">
				<span style="FONT-SIZE: 12pt; FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">我们称上述相对路径为</span>
				<span lang="EN-US" style="FONT-SIZE: 12pt">
						<font face="Times New Roman">html</font>
				</span>
				<span style="FONT-SIZE: 12pt; FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">相对路径</span>
				<span lang="EN-US" style="FONT-SIZE: 12pt">
						<o:p>
						</o:p>
				</span>
		</p>
		<p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt 18pt; TEXT-INDENT: -18pt; mso-list: l0 level1 lfo1; tab-stops: list 18.0pt">
				<span lang="EN-US" style="FONT-SIZE: 12pt; mso-fareast-font-family: 'Times New Roman'">
						<span style="mso-list: Ignore">
								<font face="Times New Roman">1、</font>
						</span>
				</span>
				<span style="FONT-SIZE: 12pt; FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">服务器端的地址</span>
				<span lang="EN-US" style="FONT-SIZE: 12pt">
						<o:p>
						</o:p>
				</span>
		</p>
		<p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt 18pt; TEXT-INDENT: 21pt">
				<span style="FONT-SIZE: 12pt; FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">服务器端的相对地址指的是相对于你的</span>
				<span lang="EN-US" style="FONT-SIZE: 12pt">
						<font face="Times New Roman">web</font>
				</span>
				<span style="FONT-SIZE: 12pt; FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">应用的地址，这个地址是在服务器端解析的（不同于</span>
				<span lang="EN-US" style="FONT-SIZE: 12pt">
						<font face="Times New Roman">html</font>
				</span>
				<span style="FONT-SIZE: 12pt; FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">和</span>
				<span lang="EN-US" style="FONT-SIZE: 12pt">
						<font face="Times New Roman">javascript</font>
				</span>
				<span style="FONT-SIZE: 12pt; FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">中的相对地址，他们是由客户端浏览器解析的）也就是说这时候在</span>
				<span lang="EN-US" style="FONT-SIZE: 12pt">
						<font face="Times New Roman">jsp</font>
				</span>
				<span style="FONT-SIZE: 12pt; FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">和</span>
				<span lang="EN-US" style="FONT-SIZE: 12pt">
						<font face="Times New Roman">servlet</font>
				</span>
				<span style="FONT-SIZE: 12pt; FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">中的相对地址应该是相对于你的</span>
				<span lang="EN-US" style="FONT-SIZE: 12pt">
						<font face="Times New Roman">web</font>
				</span>
				<span style="FONT-SIZE: 12pt; FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">应用，即相对于</span>
				<span lang="EN-US" style="FONT-SIZE: 12pt">
						<a href="http://192.168.0.1/webapp/">
								<font face="Times New Roman" color="#000080">http://192.168.0.1/webapp/</font>
						</a>
				</span>
				<span style="FONT-SIZE: 12pt; FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">的。</span>
				<span lang="EN-US" style="FONT-SIZE: 12pt">
						<o:p>
						</o:p>
				</span>
		</p>
		<p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt 18pt; TEXT-INDENT: 21pt">
				<span style="FONT-SIZE: 12pt; FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">其用到的地方有：</span>
				<span lang="EN-US" style="FONT-SIZE: 12pt">
						<o:p>
						</o:p>
				</span>
		</p>
		<p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt 18pt; TEXT-INDENT: 21pt">
				<span lang="EN-US" style="FONT-SIZE: 12pt">
						<font face="Times New Roman">forwarder</font>
				</span>
				<span style="FONT-SIZE: 12pt; FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">：</span>
				<span lang="EN-US" style="FONT-SIZE: 12pt">
						<font face="Times New Roman">servlet</font>
				</span>
				<span style="FONT-SIZE: 12pt; FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">中的</span>
				<span lang="EN-US" style="FONT-SIZE: 12pt">
						<font face="Times New Roman">request.getRequestDispatcher(address);</font>
				</span>
				<span style="FONT-SIZE: 12pt; FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">这个</span>
				<span lang="EN-US" style="FONT-SIZE: 12pt">
						<font face="Times New Roman">address</font>
				</span>
				<span style="FONT-SIZE: 12pt; FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">是在服务器端解析的，所以，你要</span>
				<span lang="EN-US" style="FONT-SIZE: 12pt">
						<font face="Times New Roman">forwarder</font>
				</span>
				<span style="FONT-SIZE: 12pt; FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">到</span>
				<span lang="EN-US" style="FONT-SIZE: 12pt">
						<font face="Times New Roman">a.jsp</font>
				</span>
				<span style="FONT-SIZE: 12pt; FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">应该这么写：</span>
				<span lang="EN-US" style="FONT-SIZE: 12pt">
						<font face="Times New Roman">request.getRequestDispatcher(“/user/a.jsp”)</font>
				</span>
				<span style="FONT-SIZE: 12pt; FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">这个</span>
				<span lang="EN-US" style="FONT-SIZE: 12pt">
						<font face="Times New Roman">/</font>
				</span>
				<span style="FONT-SIZE: 12pt; FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">相对于当前的</span>
				<span lang="EN-US" style="FONT-SIZE: 12pt">
						<font face="Times New Roman">web</font>
				</span>
				<span style="FONT-SIZE: 12pt; FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">应用</span>
				<span lang="EN-US" style="FONT-SIZE: 12pt">
						<font face="Times New Roman">webapp</font>
				</span>
				<span style="FONT-SIZE: 12pt; FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">，其绝对地址就是：</span>
				<span lang="EN-US" style="FONT-SIZE: 12pt">
						<a href="http://192.168.0.1/webapp/user/a.jsp">
								<font face="Times New Roman" color="#000080">http://192.168.0.1/webapp/user/a.jsp</font>
						</a>
				</span>
				<span style="FONT-SIZE: 12pt; FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">。</span>
				<span lang="EN-US" style="FONT-SIZE: 12pt">
						<o:p>
						</o:p>
				</span>
		</p>
		<p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt 18pt; TEXT-INDENT: 21pt">
				<span lang="EN-US" style="FONT-SIZE: 12pt">
						<font face="Times New Roman">sendRedirect</font>
				</span>
				<span style="FONT-SIZE: 12pt; FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">：在</span>
				<span lang="EN-US" style="FONT-SIZE: 12pt">
						<font face="Times New Roman">jsp</font>
				</span>
				<span style="FONT-SIZE: 12pt; FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">中</span>
				<span lang="EN-US" style="FONT-SIZE: 12pt">
						<font face="Times New Roman">&lt;%response.sendRedirect("/rtccp/user/a.jsp");%&gt;<o:p></o:p></font>
				</span>
		</p>
		<p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt 18pt; TEXT-INDENT: -18pt; mso-list: l0 level1 lfo1; tab-stops: list 18.0pt">
				<span lang="EN-US" style="FONT-SIZE: 12pt; mso-fareast-font-family: 'Times New Roman'">
						<span style="mso-list: Ignore">
								<font face="Times New Roman">2、</font>
						</span>
				</span>
				<span style="FONT-SIZE: 12pt; FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">客户端的地址</span>
				<span lang="EN-US" style="FONT-SIZE: 12pt">
						<o:p>
						</o:p>
				</span>
		</p>
		<p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt 18pt; TEXT-INDENT: 21pt">
				<span style="FONT-SIZE: 12pt; FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">所有的</span>
				<span lang="EN-US" style="FONT-SIZE: 12pt">
						<font face="Times New Roman">html</font>
				</span>
				<span style="FONT-SIZE: 12pt; FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">中的相对地址都是相对于</span>
				<span lang="EN-US" style="FONT-SIZE: 12pt">
						<a href="http://192.168.0.1/">
								<font face="Times New Roman" color="#000080">http://192.168.0.1/</font>
						</a>
				</span>
				<span style="FONT-SIZE: 12pt; FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">的，而不是</span>
				<span lang="EN-US" style="FONT-SIZE: 12pt">
						<a href="http://192.168.0.1/webapp/">
								<font face="Times New Roman" color="#000080">http://192.168.0.1/webapp/</font>
						</a>
				</span>
				<span style="FONT-SIZE: 12pt; FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">的。</span>
				<span lang="EN-US" style="FONT-SIZE: 12pt">
						<o:p>
						</o:p>
				</span>
		</p>
		<p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt 18pt; TEXT-INDENT: 21pt">
				<span lang="EN-US" style="FONT-SIZE: 12pt">
						<font face="Times New Roman">Html</font>
				</span>
				<span style="FONT-SIZE: 12pt; FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">中的</span>
				<span lang="EN-US" style="FONT-SIZE: 12pt">
						<font face="Times New Roman">form</font>
				</span>
				<span style="FONT-SIZE: 12pt; FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">表单的</span>
				<span lang="EN-US" style="FONT-SIZE: 12pt">
						<font face="Times New Roman">action</font>
				</span>
				<span style="FONT-SIZE: 12pt; FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">属性的地址应该是相对于</span>
				<span lang="EN-US" style="FONT-SIZE: 12pt">
						<a href="http://192.168.0.1/">
								<font face="Times New Roman" color="#000080">http://192.168.0.1/</font>
						</a>
				</span>
				<span style="FONT-SIZE: 12pt; FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">的，所以，如果提交到</span>
				<span lang="EN-US" style="FONT-SIZE: 12pt">
						<font face="Times New Roman">a.jsp</font>
				</span>
				<span style="FONT-SIZE: 12pt; FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">为：</span>
				<span lang="EN-US" style="FONT-SIZE: 12pt">
						<font face="Times New Roman">action</font>
				</span>
				<span style="FONT-SIZE: 12pt; FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">＝</span>
				<span lang="EN-US" style="FONT-SIZE: 12pt">
						<font face="Times New Roman">”/webapp/user/a.jsp”</font>
				</span>
				<span style="FONT-SIZE: 12pt; FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">；提交到</span>
				<span lang="EN-US" style="FONT-SIZE: 12pt">
						<font face="Times New Roman">servlet</font>
				</span>
				<span style="FONT-SIZE: 12pt; FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">为</span>
				<span lang="EN-US" style="FONT-SIZE: 12pt">
						<font face="Times New Roman">action</font>
				</span>
				<span style="FONT-SIZE: 12pt; FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">＝</span>
				<span lang="EN-US" style="FONT-SIZE: 12pt">
						<font face="Times New Roman">”/webapp/handleservlet”<o:p></o:p></font>
				</span>
		</p>
		<p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt 18pt; TEXT-INDENT: 21pt">
				<span lang="EN-US" style="FONT-SIZE: 12pt">
						<font face="Times New Roman">Javascript</font>
				</span>
				<span style="FONT-SIZE: 12pt; FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">也是在客户端解析的，所以其相对路径和</span>
				<span lang="EN-US" style="FONT-SIZE: 12pt">
						<font face="Times New Roman">form</font>
				</span>
				<span style="FONT-SIZE: 12pt; FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">表单一样。</span>
				<span lang="EN-US" style="FONT-SIZE: 12pt">
						<o:p>
						</o:p>
				</span>
		</p>
		<p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt 18pt; TEXT-INDENT: -18pt; mso-list: l0 level1 lfo1; tab-stops: list 18.0pt">
				<span lang="EN-US" style="FONT-SIZE: 12pt; mso-fareast-font-family: 'Times New Roman'">
						<span style="mso-list: Ignore">
								<font face="Times New Roman">3、</font>
						</span>
				</span>
				<span style="FONT-SIZE: 12pt; FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">站点根目录和</span>
				<span lang="EN-US" style="FONT-SIZE: 12pt">
						<font face="Times New Roman">css</font>
				</span>
				<span style="FONT-SIZE: 12pt; FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">路径问题</span>
				<span lang="EN-US" style="FONT-SIZE: 12pt">
						<o:p>
						</o:p>
				</span>
		</p>
		<p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt 21pt; TEXT-INDENT: 21pt">
				<span style="FONT-SIZE: 12pt; FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">我们称类似这样的相对路径</span>
				<span lang="EN-US" style="FONT-SIZE: 12pt">
						<font face="Times New Roman">/webapp/….</font>
				</span>
				<span style="FONT-SIZE: 12pt; FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">为相对于站点根目录的相对路径。</span>
				<span lang="EN-US" style="FONT-SIZE: 12pt">
						<o:p>
						</o:p>
				</span>
		</p>
		<p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt 21pt; TEXT-INDENT: 21pt">
				<span style="FONT-SIZE: 12pt; FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">当在</span>
				<span lang="EN-US" style="FONT-SIZE: 12pt">
						<font face="Times New Roman">jsp</font>
				</span>
				<span style="FONT-SIZE: 12pt; FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">中引入</span>
				<span lang="EN-US" style="FONT-SIZE: 12pt">
						<font face="Times New Roman">css</font>
				</span>
				<span style="FONT-SIZE: 12pt; FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">时，如果其相对路径相对于当前</span>
				<span lang="EN-US" style="FONT-SIZE: 12pt">
						<font face="Times New Roman">jsp</font>
				</span>
				<span style="FONT-SIZE: 12pt; FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">文件的，而在一个和这个</span>
				<span lang="EN-US" style="FONT-SIZE: 12pt">
						<font face="Times New Roman">jsp</font>
				</span>
				<span style="FONT-SIZE: 12pt; FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">的路径不一样的</span>
				<span lang="EN-US" style="FONT-SIZE: 12pt">
						<font face="Times New Roman">servlet</font>
				</span>
				<span style="FONT-SIZE: 12pt; FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">中</span>
				<span lang="EN-US" style="FONT-SIZE: 12pt">
						<font face="Times New Roman">forwarder</font>
				</span>
				<span style="FONT-SIZE: 12pt; FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">这个</span>
				<span lang="EN-US" style="FONT-SIZE: 12pt">
						<font face="Times New Roman">jsp</font>
				</span>
				<span style="FONT-SIZE: 12pt; FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">时，就会发现这个</span>
				<span lang="EN-US" style="FONT-SIZE: 12pt">
						<font face="Times New Roman">css</font>
				</span>
				<span style="FONT-SIZE: 12pt; FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">样式根本没有起作用。这是因为在</span>
				<span lang="EN-US" style="FONT-SIZE: 12pt">
						<font face="Times New Roman">servlet</font>
				</span>
				<span style="FONT-SIZE: 12pt; FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">中转发时</span>
				<span lang="EN-US" style="FONT-SIZE: 12pt">
						<font face="Times New Roman">css</font>
				</span>
				<span style="FONT-SIZE: 12pt; FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">的路径就是相对于这个</span>
				<span lang="EN-US" style="FONT-SIZE: 12pt">
						<font face="Times New Roman">servlet</font>
				</span>
				<span style="FONT-SIZE: 12pt; FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">的相对路径而非</span>
				<span lang="EN-US" style="FONT-SIZE: 12pt">
						<font face="Times New Roman">jsp</font>
				</span>
				<span style="FONT-SIZE: 12pt; FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">的路径了。所以这时候不能在</span>
				<span lang="EN-US" style="FONT-SIZE: 12pt">
						<font face="Times New Roman">jsp</font>
				</span>
				<span style="FONT-SIZE: 12pt; FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">中用这样的路径：</span>
				<span lang="EN-US" style="FONT-SIZE: 12pt">
						<font face="Times New Roman">&lt;link href="one.css" rel="stylesheet" type="text/css"&gt;</font>
				</span>
				<span style="FONT-SIZE: 12pt; FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">或者</span>
				<span lang="EN-US" style="FONT-SIZE: 12pt">
						<font face="Times New Roman">&lt;link href="../../one.css" rel="stylesheet" type="text/css"&gt;</font>
				</span>
				<span style="FONT-SIZE: 12pt; FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">类似</span>
				<span lang="EN-US" style="FONT-SIZE: 12pt">
						<font face="Times New Roman">href="one.css"</font>
				</span>
				<span style="FONT-SIZE: 12pt; FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">和</span>
				<span lang="EN-US" style="FONT-SIZE: 12pt">
						<font face="Times New Roman">../../one.css</font>
				</span>
				<span style="FONT-SIZE: 12pt; FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">的</span>
				<span lang="EN-US" style="FONT-SIZE: 12pt">
						<font face="Times New Roman">html</font>
				</span>
				<span style="FONT-SIZE: 12pt; FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">相对路径是相对于引用这个</span>
				<span lang="EN-US" style="FONT-SIZE: 12pt">
						<font face="Times New Roman">css</font>
				</span>
				<span style="FONT-SIZE: 12pt; FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">的文件的相对路径。而在</span>
				<span lang="EN-US" style="FONT-SIZE: 12pt">
						<font face="Times New Roman">servlet</font>
				</span>
				<span style="FONT-SIZE: 12pt; FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">中转发时就是相对于这个</span>
				<span lang="EN-US" style="FONT-SIZE: 12pt">
						<font face="Times New Roman">servlet</font>
				</span>
				<span style="FONT-SIZE: 12pt; FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">的相对路径了，因为</span>
				<span lang="EN-US" style="FONT-SIZE: 12pt">
						<font face="Times New Roman">jsp</font>
				</span>
				<span style="FONT-SIZE: 12pt; FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">路径和</span>
				<span lang="EN-US" style="FONT-SIZE: 12pt">
						<font face="Times New Roman">servlet</font>
				</span>
				<span style="FONT-SIZE: 12pt; FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">路径是不一样的，所以这样的引用肯定是出错的。</span>
				<span lang="EN-US" style="FONT-SIZE: 12pt">
						<o:p>
						</o:p>
				</span>
		</p>
		<p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt 21pt; TEXT-INDENT: 21pt">
				<span style="FONT-SIZE: 12pt; FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">所以这个时候，要用站点根目录，就是相对于</span>
				<span lang="EN-US" style="FONT-SIZE: 12pt">
						<a href="http://192.168.0.1/">
								<font face="Times New Roman" color="#000080">http://192.168.0.1/</font>
						</a>
				</span>
				<span style="FONT-SIZE: 12pt; FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">的目录，以“</span>
				<span lang="EN-US" style="FONT-SIZE: 12pt">
						<font face="Times New Roman">/</font>
				</span>
				<span style="FONT-SIZE: 12pt; FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">”开头。</span>
				<span lang="EN-US" style="FONT-SIZE: 12pt">
						<o:p>
						</o:p>
				</span>
		</p>
		<p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt 21pt; TEXT-INDENT: 21pt">
				<span style="FONT-SIZE: 12pt; FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">因此上述错误应更正为</span>
				<span lang="EN-US" style="FONT-SIZE: 12pt">
						<font face="Times New Roman">href=”/webapp/one.css”</font>
				</span>
				<span style="FONT-SIZE: 12pt; FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">类似的站点根目录的相对目录。这样在</span>
				<span lang="EN-US" style="FONT-SIZE: 12pt">
						<font face="Times New Roman">servlet</font>
				</span>
				<span style="FONT-SIZE: 12pt; FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">转发后和</span>
				<span lang="EN-US" style="FONT-SIZE: 12pt">
						<font face="Times New Roman">jsp</font>
				</span>
				<span style="FONT-SIZE: 12pt; FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">中都是相对于站点根目录的相对路径，就能正确使用所定义的</span>
				<span lang="EN-US" style="FONT-SIZE: 12pt">
						<font face="Times New Roman">css</font>
				</span>
				<span style="FONT-SIZE: 12pt; FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">样式了。</span>
				<span lang="EN-US" style="FONT-SIZE: 12pt">
						<o:p>
						</o:p>
				</span>
		</p>
		<p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt 21pt; TEXT-INDENT: 21pt">
				<span lang="EN-US" style="FONT-SIZE: 12pt">
						<o:p>
								<font face="Times New Roman"> </font>
						</o:p>
				</span>
		</p>
		<p class="MsoNormal" style="MARGIN: 0cm 0cm 0pt 21pt; TEXT-INDENT: 21pt">
				<span style="FONT-SIZE: 12pt; FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">说了这么多，不知道你了解没，有什么问题留言，大家一块交流！</span>
				<span lang="EN-US" style="FONT-SIZE: 12pt">
						<o:p>
						</o:p>
				</span>
		</p>
<img src ="http://www.blogjava.net/meil/aggbug/74167.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/meil/" target="_blank">向东博客</a> 2006-10-10 08:57 <a href="http://www.blogjava.net/meil/archive/2006/10/10/74167.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>JAVA字符集</title><link>http://www.blogjava.net/meil/archive/2006/10/10/74209.html</link><dc:creator>向东博客</dc:creator><author>向东博客</author><pubDate>Tue, 10 Oct 2006 00:56:00 GMT</pubDate><guid>http://www.blogjava.net/meil/archive/2006/10/10/74209.html</guid><wfw:comment>http://www.blogjava.net/meil/comments/74209.html</wfw:comment><comments>http://www.blogjava.net/meil/archive/2006/10/10/74209.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/meil/comments/commentRss/74209.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/meil/services/trackbacks/74209.html</trackback:ping><description><![CDATA[1. 概述 <br /><br />本文主要包括以下几个方面：编码基本知识，java，系统软件，url，工具软件等。 <br /><br />在下面的描述中，将以"中文"两个字为例，经查表可以知道其GB2312编码是"d6d0 cec4"，Unicode编码为"4e2d 6587"，UTF编码就是"e4b8ad e69687"。注意，这两个字没有iso8859-1编码，但可以用iso8859-1编码来"表示"。 <br /><br />2. 编码基本知识 <br /><br />最早的编码是iso8859-1，和ascii编码相似。但为了方便表示各种各样的语言，逐渐出现了很多标准编码，重要的有如下几个。 <br /><br />2.1. iso8859-1 <br /><br />属于单字节编码，最多能表示的字符范围是0-255，应用于英文系列。比如，字母a的编码为0x61=97。 <br /><br />很明显，iso8859-1编码表示的字符范围很窄，无法表示中文字符。但是，由于是单字节编码，和计算机最基础的表示单位一致，所以很多时候，仍旧使用iso8859-1编码来表示。而且在很多协议上，默认使用该编码。比如，虽然"中文"两个字不存在iso8859-1编码，以gb2312编码为例，应该是"d6d0 cec4"两个字符，使用iso8859-1编码的时候则将它拆开为4个字节来表示："d6 d0 ce c4"（事实上，在进行存储的时候，也是以字节为单位处理的）。而如果是UTF编码，则是6个字节"e4 b8 ad e6 96 87"。很明显，这种表示方法还需要以另一种编码为基础。 <br /><br />2.2. GB2312/GBK <br /><br />这就是汉子的国标码，专门用来表示汉字，是双字节编码，而英文字母和iso8859-1一致（兼容iso8859-1编码）。其中gbk编码能够用来同时表示繁体字和简体字，而gb2312只能表示简体字，gbk是兼容gb2312编码的。 <br /><br />2.3. unicode <br /><br />这是最统一的编码，可以用来表示所有语言的字符，而且是定长双字节（也有四字节的）编码，包括英文字母在内。所以可以说它是不兼容iso8859-1编码的，也不兼容任何编码。不过，相对于iso8859-1编码来说，uniocode编码只是在前面增加了一个0字节，比如字母a为"00 61"。 <br /><br />需要说明的是，定长编码便于计算机处理（注意GB2312/GBK不是定长编码），而unicode又可以用来表示所有字符，所以在很多软件内部是使用unicode编码来处理的，比如java。 <br /><br />2.4. UTF <br /><br />考虑到unicode编码不兼容iso8859-1编码，而且容易占用更多的空间：因为对于英文字母，unicode也需要两个字节来表示。所以unicode不便于传输和存储。因此而产生了utf编码，utf编码兼容iso8859-1编码，同时也可以用来表示所有语言的字符，不过，utf编码是不定长编码，每一个字符的长度从1-6个字节不等。另外，utf编码自带简单的校验功能。一般来讲，英文字母都是用一个字节表示，而汉字使用三个字节。 <br /><br />注意，虽然说utf是为了使用更少的空间而使用的，但那只是相对于unicode编码来说，如果已经知道是汉字，则使用GB2312/GBK无疑是最节省的。不过另一方面，值得说明的是，虽然utf编码对汉字使用3个字节，但即使对于汉字网页，utf编码也会比unicode编码节省，因为网页中包含了很多的英文字符。 <br /><br />3. java对字符的处理 <br /><br />在java应用软件中，会有多处涉及到字符集编码，有些地方需要进行正确的设置，有些地方需要进行一定程度的处理。 <br /><br />3.1. getBytes(charset) <br /><br />这是java字符串处理的一个标准函数，其作用是将字符串所表示的字符按照charset编码，并以字节方式表示。注意字符串在java内存中总是按unicode编码存储的。比如"中文"，正常情况下（即没有错误的时候）存储为"4e2d 6587"，如果charset为"gbk"，则被编码为"d6d0 cec4"，然后返回字节"d6 d0 ce c4"。如果charset为"utf8"则最后是"e4 b8 ad e6 96 87"。如果是"iso8859-1"，则由于无法编码，最后返回 "3f 3f"（两个问号）。 <br /><br />3.2. new String(charset) <br /><br />这是java字符串处理的另一个标准函数，和上一个函数的作用相反，将字节数组按照charset编码进行组合识别，最后转换为unicode存储。参考上述getBytes的例子，"gbk" 和"utf8"都可以得出正确的结果"4e2d 6587"，但iso8859-1最后变成了"003f 003f"（两个问号）。 <br /><br />因为utf8可以用来表示/编码所有字符，所以new String( str.getBytes( "utf8" ), "utf8" ) === str，即完全可逆。 <br /><br />3.3. setCharacterEncoding() <br /><br />该函数用来设置http请求或者相应的编码。 <br /><br />对于request，是指提交内容的编码，指定后可以通过getParameter()则直接获得正确的字符串，如果不指定，则默认使用iso8859-1编码，需要进一步处理。参见下述"表单输入"。值得注意的是在执行setCharacterEncoding()之前，不能执行任何getParameter()。java doc上说明：This method must be called prior to reading request parameters or reading input using getReader()。而且，该指定只对POST方法有效，对GET方法无效。分析原因，应该是在执行第一个getParameter()的时候，java将会按照编码分析所有的提交内容，而后续的getParameter()不再进行分析，所以setCharacterEncoding()无效。而对于GET方法提交表单是，提交的内容在URL中，一开始就已经按照编码分析所有的提交内容，setCharacterEncoding()自然就无效。 <br /><br />对于response，则是指定输出内容的编码，同时，该设置会传递给浏览器，告诉浏览器输出内容所采用的编码。 <br /><br />3.4. 处理过程 <br /><br />下面分析两个有代表性的例子，说明java对编码有关问题的处理方法。 <br /><br />3.4.1. 表单输入 <br /><br />User input *(gbk:d6d0 cec4) browser *(gbk:d6d0 cec4) web server iso8859-1(00d6 00d 000ce 00c4) class，需要在class中进行处理：getbytes("iso8859-1")为d6 d0 ce c4，new String("gbk")为d6d0 cec4，内存中以unicode编码则为4e2d 6587。 <br /><br />l 用户输入的编码方式和页面指定的编码有关，也和用户的操作系统有关，所以是不确定的，上例以gbk为例。 <br /><br />l 从browser到web server，可以在表单中指定提交内容时使用的字符集，否则会使用页面指定的编码。而如果在url中直接用?的方式输入参数，则其编码往往是操作系统本身的编码，因为这时和页面无关。上述仍旧以gbk编码为例。 <br /><br />l Web server接收到的是字节流，默认时（getParameter）会以iso8859-1编码处理之，结果是不正确的，所以需要进行处理。但如果预先设置了编码（通过request. setCharacterEncoding ()），则能够直接获取到正确的结果。 <br /><br />l 在页面中指定编码是个好习惯，否则可能失去控制，无法指定正确的编码。 <br /><br />3.4.2. 文件编译 <br /><br />假设文件是gbk编码保存的，而编译有两种编码选择：gbk或者iso8859-1，前者是中文windows的默认编码，后者是linux的默认编码，当然也可以在编译时指定编码。 <br /><br />Jsp *(gbk:d6d0 cec4) java file *(gbk:d6d0 cec4) compiler read uincode(gbk: 4e2d 6587; iso8859-1: 00d6 00d 000ce 00c4) compiler write utf(gbk: e4b8ad e69687; iso8859-1: *) compiled file unicode(gbk: 4e2d 6587; iso8859-1: 00d6 00d 000ce 00c4) class。所以用gbk编码保存，而用iso8859-1编译的结果是不正确的。 <br /><br />class unicode(4e2d 6587) system.out / jsp.out gbk(d6d0 cec4) os console / browser。 <br /><br />l 文件可以以多种编码方式保存，中文windows下，默认为ansi/gbk。 <br /><br />l 编译器读取文件时，需要得到文件的编码，如果未指定，则使用系统默认编码。一般class文件，是以系统默认编码保存的，所以编译不会出问题，但对于jsp文件，如果在中文windows下编辑保存，而部署在英文linux下运行/编译，则会出现问题。所以需要在jsp文件中用pageEncoding指定编码。 <br /><br />l Java编译的时候会转换成统一的unicode编码处理，最后保存的时候再转换为utf编码。 <br /><br />l 当系统输出字符的时候，会按指定编码输出，对于中文windows下，System.out将使用gbk编码，而对于response（浏览器），则使用jsp文件头指定的contentType，或者可以直接为response指定编码。同时，会告诉browser网页的编码。如果未指定，则会使用iso8859-1编码。对于中文，应该为browser指定输出字符串的编码。 <br /><br />l browser显示网页的时候，首先使用response中指定的编码（jsp文件头指定的contentType最终也反映在response上），如果未指定，则会使用网页中meta项指定中的contentType。 <br /><br />3.5. 几处设置 <br /><br />对于web应用程序，和编码有关的设置或者函数如下。 <br /><br />3.5.1. jsp编译 <br /><br />指定文件的存储编码，很明显，该设置应该置于文件的开头。例如：&lt;%@page pageEncoding="GBK"%&gt;。另外，对于一般class文件，可以在编译的时候指定编码。 <br /><br />3.5.2. jsp输出 <br /><br />指定文件输出到browser是使用的编码，该设置也应该置于文件的开头。例如：&lt;%@ page contentType="text/html; charset= GBK" %&gt;。该设置和response.setCharacterEncoding("GBK")等效。 <br /><br />3.5.3. meta设置 <br /><br />指定网页使用的编码，该设置对静态网页尤其有作用。因为静态网页无法采用jsp的设置，而且也无法执行response.setCharacterEncoding()。例如： <br /><br />如果同时采用了jsp输出和meta设置两种编码指定方式，则jsp指定的优先。因为jsp指定的直接体现在response中。 <br /><br />需要注意的是，apache有一个设置可以给无编码指定的网页指定编码，该指定等同于jsp的编码指定方式，所以会覆盖静态网页中的meta指定。所以有人建议关闭该设置。 <br /><br />3.5.4. form设置 <br /><br />当浏览器提交表单的时候，可以指定相应的编码。例如： 
<form accept-charset="gb2312">。一般不必不使用该设置，浏览器会直接使用网页的编码。 <br /><br />4. 系统软件 <br /><br />下面讨论几个相关的系统软件。 <br /><br />4.1. mysql数据库 <br /><br />很明显，要支持多语言，应该将数据库的编码设置成utf或者unicode，而utf更适合与存储。但是，如果中文数据中包含的英文字母很少，其实unicode更为适合。 <br /><br />数据库的编码可以通过mysql的配置文件设置，例如default-character-set=utf8。还可以在数据库链接URL中设置，例如： useUnicode=true&amp;characterEncoding=UTF-8。注意这两者应该保持一致，在新的sql版本里，在数据库链接URL里可以不进行设置，但也不能是错误的设置。 <br /><br />4.2. apache <br /><br />appache和编码有关的配置在httpd.conf中，例如AddDefaultCharset UTF-8。如前所述，该功能会将所有静态页面的编码设置为UTF-8，最好关闭该功能。 <br /><br />另外，apache还有单独的模块来处理网页响应头，其中也可能对编码进行设置。 <br /><br />4.3. linux默认编码 <br /><br />这里所说的linux默认编码，是指运行时的环境变量。两个重要的环境变量是LC_ALL和LANG，默认编码会影响到java URLEncode的行为，下面有描述。 <br /><br />建议都设置为"zh_CN.UTF-8"。 <br /><br />4.4. 其它 <br /><br />为了支持中文文件名，linux在加载磁盘时应该指定字符集，例如：mount /dev/hda5 /mnt/hda5/ -t ntfs -o iocharset=gb2312。 <br /><br />另外，如前所述，使用GET方法提交的信息不支持request.setCharacterEncoding()，但可以通过tomcat的配置文件指定字符集，在tomcat的server.xml文件中，形如：<connector ...="" uriencoding="GBK" />。这种方法将统一设置所有请求，而不能针对具体页面进行设置，也不一定和browser使用的编码相同，所以有时候并不是所期望的。 <br /><br />5. URL地址 <br /><br />URL地址中含有中文字符是很麻烦的，前面描述过使用GET方法提交表单的情况，使用GET方法时，参数就是包含在URL中。 <br /><br />5.1. URL编码 <br /><br />对于URL中的一些特殊字符，浏览器会自动进行编码。这些字符除了"/?&amp;"等外，还包括unicode字符，比如汉子。这时的编码比较特殊。 <br /><br />IE有一个选项"总是使用UTF-8发送URL"，当该选项有效时，IE将会对特殊字符进行UTF-8编码，同时进行URL编码。如果改选项无效，则使用默认编码"GBK"，并且不进行URL编码。但是，对于URL后面的参数，则总是不进行编码，相当于UTF-8选项无效。比如"中文.html?a=中文"，当UTF-8选项有效时，将发送链接"%e4%b8%ad%e6%96%87.html?a=x4ex2dx65x87"；而UTF-8选项无效时，将发送链接"x4ex2dx65x87.html?a=x4ex2dx65x87"。注意后者前面的"中文"两个字只有4个字节，而前者却有18个字节，这主要时URL编码的原因。 <br /><br />当web server（tomcat）接收到该链接时，将会进行URL解码，即去掉"%"，同时按照ISO8859-1编码（上面已经描述，可以使用URLEncoding来设置成其它编码）识别。上述例子的结果分别是"ue4ub8uadue6u96u87.html?a=u4eu2du65u87"和"u4eu2du65u87.html?a=u4eu2du65u87"，注意前者前面的"中文"两个字恢复成了6个字符。这里用"u"，表示是unicode。 <br /><br />所以，由于客户端设置的不同，相同的链接，在服务器上得到了不同结果。这个问题不少人都遇到，却没有很好的解决办法。所以有的网站会建议用户尝试关闭UTF-8选项。不过，下面会描述一个更好的处理办法。 <br /><br />5.2. rewrite <br /><br />熟悉的人都知道，apache有一个功能强大的rewrite模块，这里不描述其功能。需要说明的是该模块会自动将URL解码（去除%），即完成上述web server（tomcat）的部分功能。有相关文档介绍说可以使用[NE]参数来关闭该功能，但我试验并未成功，可能是因为版本（我使用的是apache 2.0.54）问题。另外，当参数中含有"?&amp; "等符号的时候，该功能将导致系统得不到正常结果。 <br /><br />rewrite本身似乎完全是采用字节处理的方式，而不考虑字符串的编码，所以不会带来编码问题。 <br /><br />5.3. URLEncode.encode() <br /><br />这是Java本身提供对的URL编码函数，完成的工作和上述UTF-8选项有效时浏览器所做的工作相似。值得说明的是，java已经不赞成不指定编码来使用该方法（deprecated）。应该在使用的时候增加编码指定。 <br /><br />当不指定编码的时候，该方法使用系统默认编码，这会导致软件运行结果得不确定。比如对于"中文"，当系统默认编码为"gb2312"时，结果是"%4e%2d%65%87"，而默认编码为"UTF-8"，结果却是"%e4%b8%ad%e6%96%87"，后续程序将难以处理。另外，这儿说的系统默认编码是由运行tomcat时的环境变量LC_ALL和LANG等决定的，曾经出现过tomcat重启后就出现乱码的问题，最后才郁闷的发现是因为修改修改了这两个环境变量。 <br /><br />建议统一指定为"UTF-8"编码，可能需要修改相应的程序。 <br /><br />5.4. 一个解决方案 <br /><br />上面说起过，因为浏览器设置的不同，对于同一个链接，web server收到的是不同内容，而软件系统有无法知道这中间的区别，所以这一协议目前还存在缺陷。 <br /><br />针对具体问题，不应该侥幸认为所有客户的IE设置都是UTF-8有效的，也不应该粗暴的建议用户修改IE设置，要知道，用户不可能去记住每一个web server的设置。所以，接下来的解决办法就只能是让自己的程序多一点智能：根据内容来分析编码是否UTF-8。 <br /><br />比较幸运的是UTF-8编码相当有规律，所以可以通过分析传输过来的链接内容，来判断是否是正确的UTF-8字符，如果是，则以UTF-8处理之，如果不是，则使用客户默认编码（比如"GBK"），下面是一个判断是否UTF-8的例子，如果你了解相应规律，就容易理解。 <br /><br />public static boolean isValidUtf8(byte[] b,int aMaxCount){ <br /><br />int lLen=b.length,lCharCount=0; <br /><br />for(int i=0;i<llen &&="" lcharcount<amaxcount;++lcharcount){=""><br /><br />byte lByte=b[i++];//to fast operation, ++ now, ready for the following for(;;) <br /><br />if(lByte&gt;=0) continue;//&gt;=0 is normal ascii <br /><br />if(lByte&lt;(byte)0xc0 || lByte&gt;(byte)0xfd) return false; <br /><br />int lCount=lByte&gt;(byte)0xfc?5:lByte&gt;(byte)0xf8?4 <br /><br />:lByte&gt;(byte)0xf0?3:lByte&gt;(byte)0xe0?2:1; <br /><br />if(i+lCount&gt;lLen) return false; <br /><br />for(int j=0;j<lcount;++j,++i) if(b[i]="">=(byte)0xc0) return false; <br /><br />} <br /><br />return true; <br /><br />} <br /><br />相应地，一个使用上述方法的例子如下： <br /><br />public static String getUrlParam(String aStr,String aDefaultCharset) <br /><br />throws UnsupportedEncodingException{ <br /><br />if(aStr==null) return null; <br /><br />byte[] lBytes=aStr.getBytes("ISO-8859-1"); <br /><br />return new String(lBytes,StringUtil.isValidUtf8(lBytes)?"utf8":aDefaultCharset); <br /><br />} <br /><br />不过，该方法也存在缺陷，如下两方面： <br /><br />l 没有包括对用户默认编码的识别，这可以根据请求信息的语言来判断，但不一定正确，因为我们有时候也会输入一些韩文，或者其他文字。 <br /><br />l 可能会错误判断UTF-8字符，一个例子是"学习"两个字，其GBK编码是" xd1xa7xcfxb0"，如果使用上述isValidUtf8方法判断，将返回true。可以考虑使用更严格的判断方法，不过估计效果不大。 <br /><br />有一个例子可以证明google也遇到了上述问题，而且也采用了和上述相似的处理方法，比如，如果在地址栏中输入"http://www.google.com/search?hl=zh-CN&amp;newwindow=1&amp;q=学习"，google将无法正确识别，而其他汉字一般能够正常识别。 <br /><br />最后，应该补充说明一下，如果不使用rewrite规则，或者通过表单提交数据，其实并不一定会遇到上述问题，因为这时可以在提交数据时指定希望的编码。另外，中文文件名确实会带来问题，应该谨慎使用。 <br /><br />6. 其它 <br /><br />下面描述一些和编码有关的其他问题。 <br /><br />6.1. SecureCRT <br /><br />除了浏览器和控制台与编码有关外，一些客户端也很有关系。比如在使用SecureCRT连接linux时，应该让SecureCRT的显示编码（不同的session，可以有不同的编码设置）和linux的编码环境变量保持一致。否则看到的一些帮助信息，就可能是乱码。 <br /><br />另外，mysql有自己的编码设置，也应该保持和SecureCRT的显示编码一致。否则通过SecureCRT执行sql语句的时候，可能无法处理中文字符，查询结果也会出现乱码。 <br /><br />对于Utf-8文件，很多编辑器（比如记事本）会在文件开头增加三个不可见的标志字节，如果作为mysql的输入文件，则必须要去掉这三个字符。（用linux的vi保存可以去掉这三个字符）。一个有趣的现象是，在中文windows下，创建一个新txt文件，用记事本打开，输入"连通"两个字，保存，再打开，你会发现两个字没了，只留下一个小黑点。 <br /><br />6.2. 过滤器 <br /><br />如果需要统一设置编码，则通过filter进行设置是个不错的选择。在filter class中，可以统一为需要的请求或者回应设置编码。参加上述setCharacterEncoding()。这个类apache已经给出了可以直接使用的例子SetCharacterEncodingFilter。 <br /><br />6.3. POST和GET <br /><br />很明显，以POST提交信息时，URL有更好的可读性，而且可以方便的使用setCharacterEncoding()来处理字符集问题。但GET方法形成的URL能够更容易表达网页的实际内容，也能够用于收藏。 <br /><br />从统一的角度考虑问题，建议采用GET方法，这要求在程序中获得参数是进行特殊处理，而无法使用setCharacterEncoding()的便利，如果不考虑rewrite，就不存在IE的UTF-8问题，可以考虑通过设置URIEncoding来方便获取URL中的参数。 <br /><br />6.4. 简繁体编码转换 <br /><br />GBK同时包含简体和繁体编码，也就是说同一个字，由于编码不同，在GBK编码下属于两个字。有时候，为了正确取得完整的结果，应该将繁体和简体进行统一。可以考虑将UTF、GBK中的所有繁体字，转换为相应的简体字，BIG5编码的数据，也应该转化成相应的简体字。当然，仍旧以UTF编码存储。 <br /><br />例如，对于"语言 ?言"，用UTF表示为"xE8xAFxADxE8xA8x80 xE8xAAx9ExE8xA8x80"，进行简繁体编码转换后应该是两个相同的 "xE8xAFxADxE8xA8x80&gt;"。 <br /></lcount;++j,++i)></llen></form><img src ="http://www.blogjava.net/meil/aggbug/74209.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/meil/" target="_blank">向东博客</a> 2006-10-10 08:56 <a href="http://www.blogjava.net/meil/archive/2006/10/10/74209.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item></channel></rss>