将数据从java传递给jsp

时间:2014-04-14 12:21:22

标签: java jsp struts

我正在研究struts,我正在尝试将一个String值从java文件传递到jsp页面。但我收到一个空值。请帮忙。

我的java代码:

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

public class myAction extends Action {
    public ActionForward execute(ActionMapping mapping, ActionForm form,
    HttpServletRequest request, HttpServletResponse response)
    throws IOException, ServletException {

        String s="Karthikeyan";

        request.setAttribute("s",s);

        RequestDispatcher reqDispatcher = getRequestDispatcher("first.jsp");
        reqDispatcher.forward(request,response);        

        return (mapping.findForward("success"));
    }
}

我的jsp代码:

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

<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    </head>

    <body>
        Welcome!!!!!!!!

        <%
            String s=(String)request.getAttribute("s");
            out.println("s="+s);
        %>
    </body>
</html>

我的jsp输出:

Welcome!!!!!!!! s=null 

我想知道为什么&#39;没有被分配。非常感谢帮助。

1 个答案:

答案 0 :(得分:0)

而不是做

<%
   String s=(String)request.getAttribute("s");
   out.println("s="+s);
%>

尝试用以下内容替换整个内容:

s=${s}