I have been trying this for a while with no luck.
I want to know if there is a way to include response of a server request (i.e. a Spring controller) inside a JSP.
I am trying to build an independent module which will rely on some specific objects and will print the HTML based on these objects.
So I want to create a Controller method which will take care of dependencies and return this JSP as response.
I can then include a call in the parent JSP so that it hits the controller method and injects the response returned by this method in the parent JSP.
I read somewhere that jsp:include can be used for this purpose as follows:
<jsp:include page="/test-url" flush="true"/>
Where /test-url will map to a Spring controller method.
But when I run this, i get the following exception:
Servlet.service() for servlet dispatcher threw exception: java.lang.IllegalStateException: Cannot forward after response has been committed
Please provide your valuable inputs if you have some idea about this?
use this way
String pPath = yoururl
<jsp:include page="<%=pPath%>" flush="true">
it work fine for me
Related
I have a JSP that's accessed from an url like
http://localhost/products.jsp
(thus without a query string), while that page includes other JSP's with:
<jsp:include page="product.jsp">
<jsp:param value="1" name="recordNumber"/>
</jsp:include>
Inside product.jsp there's a call to a Java method that receives the request object:
NavigationUtils.getProductUrl(request)
That method logic is driven by the request parameters.
What I get is that :
request.getQueryString() returns null
request.getParameterMap() has an entry for "recordNumber"
Is this standard behaviour or am I doing something wrong?
I've looked up the docs about HttpServletRequest.getQueryString() and ServletRequest.getParameterMap(), but I can't find that behaviour described nor any reference to a container-dependent handling that may yield different results.
The main issue is that I may break existing code using getParameterMap() instead of getQueryString(), so any advice on that would help.
The query string is nothing more than a mechanism to encode parameters in a request, but it's not the only one. Typically, that's used when a browser sends a GET request to the server. Another mechanism would be in the body of a form-encoded POST request.
In your case, the JSP wants to include the results of another JSP, which all happens server-side. The servlet container can pass parameters from one JSP to the other without having to encode the parameter on the query string (which would be unnecessarily inefficient).
Using getParameter or getParameterMap is the more general solution. Using getQueryString only really makes sense in specific circumstances when that's eexplicitly what you need to look art.
The servlet redirects from page1.jsp to page2.jsp like this:
String url = getServletContext().getContextPath() + "page2.jsp?command=reload";
response.sendRedirect(url);
Than the filter which try to get "command" parameter can get it. But servlet does not see the parameter.
I also tried to do this without any filters. The result is the same.
What is the reason?
What's your servlet here? The one that you get by compiling page2.jsp as a servlet? If that's the case, make sure you are retrieving the parameter correctly by using:
request["command"]
If your servlet is a real servlet (no JSP page), then you need to map a URL to that servlet and invoke the servlet with that URL mapping.
Make sure you post more info so you will get better help.
Sorry friends if this question is very easy but i am confuse i unable to find out solution.
As we all know in spring MVC framework we create controller which will handle multiple request from same page using #requestmapping annotation.
but same thing i want to do in servlet how can i do ?
Suppose i have a jsp which which will contain a jqgrid,and two forms i want to use only one servlet to load the data into jqgrid and that servlet only will handle request from both the form . Since we have only doGet and doPost in servlet how one servlet fulfill all three request. Hope you understand my question if you have and link where i get sample or and tutorial link plz reply me
Well, the only easy way to do this would be to use a request parameter to control how the processing happens.
In a very basic example, you may have something like a requestType value that gets passed as either part of the query string or the request body. You would assign values of 1-3 (or 0-2) with each value indicating a different type of request. Your servlet would then parse the request accordingly.
This actually is how the DispatcherServlet in SpringMVC works. There's only one servlet class instance and when a request comes in, it examines the query string along with other parts of the request to determine which controller should handle the request.
I've one spring controller which is setting some values to request and shows a jsp page. For the view part we use tiles. The result page has 3 parts, header , content and footer jsp's.
This header jsp use a java file and i want to access the attributes created by the first spring controller from this file. Is there any way to do that without using session?
When I tried request.getAttribute, it gives null. I think it's because it's not an immediate file after the request values setting.
As long as everything runs in the same request and the controller code is executed before the view part, setAttribute() should work. To debug issues like that, use a Filter which dumps the request URL and attributes to the console or the log.
If those calls are in different requests, you have two options: The session and a Spring bean (use a session bean or your own implementation). I prefer beans since they are type safe and they allow me to separate my code from the Servlet API which is complex to test.
You'll really need to put some code to get a code answer but unless you're using JSP scriptlets I'm guessing this is a Java bean that you're using in the header. This of course cannot access the request (hence the session) nor should it really. What you probably want to do is convert it to a tag library if you want it to have access to the request/session.
I was wondering if it was possible to call a controller method using the tag from a JSP page, a bit like the way it's done in JSF.
My Controller
#Controller(value="planesController")
#RequestMapping({"/planes"})
public class PlanesController {
#Autowired
private PlanesDAO planesDAO;
public List<Plane> allPlanes(){
return planesDAO.getAll();
}
My JSP
<sf:form>
<s:eval expression="planesController.allPlanes()" var="planes" />
<sf:checkboxes items="${planes}" path="planes" id="avions"/>
</sf:form>
I keep getting the exception :
org.springframework.expression.spel.SpelEvaluationException: EL1007E:(pos 0): Field or property 'planesController' cannot be found on null
I know I can use model.addAttribute but I call this method from several JSP pages and I thought one of the of the tag was to allow access to beans from views.
I'm using Spring 3.0.5
Thanks in Advance
Letting the view call controller code is just plain wrong, no matter if it works or not. This is MVC backwards.
It would be better if you write a service to do what you want to do and give your view access to that service, but you'd still violate MVC. It would be even better to do all of these calls in your controller in the first place and pass the resulting model to the view without any service calls there.