Java 如何将字符串值从一个servlet传递到另一个servlet?
我有一个jsp,我使用'request.getParameter'从中获得servlet1中的字符串值。我想链接servlet1和servlet2,并将我在servlet1中获得的字符串值发送到servlet2 请帮忙Java 如何将字符串值从一个servlet传递到另一个servlet?,java,jsp,servlets,Java,Jsp,Servlets,我有一个jsp,我使用'request.getParameter'从中获得servlet1中的字符串值。我想链接servlet1和servlet2,并将我在servlet1中获得的字符串值发送到servlet2 请帮忙 非常感谢。您需要设置请求属性 在你的服务中 request.setAttribute("attributeName",yourStringVAlue); RequestDispatcher rd = request.getRequestDispatcher("yourServle
非常感谢。您需要设置请求属性 在你的服务中
request.setAttribute("attributeName",yourStringVAlue);
RequestDispatcher rd = request.getRequestDispatcher("yourServletPattern");
rd.forward(request,response);
在你的服务中
String someName = (String)request.getAttribute("attributeName");
在Servlet 1中:
request.setAttribute("myAwesomeAttributeName",myAwesomeAttributeValue);
然后在servlet 2中通过
request.getAttribute("myAwesomeAttributeName");
您可以使用
ServletContext
在Servlet 1中,使用setAttribute
ServletContext servletcontext = getServletContext();
servletcontext.setAttribute("Email", email);
ServletContext servletcontext = getServletContext();
String ReferMail = (String)sc.getAttribute("Email");
在servlet 2中,使用getAttribute
ServletContext servletcontext = getServletContext();
servletcontext.setAttribute("Email", email);
ServletContext servletcontext = getServletContext();
String ReferMail = (String)sc.getAttribute("Email");
将其保留在会话中,然后使用会话检索它。getAttribute()是否可以重定向到servlet2?Plz在这里共享重定向代码。感谢freinds,它正在工作。RequestDispatcher rd=request.getRequestDispatcher(“yourServletPattern”);这里的“yourServletPattern”是什么?如何设置?yourServletPattern是您在web.xml中配置的servlet模式。您的servlet需要在web.xml中配置。有关更多信息,请参阅本教程。糟糕的建议。这只有在web应用程序同时被最多一个用户使用的情况下才能正常工作。我是在本地服务器上学习的。当我直接使用setAttribute和getAttribute时,这些值在我的数据库(MySQL)中以null插入。这就是我使用servletContext的原因。谢谢你的回复。