Pass parameters to jsp by <%# include file - java

I need to pass params to included jsp
<%# include file="second.jsp" %>
i need to use it several times in one jsp page, so i need to pass id.
I can't use jsp:include because jsp can't see struts action in this way
Can u help me? Thank you

Set them as attributes on the request object (which makes them visible to the ${var} syntax) before invoking your include.
See How to access a request attribute set by a servlet in JSP? for a similar question.

Related

JSP onload call servlet without scriptlets

I want that when my JSP is loaded to call a servlet. I have this link:
Homepage
But this way I have to click the link, I want to perform the calling automatically when the jsp is loaded.
On the other side I need to use no scriptlets. Does anyone have any idea how to do this?
Although its marked as resolved but I am editing my answer for future reference:
Apart from the javascript solution you can accomplish this with 2 more options using jsp tags:
Option1:
You can forward the request to the corresponding servlet.
Use jsp standard action jsp:forward, e.g.:
<jsp:forward page="ContentServlet?action=userContents" >
</jsp:forward>
You can replace your link with the above tag and the servlet will be called.
Option2:
you can redirect the request to your servlet using JSTL tags:
<c:redirect url="ContentServlet?action=userContents" />
Again you can replace your link with the above tag.
In Option1 browser's url will not change.
In Option 2 browser's url will change to "ContentServlet?action=userContents"
Hope it solves your problem.
Why don't you use JavaScript?
<script type="text/javascript">
function redirect(){
window.location = "/ContentServlet?action=userContents"
}
</script>
...
<body onLoad="redirect()">

How to i get original page parameters from jsp errorpage?

I have an jsp/servlet webapp on tompcat and i need something like a crash report each time an unexpected error accords
I have an error page defined and added with errorpage directive
<%#page errorPage="./erropage.jsp" %>
to my edit.jsp file(just an example).
The request to edit.jsp is made with post request (actually is an ajax request but this is not so important).
I need a solution to read original parameters (sent to edit.jsp page) from errorpage in order to buid a crash report.
request.getAttribute("javax.servlet.error.request_uri")
Doed not help me since this will include the actual url (get parameters).
Also, to build up a string from requested parameters in edit.jsp and set that string to session is not an option since there are to many files in witch i need to implement this.
Actually it seams like
request.getParamter()
in errorpage.jsp give the parameter from edit.jsp request!
I have a similar issue with cookies and I post a question about it.
Now, I believe ( I have some checks to do) that the error mechanism is using REDIRECT scheme (not FORWARD) so a new request object is created.. You can verify this assumption

Spring - Template page based on many JSP pages

How can I build an template page, based on many JSP pages with Spring?
I can not use tag, because I have to set some data to those pages first. I also do not want to implement whole template page ( header, footer ) in every JSP file because only ${content} var will be changing.
Thanks in advance
Use a jsp fragment within your jsp:
<%#include file="header.jspf" %>

How to pass a parameter to previous page?

I am working on a JSP project. I have a page that calls another JSP.
Now the problem is, how to pass or use a variable in the called JSP page in its calling page?
You can save the variable in the HTTPSession or ServletContext object.
And in the calling JSP page, use or check session attribute for the variable.
session.setAttribute(objectId, Object); to set the variable.
session.getAttribute(objectId); to get the variable.
I have a page that calls another jsp
page
The problem lies here in this sentence.
Try to follow MVC. Use JSP just for rendering the View, and Servlet as a Controller.
Here, simple.souther.us, you find simple and awesome tutorials for newbies.
Your design should be
take one input param on page1.jsp
post it to some servlet , process it there, forward request to page1 jsp and pass param taken from page 1 as attribute
See Also
why-business-logic-should-be-moved-out-of-jsp ?

Call from one JSP file to another JSP

I need to call from one JSP to another, do some stuff over there..
I am inside the caller jsp, inside his handleReqeust(HttpServletReqeust request)
method I am trying to forward the request to another JSP, to call the other JSP file to his handleRequest(HttpServletReqeust request) off course with the request object
I tried it:
RequestDispatcher dispatcher = request.getRequestDispatcher("/theSecondJspFile.jsp");
if (dispatcher != null)
dispatcher.forward(request, response);
but to make it work I need for it the object response, but I don't have it,
I am sure I missed something basic, but what?
From my question you can see, I don't have solid backround in java, so please correct me, or refer me to good guide if you feel it necessary
Thanks.
-------------------Edit--------------------------------------
I don't need a redirect, I just want to call another JSP file to his handleRequest method
I think it relate to HTML
What I can make out of your question is that you need to redirect to a secondpage.html from firstpage.html with some data. You can use both GET or POST method to send the data.
For the GET method just redirect to secondpage,html?data=value. The data will be available in the HttpRequest parameter in the controller of the secondpage.html where it can be used as required.
For the POST method you would need to post the data (using a form on firstpage.html) to secondpage.html. In the controller of the secondpage.html the data should be available in a similar way as before.
To include another JSP in a jsp :
<jsp:include page="foo.jsp"/>
You can find some reference material here.
I can not be sure, but what I think you are trying to do is to include a jsp page on to your jsp page and use the the objects and other variables declared in the first jsp page in your second.
<%# include file="mypage.jsp" %> should help you do this.
If this is not what you are looking for, please make your question tad bit more clear. some code will help really.
JSPs are supposed to be used to present the final result. JSPs are not supposed to be part of business tasks leading to this result. There you use normal Java classes for, starting with a servlet class. Let the HTTP request (the one which you enter in browser address bar or specify in some HTML link or form) point to the URL of the servlet instead. This way you can write Java code in the servlet the usual way to invoke other Java classes/methods and finally forward the request to a certain JSP file based on the outcome of the result and then just let that JSP present the final result.
To start with servlets, I'd suggest to read our Servlets info page.
I just added empty Iframe, and set his URL when I needed to call him

Categories