tomcat禁止访问路径与文件、错误页面跳转配置

tomcat禁止访问文件与文件目录

最近项目扫描的时候遇到一个问题,需要在tomcat中限制对包含项目信息文件的访问,在使用eclipse写项目是,会生成一个.project文件,这个文件里面包含了项目的关键信息,而且有时候删除扔能继续访问,于是只能对这个文件限制web访问,否则会存在安全漏洞,以下是解决办法:

在tomcat/conf/web.xml中加入以下代码

<security-constraint>
    <display-name>Forbidden</display-name>
    <web-resource-collection>
        <web-resource-name>aaa</web-resource-name><!--//填入要保护的项目名-->
            <url-pattern>/.project</url-pattern><!--//填入需要被限制的文件路径-->
    </web-resource-collection>
    <auth-constraint>
        <role-name/>
    </auth-constraint>
</security-constraint>

tomcat访问报错跳转

设置自定义的项目报错页面,同样在web.xml中添加:

<error-page>  
        <error-code>403</error-code>  
        <location>/error/authlimit.jsp</location>  
  </error-page>
  
  <error-page>  
        <error-code>404</error-code>  
        <location>/error/pagenotfound.jsp</location>  
  </error-page>
  
  <error-page>  
        <error-code>500</error-code>  
        <location>/error/servererror.jsp</location>  
  </error-page>

访问找不到页面 根据情况进行调整即可

<%@ page contentType="text/html; charset=UTF-8"%>
<%@ page import="org.jxstar.util.config.SystemVar,
				 org.jxstar.security.LicenseVar,
				 java.util.Map,
				 org.jxstar.util.factory.FactoryUtil,
				 org.jxstar.control.login.OneLoginUtil" %>
<%
	String contextpath = request.getContextPath();
%>
<!DOCTYPE html>
<html>
  <head>
    <title>403</title>
	
    <meta name="keywords" content="keyword1,keyword2,keyword3">
    <meta name="description" content="this is my page">
    <meta name="content-type" content="text/html; charset=UTF-8">
    
    <!--<link rel="stylesheet" type="text/css" href="./styles.css">-->
	<style>
		body{
			 background-color:#1196EE;
		}
		img{
			width:300px;
		}
		.error-info{
			width:30%;
			margin:5% 35%;
			text-align:center;
		}
		.error-desc span{
			color:#eee;
			font-size:18px;
		}
	</style>

  </head>
  
  <body>
	  <div class="error-info">
    		<img src="<%=contextpath %>/resources/images/error/403.png" />
    		<div class="error-desc">
	    		<span>Sorry,您访问的页面不存在</span>
    		</div>
	  </div>
  </body>
</html>
<think>好的,用户现在想了解如何基于年龄判断实现JSP页面跳转,并处理错误页面和统一显示友情链接。之前已经给过一个示例,但用户现在提供了更具体的需求,需要按照login.jsp提交到manger_control.jsp判断年龄,跳转到welcome.jsp或forbid.jsp,同时设置统一的错误页面errorhandle.jsp和友情链接。 首先,我需要确保所有步骤符合用户的指示。用户提到引用了一些配置,比如jdbcUrl和Context,但这些看起来像是数据库和Tomcat配置,可能和当前问题关联不大,但可能需要确认是否需要数据库操作。不过用户的问题主要集中在页面跳转和错误处理,所以可能暂时不需要处理数据库部分。 接下来,用户需要login.jsp提交到manger_control.jsp。之前的示例中,表单提交到login1.jsp,现在需要调整表单的action为manger_control.jsp。同时,用户希望根据年龄大于18岁跳转到welcome.jsp,否则到forbid.jsp,并处理错误情况,比如年龄输入非数字的情况,跳转到errorhandle.jsp。此外,所有页面需要显示共同的友情链接,并且错误处理页面也是统一的。 需要确保每个JSP页面(login.jsp, manger_control.jsp, welcome.jsp, forbid.jsp, errorhandle.jsp)都包含友情链接部分。可以考虑使用JSP的include指令或者引入公共文件,比如footer.jsp,来统一管理友情链接,避免重复代码。 在错误处理方面,之前的示例中使用的是try-catch块来捕获NumberFormatException,然后通过JavaScript弹窗提示并跳转。但用户现在希望使用统一的错误处理页面errorhandle.jsp。因此,在捕获异常时,应该转发到errorhandle.jsp,而不是使用客户端脚本。可以使用JSP的errorPage指令或者配置全局错误页面,但用户可能希望在每个页面中显式处理错误,所以可能在manger_control.jsp中捕获异常,并转发到errorhandle.jsp,同时传递错误信息。 另外,需要确保所有页面都设置了isErrorPage属性,或者在web.xml中配置错误页面。不过根据用户需求,每个页面(welcome.jsp和forbid.jsp)都有自己的错误处理页面errorhandle.jsp,所以可能在每个页面的page指令中设置errorPage属性为errorhandle.jsp。 现在,需要编写各个JSP文件: 1. login.jsp:表单提交到manger_control.jsp,包含用户名和年龄输入,以及提交按钮。下方有友情链接。 2. manger_control.jsp:接收参数,判断年龄是否为数字,大于18则重定向到welcome.jsp,否则到forbid.jsp。如果转换年龄失败,转发到errorhandle.jsp并传递错误信息。 3. welcome.jsp和forbid.jsp:显示相应内容,包含友情链接,并设置errorPage为errorhandle.jsp。 4. errorhandle.jsp:显示错误信息,包含友情链接,并设置isErrorPage="true"。 在友情链接的处理上,每个页面都需要包含相同的HTML代码,所以最好将这部分提取到一个单独的JSP文件,比如footer.jsp,然后使用<%@ include file="footer.jsp" %>来包含,这样可以减少重复,方便维护。 需要注意的是,使用response.sendRedirect()时,参数可以通过URL传递,比如welcome.jsp?username=xxx,然后在目标页面中使用request.getParameter获取。而转发(forward)则可以在请求属性中传递信息,比如错误信息,使用request.setAttribute和request.getAttribute。 在错误处理时,如果年龄参数无法转换为整数,应该捕获异常,并将错误信息存储到请求属性中,然后转发到errorhandle.jsp。例如: 在manger_control.jsp中: ```jsp <% try { age = Integer.parseInt(ageParam); } catch (NumberFormatException e) { request.setAttribute("errorMessage", "年龄格式错误,请输入数字!"); request.getRequestDispatcher("errorhandle.jsp").forward(request, response); return; } %> ``` 在errorhandle.jsp中显示errorMessage。 此外,需要确保每个页面的page指令中设置errorPage属性,例如在welcome.jsp中: ```jsp <%@ page language="java" contentType="text/html; charset=UTF-8" errorPage="errorhandle.jsp" %> ``` 这样,当这些页面发生未捕获的异常时,会自动跳转到errorhandle.jsp。 最后,检查所有代码是否符合JSP语法,路径是否正确,比如表单的action是否正确指向manger_control.jsp,重定向的路径是否正确,友情链接的包含是否正确。 可能还需要考虑字符编码的问题,确保所有页面使用UTF-8编码,避免中文乱码。 总结步骤: 1. 创建login.jsp,包含表单,提交到manger_control.jsp,包含友情链接。 2. 创建manger_control.jsp,处理年龄参数,判断并重定向,处理错误并转发到errorhandle.jsp。 3. 创建welcome.jsp和forbid.jsp,显示相应内容,设置errorPage,包含友情链接。 4. 创建errorhandle.jsp,显示错误信息,包含友情链接,设置isErrorPage。 5. 提取友情链接到footer.jsp,并在各页面中包含。 需要验证每个步骤的正确性,确保错误处理机制有效,友情链接正确显示,跳转逻辑符合预期。</think>### 完整实现方案 #### 1. 登录页面 `login.jsp` ```jsp <%@ page contentType="text/html; charset=UTF-8" %> <html> <head> <title>用户登录</title> </head> <body> <h2>用户登录</h2> <form action="manger_control.jsp" method="post"> 姓名:<input type="text" name="username" required><br> 年龄:<input type="number" name="age" required><br> <input type="submit" value="提交"> </form> <!-- 友情链接 --> <%@ include file="footer.jsp" %> </body> </html> ``` --- #### 2. 控制逻辑页面 `manger_control.jsp` ```jsp <%@ page contentType="text/html; charset=UTF-8" errorPage="errorhandle.jsp" %> <% String username = request.getParameter("username"); String ageParam = request.getParameter("age"); try { int age = Integer.parseInt(ageParam); if (age > 18) { response.sendRedirect("welcome.jsp?username=" + username); } else { response.sendRedirect("forbid.jsp?username=" + username); } } catch (NumberFormatException e) { request.setAttribute("errorMessage", "年龄必须是数字!"); request.getRequestDispatcher("errorhandle.jsp").forward(request, response); } %> ``` --- #### 3. 欢迎页面 `welcome.jsp` ```jsp <%@ page contentType="text/html; charset=UTF-8" errorPage="errorhandle.jsp" %> <html> <head> <title>欢迎页面</title> </head> <body> <h1>欢迎您,<%= request.getParameter("username") %>!</h1> <p>您已满足访问条件(年龄>18岁)</p> <!-- 友情链接 --> <%@ include file="footer.jsp" %> </body> </html> ``` --- #### 4. 禁止访问页面 `forbid.jsp` ```jsp <%@ page contentType="text/html; charset=UTF-8" errorPage="errorhandle.jsp" %> <html> <head> <title>禁止访问</title> </head> <body> <h1>抱歉,<%= request.getParameter("username") %>!</h1> <p>您未满18岁,禁止访问此内容</p> <!-- 友情链接 --> <%@ include file="footer.jsp" %> </body> </html> ``` --- #### 5. 错误处理页面 `errorhandle.jsp` ```jsp <%@ page contentType="text/html; charset=UTF-8" isErrorPage="true" %> <html> <head> <title>错误处理</title> </head> <body> <h1 style="color:red">系统错误</h1> <p>错误原因:<%= request.getAttribute("errorMessage") %></p> <p>请返回<a href="login.jsp">登录页</a></p> <!-- 友情链接 --> <%@ include file="footer.jsp" %> </body> </html> ``` --- #### 6. 公共友情链接文件 `footer.jsp` ```jsp <hr> <div style="margin-top: 50px"> <h3>友情链接:</h3> <ul> <li><a href="https://www.example.com" target="_blank">示例网站</a></li> <li><a href="https://news.baidu.com" target="_blank">新闻门户</a></li> </ul> </div> ``` --- ### 实现特点说明 1. **跳转逻辑** 通过`manger_control.jsp`解析年龄参数,使用`response.sendRedirect()`实现服务端重定向[^5]。当参数转换失败时,通过`forward`方式跳转到错误页。 2. **统一错误处理** - 所有页面声明`errorPage="errorhandle.jsp"`,当未捕获异常发生时自动跳转[^6] - 手动错误通过`request.setAttribute()`传递错误信息 3. **代码复用** 使用`<%@ include %>`指令实现友情链接的统一维护,避免重复代码[^3] 4. **输入验证** 前端通过`required`属性和`type="number"`进行基础验证,后端通过`Integer.parseInt()`双重验证[^5] --- ### 流程图解 ```mermaid graph TD A[login.jsp] -->|提交表单| B(manger_control.jsp) B --> C{年龄判断} C -->|>18| D[welcome.jsp] C -->|≤18| E[forbid.jsp] B -->|转换失败| F[errorhandle.jsp] D -->|异常| F E -->|异常| F ``` ---
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值