I use GlassFish 4.1 web profile which as I understand uses EL 3.0. I did everything as was explained here - https://stackoverflow.com/a/3735006/5057736 however my implementation of this solution doesn't work.
This is my constant class
public class CommonKeys {
public static final String TITLE = "SOME_KEY";
}
This is how I set attribute:
request.setAttribute(CommonKeys.TITLE, "TEST");
This is my jsp:
<%#page contentType="text/html" pageEncoding="UTF-8"%>
<%# taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%#page import="org.temp.CommonKeys"%>
<div> Method 1:<%=request.getAttribute(CommonKeys.TITLE)%></div>
<div> Method 2:${requestScope[CommmonKeys.TITLE]}</div>
<div> Method 3:${requestScope["SOME_KEY"]}</div>
This is the output I get
Method 1:TEST
Method 2:
Method 3:TEST
Why does Method 2 not work?
<c:set var="TITLE" value="<%=CommmonKeys.TITLE%>" />
Method 2:${requestScope[TITLE]}
Change your code as per above, should be working fine. It works for me.
Related
Is there any way I could use jstl inside a java function? Basically, what I want is I would just call the connectTodb() function in a jsp page where I need a connection. However, I do not know how.
Can I do this for example?
connectTodo() {
<sql:setDataSource driver="com.mysql.jdbc.Driver"
url="jdbc:mysql://localhost/database_name"
var="localSource"
user="database_user"
password="database_password"/>
}
How do I call this method in my jsp file? Or if I can't do this, how do I connect to the database with just a function? Help me out, thank you!
You can write functions in JSP, but that is generally discouraged. Normally you would create tags and/or fragments. You posted one,
<sql:setDataSource driver="com.mysql.jdbc.Driver"
url="jdbc:mysql://localhost/database_name"
var="localSource"
user="database_user"
password="database_password"/>
That is made available in a jsp with the <%# %> block like
<%# taglib uri="http://java.sun.com/jsp/jstl/sql" prefix="sql"%>
<html>
You could write your own tag file like initDB.tag
<%# taglib prefix="db" tagdir="/WEB-INF/tags" %>
<%# taglib uri="http://java.sun.com/jsp/jstl/sql" prefix="sql"%>
<sql:setDataSource driver="com.mysql.jdbc.Driver"
url="jdbc:mysql://localhost/database_name"
var="localSource"
user="database_user"
password="database_password"/>
and then you can use it like
<db:initDB />
Finally, you might check if your server has a connection pool that you can access from a JNDI name.
I was trying to write a JSP page. But i have read many times that source code within JSP page is a bad practice. So then I tried to write a different class in the same package and call it within that JSP page.
Here's the JSP code:
<jsp:useBean id="link" scope="application" class = "tms.TestJava" />
<%
TestJava t=new TestJava();
t.test();
%>
and here's the class code:
public class TestJava {
public void test() throws IOException
{
System.out.println("sdds");
}
}
I have imported the class into JSP page.
Now the problem is that when I use System.out.println in the class (test method), it gets printed onto the console and I want it to print it to the JSP page. How can I achieve this? Is there a seperate method? Do I have to make the class a servlet?
Thanks!
Try using a tag library: JSTL
Specifically:
<%# taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<html>
<head>
<title><c:out> Tag Example</title>
</head>
<body>
<c:out value="${'<tag> , &'}"/>
</body>
</html>
Or better yet, since you are using JavaBeans already, refactor so that you aren't using System.out() anymore. The idea is that you want to display properties of your bean in your page. Consider: JavaBeans So Do something like this:
Java
public class Course{
private String code = "Math";
public String getCode(){
return code;
}
}
Jsp
<jsp:useBean id="course" class="com.javaBeans.Course" />
<jsp:getProperty name="course" property="code"/>
Chiefly, you don't want to just System.out() to a page. The page should be a view of the data of components on the sever which in this case is the bean.
I think you're going about this the wrong way, but here is a possible solution...
<head>
<%# page language="java" import="tms.TestJava"%>
</head>
<body>
<%=TestJava.getAString()%>
<%=TestJava.getAString()%>
<%=TestJava.getAString()%>
<%=TestJava.getAString()%>
<%=TestJava.getAString()%>
</body>
public String getAString(){
return "<li></li>";
}
If your goal is to build dynamic JSPs, you will probably want to look into JSTL as someone above mentioned, look into defining your own tags, etc. I don't think very much new dev is using scriplet code.
Below is the code I have in index.jsp using jstl 1.2.
<%# taglib prefix = "c" uri="http://java.sun.com/jstl/core"%>
<% String[] setName = {"Hello", "you", "are", "using", "jstl", "in", "jsp"};
request.setAttribute("getName", setName);
%>
<html>
<body>
<table>
<tr><td>Print</td></tr>
<c:forEach var="itemName" items="#{getName}" >
<tr>
<td>${itemName}</td>
</tr>
</c:forEach>
</table>
</body>
</html>
The output I was expecting is as below
Print
Hello
you
are
using
jstl
in
jsp
However below is what I am getting
Print
#{name}
Please let me know where I am missing
Below is the only jar file I have in WEB-INF/lib folder
jstl-1.2.jar
Thanks in advance
Fahim
Note: Adding Java and JSP tag as person who have knowledge of Java and JSP might be knowing JSTL too...
Here,
<%# taglib prefix = "c" uri="http://java.sun.com/jstl/core"%>
You're specifying the wrong JSTL taglib URL. This one is for JSTL 1.0. After JSTL 1.1 it requires a /jsp in the path. See also the JSTL 1.1 tag library documentation.
<%# taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
As to the of the code (and to reply on all those duplicate answers complaining to use ${} instead), the #{} syntax will only work inside JSP when you're targeting a Servlet 2.5 / 2.1 compatible container with a web.xml conforming Servlet 2.5 spec. Tomcat 6.0 is an example of such a container. The #{} will indeed not work in JSP tags on older containers such as Tomcat 5.5 or older.
For clarity and to avoid confusion among starters, better use ${} all the time in JSP tags. Also better use self-documenting variable names.
<%# taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%
String[] names = {"Hello", "you", "are", "using", "jstl", "in", "jsp"};
request.setAttribute("names", names);
%>
<!DOCTYPE html>
<html lang="en">
<head>
<title>JSTL demo</title>
</head>
<body>
<table>
<tr><td>Print</td></tr>
<c:forEach items="${names}" var="name">
<tr><td>${name}</td></tr>
</c:forEach>
</table>
</body>
</html>
See also:
Our JSTL wiki page
Difference between JSP EL, JSF EL and Unified EL
In JSTL 1.2, you don't want to use #{name} in pure JSP, that's only a JSF artifact. Instead, simply use ${name}.
You need to refer items using expression language like ${name}
U r using # instead of $ before name
Let me know if this resolves.
#{name} is not a valid Java variable reference - looks like you are confusing it with JQuery selector.
Anyways try just using items="${name}"
#{name} is should be like ${name}
oh! might be the jars related to JSTL. check thins link for those jars to include in your project
Below is the final code I am using and it is running...
Posting so that someone can use it... Might help me tomorrow ;)
<%# page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<%# taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<% String[] setName = {"Hello", "you", "are", "using", "jstl", "in", "jsp"};
request.setAttribute("getName", setName);
%>
<html>
<body>
<table>
<tr><td>Print</td></tr>
<c:forEach var="itemName" items="#{getName}">
<tr>
<td>${itemName}</td>
</tr>
</c:forEach>
</table>
</body>
</html>
Learning : I was using <%# taglib prefix="c" uri="http://java.sun.com/jstl/core" %> instead of <%# taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
i am trying to include a java code into the value of the inputText in my jsf page but an error occur
according to tld or attribute directive in tag file attribute value
does not accept any expressions
Here is my jsf page.
<%# page contentType="text/html" %>
<%# taglib uri="http://java.sun.com/jsf/html" prefix="html" %>
<%# taglib uri="http://java.sun.com/jsf/core" prefix="core" %>
<%# page language="java" %>
<core:view>
<html:form>
<html:outputLabel value="Informations " style="FONT-SIZE: xx-large;"/>
<br />
<br />
<%
final String property=System.getProperty("jboss.server.home.dir");
%>
<html:outputLabel value="RĂ©pertoire de configuration: " />
<html:inputText value='<%=property%>'/>
</html:form>
</core:view>
Doesn ' t work either with double quote or nothing
How to resolve this problem please ?
Thank you very much
The problem is with this line of code:
<html:inputText value='<%=property%>'/>
JSF uses Expression Language to populate/read values to/from a JavaBean. You will have to create a POJO action (called ManagedBean) with a variable property and link it there.
E.g.
public class ConfigurationAction {
private String property = System.getProperty("jboss.server.home.dir");
/**NOTE: MUST create a getter and setter. **/
public String getProperty() {
return property;
}
public void setProperty(String property) {
this.property = property;
}
}
Don't forget to map the ManagedBean. In JBoss Seam, you will just add an #Name annotation above the class like, #Name("configurationAction").
Finally, render this in JSF with Expression Language (EL)
<html:inputText value="#{configurationAction.property}"/>
Where configurationAction is the name of your ManagedBean, and property is the instance of the ManagedBean.
my model returns an arraylist of strings to servlet in the form
ArrayList<String> currentCustomer = model.getAllCustomers();
i want to pass this arraylist from the servlet to the jsp page. how do i do this? below is what i tried
req.setAttribute("currentCustomer", currentCustomer);
and in the jsp page, i want to use JSTL to loop over each value and display it. how do i do that? its frustrating me to no end. ive scoured the web but to no avail. any help is greatly appreciated.
here is the jsp code
<%# taglib uri="http://java.sun.com/jstl/core" prefix="c" %>
<body>
<div>
<c:forEach var="customer" items="currentCustomer">
${customer}
</c:forEach>
</div>
</body>
Let's make it work :)
<%# taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
...
<c:forEach var="customer" items="${currentCustomer}">
<c:out value="${customer.name}" />
<c:out value="${customer.age}" />
</c:forEach>
P.S. jsp:useBean is another way to go...
P.P.S. I also made a correction in the taglib import. That's one of these hard-visible mistakes when you can look on two different entries and think they are the same :)
its allrite guys, i solved the problem.. thanks for your help..
apparently the code i was using was outdated (thanks internet!) i was writing this on the header:
<%# taglib uri="http://java.sun.com/jstl/core" prefix="c" %>
while it should have been
<%# taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
It will be smt like
<c:forEach var="currentCustomer" items="${customers}">
${currentCustomer.name}
${currentCustomer.age}
</c:forEach>