gpt4 book ai didi

java - 如何将servlet输出包含到jsp文件中

转载 作者:行者123 更新时间:2023-12-01 21:49:09 27 4
gpt4 key购买 nike

在我的网络应用程序中,我有一个包含一些信息的主页。该页面由servlet和相应的jsp文件创建。我的 Web 应用程序中的几乎所有其他页面都必须包含与主页相同的信息以及一些附加信息。我不想要重复的代码,所以我想在其他 jsp 文件中使用主 servlet 的输出。下面是我尝试完成的一个简单示例。

这是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>servlet1</servlet-name>
<servlet-class>app.Servlet1</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>servlet1</servlet-name>
<url-pattern>/servlet1</url-pattern>
</servlet-mapping>

<servlet>
<servlet-name>servlet2</servlet-name>
<servlet-class>app.Servlet2</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>servlet2</servlet-name>
<url-pattern>/servlet2</url-pattern>
</servlet-mapping>

</web-app>

这是java文件:

servlet1.java

package app;


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 Servlet1 extends HttpServlet {

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

request.setAttribute("servletAttribute", 1);

RequestDispatcher view = request.getRequestDispatcher("/servlet1.jsp");
view.forward(request, response);
}
}

servlet2.java

package app;


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 Servlet2 extends HttpServlet {

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

request.setAttribute("servletAttribute", 2);

RequestDispatcher view = request.getRequestDispatcher("/servlet2.jsp");
view.forward(request, response);
}
}

这是jsp文件:

servlet1.jsp

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

servlet1

<%
Integer servletAttribute = (Integer)request.getAttribute("servletAttribute");
out.print("<br>servletAttribute:" + servletAttribute);
%>

servlet2.jsp

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

<jsp:include page="/servlet1" />

servlet2

<%
Integer servletAttribute = (Integer)request.getAttribute("servletAttribute");
out.print("<br>servletAttribute:" + servletAttribute);
%>

因此 servlet2.jsp 必须显示 servlet1 的输出。它显示它,但不显示来自 servlet2 的附加信息。我在日志文件中收到此错误:

org.apache.catalina.core.StandardWrapperValve.invoke Servlet.service() for servlet [servlet2] in context with path [/WebApplication3] threw exception [java.lang.IllegalStateException: Exception occurred when flushing data] with root cause
java.io.IOException: Stream closed

据我了解,出现此错误是因为当 servlet2.jsp 调用“/servlet1”时,servlet1 向客户端发送了响应,而 servlet2.jsp 不再有 session 。

所以我的问题是 - 如何修复我的代码来完成我想要的事情?是否可以将某些 servlet 的输出包含到某些 jsp 文件中?如果可能的话,这是好还是坏做法?

最佳答案

在 servlet2.jsp 中:

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<jsp:include page="/servlet1" />

在 servlet2.jsp 中,您使用了 jsp:include。它包括 servlet1 响应的响应。

但是servlet1,它将把响应转发给另一个jsp。这样就会出现异常。

为了避免这种情况,在 Servlet1 类中应该使用 view.include(request,response); 而不是 view.forward(request, response);

package app;
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 Servlet1 extends HttpServlet {

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

request.setAttribute("servletAttribute", 1);

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

关于java - 如何将servlet输出包含到jsp文件中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35455627/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com