Is it possible to pass value of variable from Servlet in to JSP. Say i have a JSP something like welcome.jsp and I have some variable initialized in welcome.java servlet.
When I run welcome.jsp it should take value from welcome.java and display it on page.
When I set the RequestDispatcher and forward it its still going to show the servlet URL.But I want the URL to be JSP file.But the Variable I display in JSP file should be taken from servlet.
You can archive it using session attributes.
.java
request.getSession().setAttribute("attrName", "value");
request is instance of javax.servlet.http.HttpServletRequest
.jsp
${attrName}
To get more about session attributes, read docs. You can set attribute of any type.
request.getSession().setAttribute("attrName", new ArrayList());
You can put the variable in Session and redirect the user to welcome.jsp page.
Related
I want to display something from one jsp page in another jsp page by clicking a button. I did it using request.setAttribute request.getAttribute but it doesn't work for me, for some reason the variable I send is null or the page is blank.
From your original question : When you are doing setAttribute(), its scope is limited to the request when the main page is loading and hence will not be available on the next page as it will be a new request.
<%Object product=ptp;
request.setAttribute("purchase", ptp.getId());
%>
What you can do is, submit this value in URL param as GET or in a form (get/ post) to fetch it on next JSP using request.getParameter().
Or you can use session scope by session.setAttribute()
Hope it helps
you can pass the variables through request scope or session scope.
request.setAttribute("variable name","value of its");
session.setAttribute("variable name","value");
Here a detailed exmple
http://www.jsptut.com/sessions.jsp
I wanted to display a table contents from my database in a jsp page. I know I have to get a list of the rows of that table in the form of objects and use the to iterate through them.
I am trying to avoid scripting in my jsp page. So I wanted to included a servlet in my jsp page which connects to database , takes the result set of the table and add the rows as objects (of the respective class type) and add those objects to a List and set that List object as an attribute to request object.
I tried to use <#include page="/servlet"> in the jsp page .In that servlet I connected to the database and received a result set and added the objects into a list and set attribute to request object.
It gave an error saying this servlet cannot be found.So I added <#include page="${pageContext.request.contextPath}/servlet"> . It showed no error but in that jsp page it showed an exception .How do I solve this?
In your JSP write code as shown below ,
<%#page contentType="text/html" pageEncoding="UTF-8"%>
<jsp:include page="/customServlet" />
& In you servlet doGet method use below code to include response of servlet in jsp,
RequestDispatcher view = request.getRequestDispatcher("/customServletJSP.jsp");
view.include(request, response);
RequestDispatcher interface used to forward or include response of servlet to other servlet or jsp.
I have a Servlet, which sends values to a JSP1. I am setting the values in the servlet as
request.setAttribute();
and i am using
dispatcher.forward(request, response);
and i am able to access these values in the JSP1 using expression language.
Now, I also need to use this in another JSP2.
These two JSP's are clubbed together to display in a single page. So, I need to access the values of the servlet in the second jsp as well. How can i do that?
As you say you have both the jsp clubbed in a single page , you can simply get the attribute as,
${attributeName}
it refers to the object set in the HttpServletRequest scope . I suggest to take the tutorial for Expression language
It is quite simple. If you are setting an attribute "attr1" in a servlet and then forwarding to a JSP (file1.jsp):
request.setAttribute("attr1", "first attr");
request.getRequestDispatcher("file1.jsp").forward(request, response);
And suppose in the first JSP (file1.jsp), you are including another JSP (say file2.jsp):
<body>
.....
<jsp:include page="file2.jsp"></jsp:include>
.....
</body>
Then in file2.jsp, you can access attr1 by calling the request object's getAttribute method:
<body>
....
${attr1}
....
</body>
In case of jsp:include action, when file2.jsp is included in file1.jsp, both request and response objects are passed to file2.jsp as parameters. So file2.jsp can access the attributes of file1.jsp's request object.
i want to use JSP values in servlet. iam fetching some parameters from the url and i need to pass those parameters in my servlet and also the servlet result something by using those parameters and i want to use the result again in the same jsp and in other jsp.
i get those parameters first time when my home jsp is called.
i am getting url parameter by using jstl core tags:
<c:set var="data" scope="session" value="${param.urldata}" />
How do i do this using JSP tags???
Thanks in advance
As i understand somewhat , you are trying to send the values from JSP page to Servlet..
For this we have lot of solution
Pass the value through method
Set the value at session and get it wherever you want in the whole application
I prefer second point, because which hold the value for the entire application
Check Pass variables from servlet to jsp
Above question pass the value from servlet to JSP,Your question is opposite of the answer that's it..!
Set value at JSP : session.setAttribute("key","value");
Get value at Servlet: session.getAttribute("name");
Also refer : Sharing values between servet and JSP
I have a servlet which can get requests by multiple JSP's.
But when I use the RequestDispatcher in the servlet, I don't know how to forward to the JSP that sent the request.
req.getRequestDispatcher("page.jsp").forward(req, resp);
I know there is something like this in html: javascript:javascript:history.go(-1)
I just need something like this:
req.setAttribute("originalRequest", req.getRequestPage());
req.getRequestDispatcher(originalRequest).forward(req, resp);
That piece of code is probably very noob but it gives you the idea of what I need.
So: I need to forward to the page that sent the original request (basically reload the page), but because multiple jsp's use the servlet, I cannot simply forward to "page.jsp"
You can do following
Create a hidden parameter for every jsp named jspName and give value for respective JSPs. e.g. for JSP A, parameter name is jspName and value is a, for JSP B, parameter name is jspName and value is b
Read this hidden parameter in the servlet using following code.
String jspName = request.getParameter("jspName");
RequestDispatcher rd = request.getRequestDispatcher(jspName);
rd.forward(request, response);
When you are calling the servlet from JSP A, then it will have paramter japName=a, when servlet code is running, it will retrieve the value a from request.getParamter("jspName") and a getRequestDispatcher(jspName) will create the dispatcher for the same and rd.forward(request, response) will forward to the jsp.
I have not tried the following, but I hope this may help you to solve your problem.
req.setAttribute("originalRequest", req.getRequestPage());
req.getRequestDispatcher(req.getAttribute(originalRequest).toString()).forward(req, resp);
I have tried in this way, It's working for me...
In JSP
You need to set this in every JSP. For ex: index.jsp is my jsp name.
<% session.setAttribute("jspName","index.jsp"); %>
In Servlet
request.getRequestDispatcher(session.getAttribute("jspName").toString()).forward(req, res);