I have a servlet controller for a jsp file. I pass some parameters to this file when loading it, via request.setAttribute(), from the servlet. However, I need to include another nested jsp from the main jsp file. Will the nested file have access to the params which were sent? If not, how can I pass those params to the nested file?
Yes, the nested jsp will see request scoped attributes set from servlet controller.
use jstl's set method in the parent jsp and use the same variable in jspf.
In parent jsp.
<c:set var="foo" value="bar"/>
In included jspf.
<h2>The included parameter is ${foo}</h2>
Also you can access all the attributes set inside a servlet using request.setAttribute() in the jsp page using EL e.g ${attributeName}
Related
I'm creating a debugging tool that logs output for a specific (and commonly used) jsp tag. It would be helpful if i could also log the name of the jsp page and the line number where the custom tag is called.
Is it possible to get access to the jsp name and line number where a custom tag is called from inside the custom tag?
No, you can not get a line number, where a custom tag is called.
Also you can not get exact JSP name, but via PageContext object, which is available to you in TagSupport, you can get servlet name (which will look like 'index_jsp'):
((Servlet)pageContext.getPage()).getServletConfig().getServletName()
But this is not specified in any specification, so naming scheme differs between JSP containers.
I have a JSP file named as project.jsp. It contains a variable
String context = request.getcontextpath();
which will deliver context path of my server URL.
/ARUBA-LIB-G3245-KITKAT from http://localhost:8080/ARUBA-LIB-G3245-KITKAT/.
Now I want to access this context variable from my project.jsp file to java class which is in jar format file and resides in WEB-INF/lib/AuthenticateDetails.jar.
How can I access this variable from specified java class file?
The same as in java, an import statement and so on.
<%# page import="java.util.Random"
import="org.authdetails.dao.SomeClass" %>
(Or many imports in one import=... with line break in string.
<% new SomeClass(contextPath); %>
Using the MVC (Model-View-Controller) principle, one normally has a servlet (Controller, compilable!) that prepares the data (Model) and puts the data as request attributes, and then forwards to the JSP (view).
In the JSP you can use EL (Expression Language) variables, where some are predefined to access session variables, request parameters and such.
Combining that with JSP tags, one rarely needs to use <% ... %> scriptlets.
Pass the context path variable to the processing method in the library class (library class should be accessible from the jsp though import directive)
So I have a code like this in my jsp file:
<a href="<%= getServletConfig().getServletContext().getContextPath() %>/registerMe.jsp"
class="btn">Not a member? Register..</a>
And I know that using scriplets is bad practice in JSP files. How can I avoid such a situation then?
Use an EL expression:
<a href="${pageContext.servletContext.contextPath}/registerMe.jsp"
class="btn">Not a member? Register..</a>
You can use request.getContextPath() in your action class, and you can pass that to JSP as a string using request, or you can use bean to get that in JSP.
Application scoped objects are stored as attributes of the ServletContext. If the "function call" has access to the ServletContext, then it can just grab them as follows:
Bean bean = (Bean) servletContext.getAttribute("beanname");
I of course expect that the "function" is running in the servlet context. I.e. it is (in)directly executed by a servlet the usual way.
You can also try this link. It have example with full explanation.
I have a framework based on JSP, and I want to move the logic from the JSP in Java, so for that I'm using a POJO with
<jsp:useBean id="myBean" class="myClass" scope="page"/>
<c:set var="myVariable" scope="request" value="${myBean.myGetter}"/>
I have a reference to the request in myClass.
My question is if I can set some attributes on request, do a RequestDispatcher.include to a JSP that will use the attributes I set, when the call gets back, to use some attributes that were put on the request by the included JSP.
I used RequestDispatcher only in servlet/filter, but not on a POJO within a getter.
Yes, if you have reference to request you can do it. Ultimately, the JSP code is translated to Servlet, and calling include in Bean will be as same as calling it from code within servlet.
I have a serveresource method which is invoked on clicking a link. The serveresource method takes the input from the parameter that was passed and retrieves a row from the database. Now all the values in a row are in set using the mutator methods. I have everything in a java object. I need to pass this object to a jsp page to print the values of a single row on the jsp page. I am not sure how to handle that java object in the jsp page instead of setting each value as an attribute in the serveresource method. Need help from experts.. Thanks in Advance
UPDATE
It was because I have an Ajax call and when I set values it is in a completely different life cycle which is causing the problem. I figured it out.
You should defining the java object as Bean in JSP.
The Bean in JSP can be defined using < jsp:useBean..> standard jsp tag. And set and get property using < jsp:setProperty..> and < jsp:getProperty..> standard jsp tags.
Refernces:
Use Bean
Using Beans in JSP. A brief introduction to JSP and Java Beans
UseBean syntax
< jsp:setProperty> and < jsp:getPropety> syntax
The usual method is to add it to the HttpServletRequest object, thus:
MyBean myBean = new MyBean();
myBean.setValue("something);
myBean.setAnotherValue("something else");
// ... stuff ...
request.setAttribute("myBean", MyBean);
This can be accessed from the jsp page using EL thus:
<table>
<tr>
<td>${myBean.value}</td>
<td>${myBean.anotherValue}</td>
</tr>
</table>
you can bind with request object
In Servlet or JSP
request.setAttribute("strIdentifire", yourJavaObject);
In JSP
YourJavaObjectClass obj = (YourJavaObjectClass)request.getAttribute("strIdentifire");