JSP中forward用法

1.首页(填写姓名)(可选,表单post到time.jsp即可):

 

2.判断时间forward到不同页面:

time.jsp:

<%--
    Document   : index
    Created on : 2009-10-3, 15:48:00
    Author     : lucifer
--%>

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
   "http://www.w3.org/TR/html4/loose.dtd">
<%@page import="java.util.Date" %>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>JSP Page</title>
    </head>
    <body>
        <%
               Date dat =
                       new Date();
               if(dat.getHours() <= 12){
                    %>
                    <jsp:forward page="AmGreeting.jsp"/>
         <%}
               else{
                    %>
                    <jsp:forward page="PmGreeting.jsp"/>
                    <%}
         %>
    </body>
</html>

 

3.如果是早上:

AmGreeting.jsp:

<%--
    Document   : AmGreeting
    Created on : 2009-10-3, 16:00:10
    Author     : lucifer
--%>

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
   "http://www.w3.org/TR/html4/loose.dtd">

<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>JSP Page</title>
    </head>
    <body>
        <h1>Good Morning! </h1>
        <%
               String name = request.getParameter("userName");
               out.println(name);
         %>
         !!!
    </body>
</html>

 

 

如果是下午:

PmGreeting.jsp:

<%--
    Document   : AmGreeting
    Created on : 2009-10-3, 16:00:10
    Author     : lucifer
--%>

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
   "http://www.w3.org/TR/html4/loose.dtd">

<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>JSP Page</title>
    </head>
    <body>
        <h1>Good Afternoon! </h1>
        <%
               String name = request.getParameter("userName");
               out.println(name);
         %>
         !!!
    </body>
</html>

JSP,`forward`是用于请求转发的一种机制,它允许将请求从一个JSP页面转发到另一个资源(如另一个JSP页面、Servlet或静态文件),而客户端并不知道这一过程。使用`forward`可以有效地将请求处理和视图生成分离,提高代码的可维护性和灵活性。 ### 使用步骤 1. **创建RequestDispatcher对象**:首先,需要通过`request.getRequestDispatcher(String path)`方法获取一个`RequestDispatcher`对象,这里的`path`参数是要转发到的目标资源的路径。 2. **调用forward方法**:然后,通过`RequestDispatcher`对象的`forward(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response)`方法执行请求转发。这个方法会将当前的请求和响应对象传递给目标资源。 ### 示例 假设你有一个名为`index.jsp`的页面,想要将请求转发到`welcome.jsp`页面,你可以这样做: ```jsp <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Index Page</title> </head> <body> <h1>Welcome to the Index Page</h1> <% // 创建RequestDispatcher对象 RequestDispatcher dispatcher = request.getRequestDispatcher("welcome.jsp"); // 执行请求转发 dispatcher.forward(request, response); %> </body> </html> ``` 在这个例子,当用户访问`index.jsp`时,页面会立即被转发到`welcome.jsp`,用户看到的将是`welcome.jsp`的内容,而URL地址栏显示的仍然是`index.jsp`的地址。 ###
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值