2016-01-08 212 views
0

我想從servlet發送一個參數到一個jsp文件。這是servlet:發送從servlet到jsp的響應

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}但它不起作用。 它顯示爲沒有定義。

+0

正所謂HomepageController? –

+0

是的,它被稱爲 – Jones

+2

檢查錯別字 –

回答

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>