Pass JSP variable to a java class method - java

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)

Related

Is it possible to create a local page scope in a JSP?

I'm working on a componentization system based on JSPs. This means that parts of the JSP can be moved from one JSP to an other one via drag and drop and leads to a need for a local page scope as variable of a component defined in one JSP my collide in an other JSP.
I can use Servlet 3.0 / JSP 2.2 / JSTL 1.2.
++ Tag File ++
The straight way of doing that would be to create a tag file for a component or something similar as they have this local page scope. But for every change in the tag file it would need to get redeployed and needing to create a tag file needs some time by itself. But if there is no other solution this direction (custom tags, tag files, JSP includes,...) is probably the way to go.
++ Namespace prefixing/suffixing ++
In Portlets one is supposed to concatenate each variable with a namespace provided from the portlet response object. I could do something similar but this can lead to side effects and unexpected behavior. If someone forgets to prefix/suffix the namespace it might work for some time but stops working at an other time without changing the code when the component moved to an other JSP with a conflicting variable name.
++ Custom Tag wrapping ++
I was hoping that I as a component framework developer can wrap the component code of a component developer with a tag file for a component tag like this
<a:component>
<div data-component-id="9">
<c:set var="componentId" value="9"/>
<c:set var="path" value='${abc:getCurrentPath()}_${componentId}'/>
<c:set var="resource" value='${abc:getResourceFromPath(path)}'/>
<c:set var="val" value="${resource.getValue('paragraphValue')"/>
<p><c:out value="${value}"/></p>
</div>
</a:component>
to have the variable in the local page context of the tag file. But instead they are in the context of the surrounding page. I'm looking for something like this
<% { %>
<div data-component-id="9">
<%
String componentId = 9;
String path = obj.getCurrentPath()+componentId;
Resource resource = otherObj.getResourceFromPath(path);
String value = resource.getValue("paragraphValue");
%>
<p><c:out value="<%=value%>"/></p>
</div>
<% } %>
which would create a code block in which variables have their own namespace. But of course for JSTL and JSTL-EL instead of scriptlet code.
I had a look at the JSTL specification but did not find out if there is a way to create such a local page scope. But I didn't check everything as it's huge and I'm not sure if it's possible with custom tags.
It is clear to me that bigger code blocks should not be in the JSP but with the framework I would like to provide simple solutions for smaller components.
++ Changing the JSP code ++
When components are initially placed on a JSP or moved around via drag 'n drop I actually move the JSP code of a component from one JSP to an other or within a JSP. This means I can also programmatically manipulate the JSP code of a component if it doesn't get too complex and it helps solving the problem.
As I thought that custom tag wrapping could be the ideal solution I created an other more specific question and I've got an answer here that solves the problem.
It's simply to remove all pageContext attributes before the evaluation of the body and adding them again at doEndTag().

How to get to application context path without using scriplets in JSP files?

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.

Passing parameters from one jsp file to an included jsp file?

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}

traversing arraylist using EL using foreach

I have a class language having features ID and Name.
I have another class Language_List that has ArrayList<language> lang as a member variable.
In my JSP page I want to access the Name variable of ArrayList<language> lang using EL and For each Loop.
<c:forEach var="language" items="${languages.lang}">
${language}<br>
</c:forEach>
However, it doesn't show ant result and intellisense doesn't work too. Anyone who can help me with this
PS: languages is a Bean variable contain list of languages from DB
I tried this and got this
<b>${languages.lang}</b>
HTML
[sakila.language#f1541c, sakila.language#63c8fabf, sakila.language#1fc644c7, sakila.language#11cd751d, sakila.language#47c3cc0c, sakila.language#7894ca3, sakila.language#47066532, sakila.language#74ddda0b, sakila.language#1116441e, sakila.language#4cd21655, sakila.language#74b84dd9, sakila.language#6fff1d6c, sakila.language#55e4d6e5, sakila.language#22d88071, sakila.language#33d88c96, sakila.language#4df5e671, sakila.language#4aec2cb3, sakila.language#576ac232, sakila.language#76a6dbd7, sakila.language#44ab3d1c, sakila.language#46391c7c, sakila.language#4f7d34e8, sakila.language#251c941d, sakila.language#77400ef3]
The EL doesn't access fields of your objects. It accesses bean properties of your objects. This means that ${languages.lang} is translated to a call to languages.getLang().
If you don't have such a getter, you'll get an exception though. If it just doesn't display anything, it's probably because languages is null, or because its lang list is null or empty. To confirm or infirm those guesses, we need to see the code where you create and populate the bean and its list of languages, and where you store it somewhere to make it accessible from the JSP.
Another possibility is that you forgot to declare the core taglib at the beginning of the JSP. To confirm or infirm that, paste the code of the JSP, and the HTML code generated by the JSP (using View page source in the browser)

Unable to access common content in servlet to jsp call

i have scenario where, i need to call a jsp from a servlet and pass a hashmap containing custom objects whose details jsp needs to display...(this is working fine using Request Dispatcher and jstl in jsp code)
however my jsp includes some other jsps that define the look and feel of the whole application.
my structure is as:
TestProject
<Tabs>- images
<Tabs><Tabs>* background.jpg
<Tabs>- jsp
<Tabs><Tabs> * common
<Tabs><Tabs><Tabs>+ common.jsp
<Tabs><Tabs> * xml
<Tabs><Tabs><Tabs> + XMLDisplay.jsp
my XMLDisplay.jsp includes common.jsp which in turn has path for background.
the common.jsp is common to all other files in application and is working fine.... but not in XMLDisplay.jsp.
i have displayed the context path and the servlet path in XMLDisplay.jsp and its fine. (same as in other files) however for images the path somehow is getting distorted.
and the images are getting accessed from
http:localhost:8080\images\background.jpg
instead of
http:localhost:8080\TestProject\images\background.jpg
the only difference in the other pages is that the call is from one jsp to another while here the call is from servlet to jsp.
Try to use jstl tag <c:url value="\images\background.jpg" var="image1"/> from taglib
<%# taglib uri="http://java.sun.com/jstl/core" prefix="c"%>
You should prefer this tag for resources like (*.js, images, etc.) and for constructing links with parameters.

Categories