如何从一个servlet调用多个JSP?

时间:2016-05-03 16:02:19

标签: jsp servlets include

我有一个包含4个部分的屏幕:Parm,Header,Detail和Footer。

这将是屏幕的工作流程:用户将在屏幕的Parm部分输入一些数据,然后单击显示按钮。然后需要根据屏幕Parm部分中的选择刷新屏幕的页眉,细节和页脚部分。

所以我有一个JSP Frm.jsp ,里面有4个JSP( Parm.jsp Header.jsp Detail.jsp Footer.jsp )。

<body>
<iframe src="Parm.jsp" name="parmFrame"></iframe>
<iframe src="Header.jsp" name="headerFrame"></iframe>
<iframe src="Detail.jsp" name="detailFrame"></iframe>
<iframe src="Footer.jsp" name="footerFrame"></iframe>
</body>

Parm.jsp 有一个包含一些输入控件的表单,并且有一个“显示”按钮,点击时将表单提交给Servlet ScreenServlet

<form name="parmForm" action="ScreenServlet" method="POST">
<input type="text" name="criteria1">
<input type="text" name="criteria2">
<input type="hidden" name="section" value="parm">
<input type="submit" value="Display">
</form>

因此Servlet从Parm.jsp接收1个请求。根据该请求,我可以检索该Servlet中3个JSP的数据。但是如何从Servlet中调用其他3个JSP( Header.jsp Detail.jsp Footer.jsp )?困惑。

2 个答案:

答案 0 :(得分:0)

来自Servlet电话,

RequestDispatcher.include(请求,响应)

对于Ex,

RequestDispatcher dispatcher1 = request.getRequestDispatcher("jsp1.jsp");
dispatcher.include( request, response ); 

RequestDispatcher dispatcher2 = request.getRequestDispatcher("jsp2.jsp");
dispatcher.include( request, response ); 

RequestDispatcher dispatcher3 = request.getRequestDispatcher("jsp3.jsp");
dispatcher.include( request, response ); 

RequestDispatcher dispatcher4 = request.getRequestDispatcher("jsp4.jsp");
dispatcher.include( request, response ); 

但是,我建议转发到单个jsp并让jsp包含其他jsps。

对于Ex,     RequestDispatcher dispatcher1 = request.getRequestDispatcher(“main.jsp”);     dispatcher.forward(请求,响应);

在main.jsp

<jsp:include file="jsp1.jsp"/>
<jsp:include file="jsp2.jsp"/>
<jsp:include file="jsp3.jsp"/>
<jsp:include file="jsp4.jsp"/>

答案 1 :(得分:0)

<强>回到Home.jsp

<body>
<iframe src="Parm.jsp" name="parmFrame"></iframe>
<iframe src="Header.jsp" name="headerFrame"></iframe>
<iframe src="Detail.jsp" name="detailFrame"></iframe>
<iframe src="Footer.jsp" name="footerFrame"></iframe>
</body>

<强> Parm.jsp

<form name="parmForm" action="ScreenServlet" method="POST">
<input type="text" name="criteria1">
<input type="text" name="criteria2">
<input type="hidden" name="section" value="parm">
<input type="submit" value="Display">
</form>

<强>的web.xml:

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.1" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd">

    <servlet>
        <servlet-name>ScreenServlet</servlet-name>
        <servlet-class>com.my.ScreenServlet</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>ScreenServlet</servlet-name>
        <url-pattern>/ScreenServlet</url-pattern>
    </servlet-mapping> 
</web-app>

<强>的Servlet

package com.my;
import java.io.IOException;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


public class ScreenServlet extends HttpServlet {        

    @Override
    public void doGet(HttpServletRequest request, 
            HttpServletResponse response) 
            throws ServletException, IOException {                      

        RequestDispatcher view = request.getRequestDispatcher("/Home.jsp");      
        view.include(request, response);
    }                                                                               
}

<强> header.jsp中:

<%@page contentType="text/html" pageEncoding="UTF-8"%>

Header Page's Data
<%
    // Write your custom logic by checking the request paramter

   if (request.getParameter("criteria1")!=""){
   // do something as you are required.

   }
%> 

<强> Detail.jsp:

<%@page contentType="text/html" pageEncoding="UTF-8"%>

Details Page's Data:
<%
    // Write your custom logic by checking the request paramter

   if (request.getParameter("criteria1")!=""){
   // do something as you are required.

   }
%> 

<强> footer.jsp中:

<%@page contentType="text/html" pageEncoding="UTF-8"%>

Footer Page's Data:
<%
    // Write your custom logic by checking the request paramter

   if (request.getParameter("criteria1")!=""){
   // do something as you are required.

   }
%>