Tomcat配置技巧

来源:互联网 发布:淘宝问答入口 编辑:程序博客网 时间:2024/05/12 15:00

做jsp时,关于tomcat服务器优化,常遇到的一些简单问题的解决方法: 


1.如何禁止访问目录列表:
    修改tomcat x.x/conf/web.xml内的一个属性值,就是把true 改为 false 
在web.xml里找到以下代码 

    <servlet>
        
<servlet-name>default</servlet-name>
        
<servlet-class>org.apache.catalina.servlets.DefaultServlet</servlet-class>
        
<init-param>
            
<param-name>debug</param-name>
            
<param-value>0</param-value>
        
</init-param>
        
<init-param>
            
<param-name>listings</param-name>
            
<param-value>true</param-value>
        
</init-param>
        
<load-on-startup>1</load-on-startup>
    
</servlet>
    把其中的
            <param-name>listings</param-name>
            
<param-value>true</param-value>
    改为 
< param-name > listings </ param-name > 
< param-value > false </ param-value > 

2.如果某文件资源没有找到,服务器要报404错误: 
  如果执行的某个JSP文件产生 NullPointException,会显示一些异常提示代码,为了让用户看到更更友好的页面,可在自己的虚拟目录的WEB-INF/web.xml中作如下的设置 
<error-page>
    <error-code>404</error-code>
    <location>/notFound.jsp</location>
</error-page>

<!--对特定的Exception捕获-->
<error-page>
    <exception-type>java.lang.NullPointerException</exception-type>
    </location>/null.jsp</location>
</error-page>
    同理,你也可以设置抛出其它异常时应该显示的页面!另外在jsp页面最上方加上这样一句话,也可以捕捉NullPointerException这种异常,此时转向error.jsp 
3.设置session的超时时间 (单位:分钟)
    <session-config>
        
<session-timeout>30</session-timeout>
    
</session-config>
4.设置默认欢迎页面(当不输入文件名,只输入目录时起作用) 
    <welcome-file-list>
        
<welcome-file>index.html</welcome-file>
        
<welcome-file>index.htm</welcome-file>
        
<welcome-file>index.jsp</welcome-file>
    
</welcome-file-list>