Pass variables from servlet to jsp
You could set all the values into the response object before forwaring the request to the jsp. Or you can put your values into a session bean and access it in the jsp.
how to send parameter from servlet to jsp page
You can not pass hidden params while using request.sendRedirect. You have following options to pass parameters to the JSP from servlet.
- Set request params in the url itself as
response.sendRedirect(redirectURL + "index.jsp?errorMessage=", dbMessage);
and then in JSP change code to
String errorMsg = request.getParameter("errorMessage")
Error message will be visible in URL on the browser side.
- Use server-side forwarding as mentioned in the answer by Jaydip as shown below.
request.setAttribute("errorMessage", dbMessage);
RequestDispatcher dispatcher = serveltContext().getRequestDispatcher("/index.jsp");
dispatcher.forward(request, response);
Using session
request.getSession().setAttribute("errorMessage", dbMessage);
on the JSP, change code to
String error_msg=(String)request.getSession().getAttribute("errorMessage");
Using cookie
Cookie errorCookie = new Cookie("errorMessage", dbMessage);
errorCookie.setPath(request.getContextPath());
response.addCookie(errorCookie);
On browser side you can read cookie via js or from the request itself
String error_msg = null;
Cookie [] cookies = request.getCookies();
for (Cookie cookie : cookies) {
if ("errorMessage".equals(cookie.getName())) {
error_msg = cookie.getValue();
}
}
Passing variable from servlet to JSP
This might be a typo, $(item)
should be ${item}
in the following -
<c:forEach var="column" items="$(item)" >
Update
http://localhost:8080/DataExtractor/
that doesn't map to the servlet, while http://localhost:8080/DataExtractor/DataGetterServlet
does. If the servlet isn't invoked then it's obvious that data
is not going to be the request. In other words, the first url is not invoking the servlet, but directly talking you to the page. (You probably have as welcome-page in web.xml)
Pass data from Java Servlet to JSP?
You will need to set the data retrieved in the servlet in request scope so that the data is available in JSP
You will have following line in your servlets.
List<Account> accounts = getAccounts();
request.setAttribute("accountList",accounts);
Then in JSP you can access this data using the expression language like below
${accountList}
I would use request dispatches instead of the sendRedirect
as follows
RequestDispatcher rd = sc.getRequestDispatcher(url);
rd.forward(req, res);
If you can use RequestDispatcher
then you can store these values in request
or session
object and get in other JSP.
Is there any specific purpose of using request.sendRedirect
?. If not use RequestDispatcher
.
See this link for more details.
public class AccountServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
List<Account> accounts = getAccountListFromSomewhere();
String url="..."; //relative url for display jsp page
ServletContext sc = getServletContext();
RequestDispatcher rd = sc.getRequestDispatcher(url);
request.setAttribute("accountList", accounts );
rd.forward(request, response);
}
}
Related Topics
Creating an Animated 4X4 Grid in Java
Which Part of Throwing an Exception Is Expensive
Why Does This Gridbaglayout Not Appear as Planned
Stop Scheduled Timer When Shutdown Tomcat
Mapping List in Yaml to List of Objects in Spring Boot
Do/Can Abstract Classes Replace Interfaces
How to Reference a Method in Javadoc
Is There Any Good and Free Date and Time Picker Available for Java Swing
Are There Any Java Method Ordering Conventions
Installing Java on Os X 10.9 (Mavericks)
How to Set a Long Java Classpath in Windows