[英]Passing data from java to jsp
I am working on struts and I am trying to pass a String value from a java file to a jsp page.我正在研究 struts,我试图将一个字符串值从一个 java 文件传递到一个 jsp 页面。 But I am receiving a null value.但我收到一个空值。 Please help.请帮忙。
My java code :我的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"));
}
}
My jsp code :我的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>
My jsp output :我的jsp输出:
Welcome!!!!!!!! s=null
I wanted to know why 's' is not getting assigned.我想知道为什么 's' 没有被分配。 Help would be greatly appreciated.帮助将不胜感激。
Instead of doing而不是做
<%
String s=(String)request.getAttribute("s");
out.println("s="+s);
%>
Try to replace the whole thing with:尝试将整个内容替换为:
s=${s}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.