有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

将数据从java传递到jsp

我正在研究struts,并试图将字符串值从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 

我想知道为什么“s”没有被分配。非常感谢您的帮助


共 (1) 个答案

  1. # 1 楼答案

    而不是

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

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

    s=${s}