将servlet的响应发送到jsp

时间:2016-01-08 08:11:05

标签: java jsp servlets

我正在尝试将参数从servlet发送到jsp文件。这是servlet:

public class HomepageController extends HttpServlet {

private static final long serialVersionUID = 1L;

    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        CategoryService cs = new CategoryService();

        request.setAttribute("attribute", "Hello world!");
        request.getRequestDispatcher("/views/homepage.jsp").forward(request,response);
    }
}

在jsp中我尝试像这个$ {attribute}一样访问它,但它不起作用。 它显示好像没有定义。

1 个答案:

答案 0 :(得分:0)

通过将版本添加到web.xml文件来解决它。

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

<servlet>
    <servlet-name>HomepageController</servlet-name>
    <servlet-class>controller.HomepageController</servlet-class>
</servlet>
<servlet-mapping>
    <servlet-name>HomepageController</servlet-name>
    <url-pattern>/homepage</url-pattern>
</servlet-mapping>