问题
I am developing a website with servlets and JSP.I want to know how to share data between a servlet and JSP as while searching I noticed that the getSession() method has been deprecated and currently I cannot add data to session for sharing the data.Please someone suggest me how should I share data between the servlets and JSP.
回答1:
We can apply two ways most probably
1 ) request
2 ) session
Examples :
request.setAttribute("userName", userName );
session.setAttribute("userName", userName );
回答2:
please follow the below references.This will help ful to you
how-do-i-share-data-between-a-jsp-page-and-servlet
回答3:
in your body method doGet
do something like that,
String value = "par1"
request.setAttribute("parameter", value);
your jsp file:
<%
String value= (String) request.getAttribute("parameter");
out.println(value);
%>
来源:https://stackoverflow.com/questions/60518878/how-to-share-data-between-a-jsp-and-servlet