Tomcat使用端口号访问制定地址解决办法

本文介绍了如何配置Apache Tomcat服务器,以便通过特定端口号访问时直接跳转到项目首页,而非Tomcat默认欢迎页面。主要内容包括修改Connector端口、设置URIEncoding支持URL中文参数,以及在ROOT目录下创建web.xml实现自动跳转。同时提到了针对404错误页面的定制方法和可能的Apache配置方案。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Tomcat 服务器server.xml的关键参数配置

特此声明:以下配置为apache-tomcat-6.0.20的配置。

1,配置tomcat服务器访问端口,只需配置Connector的port端口即可。Tomcat默认为8080,现修改port参数值为8888。

   <!--  Define a non-SSL HTTP/1.1 Connector on port 8080  -->
    <Connector port="8888" protocol="HTTP/1.1" 
               connectionTimeout="20000" 
               redirectPort="8443" URIEncoding='UTF-8' />

2,配置tomcat支持URL中文参数,只需添加Connector的URIEncoding参数即可,默认情况下该参数未被配置。要支持URL参数支持中文,我这里设置URIEncoding="UTF-8"。你也可以设置为GBK。

**************************************************************************************************************************************************************************************************************************************

以下内容为Tomcat使用端口号访问制定地址解决办法。

最近由于项目需要。使用端口号访问的时候,需要跳转到首页。而不是进入tomcat 欢迎页面。

以下为局部配置。(推荐使用)

在ROOT下边建立web.xml文件

<web-app xmlns="http://java.sun.com/xml/ns/javaee"
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
   version="2.5">

        <welcome-file-list>
                <welcome-file>/index.jsp</welcome-file>
        </welcome-file-list>
        <error-page>
                <error-code>404</error-code>
                <location>/resourceNotFound.jsp</location>
        </error-page>
</web-app>

然后在ROOT下边建立两个文件 index.jsp resourceNotFound.jsp

index.jsp内容如下:

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
        <base href="<%=basePath%>"/>
    <META HTTP-EQUIV="Refresh" CONTENT="0;URL=<%=basePath%>AMRWeb/shopping/search/showSearchPage_amrshop.action">
</head>
<body>
</body>
</html>
resourceNotFound.jsp内容如下:

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path;
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>404 未找到页面!</title>
        <script type="text/javascript" src="<%=basePath%>/AMRWeb/jquery/jquery.js"></script>
        <link rel="stylesheet" type="text/css" href="<%=basePath%>/AMRWeb/css/amrmain.css"/>
        <link rel="stylesheet" type="text/css" href="<%=basePath%>/AMRWeb/css/amrProject.css"/>
        <link rel="shortcut icon" href="<%=basePath%>/AMRWeb/pictures/icons/favicon.ico" type="image/x-icon">
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    <meta http-equiv="pragma" content="no-cache">
        <meta http-equiv="cache-control" content="no-cache,must-revalidate">
        <meta http-equiv="expires" content="0">


</head>
<body class="pgInterstSearch">

 <script type="text/javascript">
        $(function() {
                var second=5;
            $("#secondSpan").text(second+"");
            var fuc = function(){
                second-=1;
                $("#secondSpan").text(second+"");
                if(second==0) {
                    window.location="<%=basePath%>/AMRWeb/shopping/search/showSearchPage_amrshop.action";
                }else{
                    setTimeout(fuc,1000);
                }
            };
            fuc();//开始倒计时
        });
     </script>


<table class="interstOuterTable">
<tr>
        <td class="interstOuterTD">
                <div class="interstSearch">
                        <div class="interstSearchInner">
                                <div class="brandHead"><div class="logo"><img src="<%=basePath%>/AMRWeb/pictures/decor/interst-logo.gif"> </div></div>
                                <div style='font-size:25px;font-family:"黑体";font-weight: bold;padding:42px 0 32px 48px;'>ERROR-404找不到该页面!</div>
                                <div class="bar"></div>
                                <div class="text">
                                        非常抱歉,在我们的服务器上,无法找到您要访问的页面,请确认网址是否正确。</br>
                                         <span id="secondSpan"></span> 秒钟后,将自动转入<<<a href="<%=basePath%>/AMRWeb/shopping/search/showSearchPage_amrshop.action">首页</a></br>
                                        Sorry, The server can't find the requested page. For instance, the server often returns this code if the request is for a page that doesn't exist on the server.
                                </div>
                        </div>
                </div>
        </td>
</tr>
</table>

</body>
</html>

2.除此之外。项目下边的web.xml 也需要配置同样内容。(如:我的是在AMRWeb 项目下边。也就是你项目本身带的index.jsp位置。)


——————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————————

以下为全局配置。(这种方式也可以实现。但是最好不要用全局)

你可以在ROOT下边创建一个index.jsp

然后坐一个自动跳转过去。步骤如下:

你可以使用linux 找到ROOT 目录:

我的目录坐下参考:

cd /amr/apache-tomcat-6.0.35/webapps/ROOT

vi index.jsp 或者touch index.jsp 

代码如下:

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>My JSP 'index.jsp' starting page</title>
	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0">    
	<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
	<meta http-equiv="description" content="This is my page">
	<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->
	<script type="text/javascript" src="<%=path%>/jquery/jquery.js"></script>
	<script type="text/javascript">
	window.location.href="shopping/search/showSearchPage_amrshop.action";
	</script>
  </head>
  
  <body >
  
  </body>
</html>

但是项目都要求要友好一些。所以大家可以做成404错误页面那样。

你可以把内容ROOT下的index.jsp文件内容换成别的

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path;
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>404 未找到页面!</title>
        <script type="text/javascript" src="<%=basePath%>/AMRWeb/jquery/jquery.js"></script>
        <link rel="stylesheet" type="text/css" href="<%=basePath%>/AMRWeb/css/amrmain.css"/>
        <link rel="stylesheet" type="text/css" href="<%=basePath%>/AMRWeb/css/amrProject.css"/>
        <link rel="shortcut icon" href="<%=basePath%>/AMRWeb/pictures/icons/favicon.ico" type="image/x-icon">
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    <meta http-equiv="pragma" content="no-cache">
        <meta http-equiv="cache-control" content="no-cache,must-revalidate">
        <meta http-equiv="expires" content="0">


</head>
<body class="pgInterstSearch">

 <script type="text/javascript">
        $(function() {
                var second=5;
            $("#secondSpan").text(second+"");
            var fuc = function(){
                second-=1;
                $("#secondSpan").text(second+"");
                if(second==0) {
                    window.location="<%=basePath%>/AMRWeb/shopping/search/showSearchPage_amrshop.action";
                }else{
                    setTimeout(fuc,1000);
                }
            };
            fuc();//开始倒计时
        });
     </script>


<table class="interstOuterTable">
<tr>
        <td class="interstOuterTD">
                <div class="interstSearch">
                        <div class="interstSearchInner">
                                <div class="brandHead"><div class="logo"><img src="<%=basePath%>/AMRWeb/pictures/decor/interst-logo.gif"> </div></div>
                                <div style='font-size:25px;font-family:"黑体";font-weight: bold;padding:42px 0 32px 48px;'>ERROR-404找不到该页面!</div>
                                <div class="bar"></div>
                                <div class="text">
                                        非常抱歉,在我们的服务器上,无法找到您要访问的页面,请确认网址是否正确。</br>
                                         <span id="secondSpan"></span> 秒钟后,将自动转入<<<a href="<%=basePath%>/AMRWeb/shopping/search/showSearchPage_amrshop.action">首页</a></br>
                                        Sorry, The server can't find the requested page. For instance, the server often returns this code if the request is for a page that doesn't exist on the server.
                                </div>
                        </div>
                </div>
        </td>
</tr>
</table>

</body>
</html>

当然这只是你访问端口的时候的错误页面。如:localhost:8080 (正确的情况为localhost:8080/AMRWeb)

还有一种情况就是当你访问端口后加错误路径的情况下 产生的404问题.如:localhost:8080/AMRWeb/sss

这种情况的发生你得单独配置。步骤如下:

使用linux操作如下:

我的目录:cd /amr/amrweb/apache-tomcat-6.0.13/conf

vi web.xml 编辑内容如下:

<error-page>
<error-code>404</error-code>
<location>/error.jsp</location>
</error-page>
<error-page>
<error-code>500</error-code>
<location>/error.jsp</location>
</error-page>

然后找到cd /home/amrweb/apache-tomcat-6.0.13/webapps/AMRWeb  目录下

vi error.jsp 具体内容如下:

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path;
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>404 未找到页面!</title>
        <script type="text/javascript" src="<%=basePath%>/jquery/jquery.js"></script>
        <link rel="stylesheet" type="text/css" href="<%=basePath%>/css/amrmain.css"/>
        <link rel="stylesheet" type="text/css" href="<%=basePath%>/css/amrProject.css"/>
        <link rel="shortcut icon" href="<%=basePath%>/pictures/icons/favicon.ico" type="image/x-icon">
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    <meta http-equiv="pragma" content="no-cache">
        <meta http-equiv="cache-control" content="no-cache,must-revalidate">
        <meta http-equiv="expires" content="0">


</head>
<body class="pgInterstSearch">

 <script type="text/javascript">
        $(function() {
                var second=5;
            $("#secondSpan").text(second+"");
            var fuc = function(){
                second-=1;
                $("#secondSpan").text(second+"");
                if(second==0) {
                    window.location="<%=basePath%>/shopping/search/showSearchPage_amrshop.action";
                }else{
                    setTimeout(fuc,1000);
                }
            };
            fuc();//开始倒计时
        });
     </script>


<table class="interstOuterTable">
<tr>
        <td class="interstOuterTD">
                <div class="interstSearch">
                        <div class="interstSearchInner">
                                <div class="brandHead"><div class="logo"><img src="<%=basePath%>/pictures/decor/interst-logo.gif"> </div></div>
                                <div style='font-size:25px;font-family:"黑体";font-weight: bold;padding:42px 0 32px 48px;'>ERROR-404找不到该页面!</div>
                                <div class="bar"></div>
                                <div class="text">
                                        非常抱歉,在我们的服务器上,无法找到您要访问的页面,请确认网址是否正确。</br>
                                         <span id="secondSpan"></span> 秒钟后,将自动转入<<<a href="<%=basePath%>/shopping/search/showSearchPage_amrshop.action">首页</a></br>
                                        Sorry, The server can't find the requested page. For instance, the server often returns this code if the request is for a page that doesn't exist on the server.
                                </div>
                        </div>
                </div>
        </td>
</tr>
</table>

</body>
</html>

需要注意的是:看清下目录。因为跟ROOT目录不一样。所以需要注意下


**********************************************************************************************************************************************************************************************************************************************************************************************************************************************************************************




还有一种方法是配置apache 。但是由于时间原因,没有研究。有大仙知道怎么配的。请告诉小弟一下。万分感谢。

写的不对的地方请各位纠正~~~




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值