hi I am new to java and I am creating a jsp and I am using scriplet code. I would like to be able to have my object to not display on the screen when it is empty.
<% if (webApp.getInfo != null) { %> <h6><%=webApp.getInfo()%> <% } %>
Currently it is checking if the object is null but it is still displaying a line on the page when I run the JSP. How can I check to see if webApp.getInfo is empty and not have a line display or test display for those headers?
<c:if test="${!empty str}">
<h6>...</h6>
</c:if>
Note that:
this is meaningful only for strings, not for any object
this is JSTL, which is preferred to scriptlets (from your example)
your objects needs to be set as request attribute.
you only need this if you have additional tags inside the if-clause. If it is only the string that you want to output, there is no need to check - as BalusC noted this is not displayed.
you can trim whitepsaces from the jsp output using configurations provided by your servlet container, or if using servlet 3.0 - via <trim-directive-whitespaces> in web.xml
Related
I was working through a null pointer exception on code like the following:
<%
SessionData session = getSessionData(request);
Webpage webPage = null;
if (session!= null) {
webPage = session.getWebPage();
}
%>
<script type="text/javascript">
//NullPointer happens here, webPage is null when the session is lost
<tags:ComboBox
comboBox="<%=webPage.getComboBox()%>" />
</script>
I was surprised when I could move the ending of if (session!=null to after the javascript, which seems to ignore that code when the session was null.
<%
SessionData session = getSessionData(request);
Webpage webPage = null;
if (session!= null) {
webPage = session.getWebPage();
//} move this to below
%>
<script type="text/javascript">
//NullPointer happens here, webPage is null when the session is lost
<tags:ComboBox
comboBox="<%=webPage.getComboBox()%>" />
</script>
<% } %> //moved to here
Does the scriptlet for the ComboBox tag, inside the brackets, no longer run? I would think it would still try to get the combobox off the webpage, and still end up getting a null pointer. Am I incorrect in thinking that scriptlets all get their values before the code is actually ran?
(just thought I'd mention, there is an included script which redirects the page if there is no session. I get a NullPointer with the first section of code, and correctly redirect with the second section)
A JSP is compiled to a servlet on-the-fly by the servlet container.
This compilation is actually simple kind of inversion:
TEXT1
<% java code %>
TEXT2
<%= java expression %>
TEXT3
That is compiled to:
out.print("TEXT1");
java code
out.print("TEXT2");
out.print(java expression);
out.print("TEXT3");
So when you say:
TEXT1
<% if (true) { %>
TEXT2
<% } %>
TEXT3
You get:
out.print("TEXT1");
if (true) {
out.print("TEXT2");
}
out.print("TEXT3");
The above examples are minified for clarity, e.g. newlines are ignored, the boilerplate servlet setup is not included, and the complexity of tag library execution is not covered.
In short, you are incorrect as to the order in which tag libraries and scriptlets are processed; the JSP compiler first identifies JSP directives, then resolves and renders tag library output, and then converts everything not in a scriptlet into a bunch of static strings written to the page, before stitching the resulting Java file together around the existing scriptlet code, looking something like this:
// start of class and _jspService method declaration omitted for brevity
out.write("<html>\n");
out.write("\t<head>\n");
out.write("\t<title>Example Static HTML</title>\n");
// comment inside a scriptlet block
int x = request.getParameter("x");
pageContext.setParameter("x", x);
out.write("\t</head>\n");
The problem here stems from the fact that Tag Libraries are resolved first, and the code which isolates and evaluates them doesn't care about either scriptlet blocks or the DOM. In your case, the <tags:ComboBox> tag just thinks the scriptlet is a regular string.
What you should be doing instead is exposing the value in your scriptlet to the accessible scope used by the tag library; in the case of JSTL, for example, you need to add it into the page context via pageContext.setAttribute("varName", value).
Check this answer for more details.
I have a jsp page namely User_Ref.jsp whcih has a datepicker .When I click on submit on that page ,it is redirected to another jsp page namely ref_time_current.jsp.In this jsp page I have used a scriptlet to hold the value which was selected by user from the calendar i.e datepicker. The scriptlet is
<%
Ref_log_current obj = new Ref_log_current();
String str= request.getParameter("datepicker");
ref.refarray_vac1(str);
%>
Now I want to use this str variable defined in scriptlet in this way in same jsp page-<c:out value="${ref.refarray_vac1(str)}"></c:out>
But When I execute this,refarray_vac1(String Date) method which return list is showing an empty list.I think I'm using the str variable in wrong way.Please correct me.
JSTL has only access to scoped variable, not directly to scriplet ones. But you can easily create a page variable that way :
<%
Ref_log_current obj = new Ref_log_current();
String str= request.getParameter("datepicker");
pageContext.setAttribute("str", str); // store str in page scope under name str
%>
You can then safely access ${str} in the JSP file.
In JSTL is not possible to use scriptlet variable in expression. Also you don't need to use scriptlet.
You need to import the bean class you create in JSP
<%# page import="com.beans.Ref_log_current" %>
You can access parameters like this
<jsp:useBean id="ref" class="com.beans.Ref_log_current" scope="page"/>
<c:out value="${ref.refarray_vac1(param.datepicker)}"/>
I've been trying to write a website in which all navigation is handled by hiding and showing divs. It is my understanding that this method is called Single Page Interface. This has worked for simple designs in the past but my current task is starting to become very troublesome using this method. How would I go about replicating the same behavior but instead of hiding and showing divs I can just have a main container div that is then populated with the desired html from the server?
Example:
<script>
$("#button").onclick(function() {
$("#a").show();
$("#b").hide();
});
</script>
<html>
<body>
<div id="a" style="display:none;">A: SOME HTML</div>
<div id="b" style="display:block;">B: SOME HTML</div>
<button id="button">Change to A</button>
</body>
</html>
(note this is a very rough example of white I'm trying to do)
But I would like the contents of a container div to change from "B" to "A" via some jsp
Could anybody point in the correct direction?
Further Explanation:
Maybe I can clarify a little better. So when the user loads the page they are presented with a section that has a table of all the existing files in a database. The user can select a file from the DB list to rename or copy. If the user wishes to rename a file, for example, they would be presented with a new display (all within the same "Tab") which will have a set of fields populated for the file that they have selected and a set of empty fields in which they can specify the new file name. Currently this changing of displays is handled by showing and hiding divs, but I would like to retrieve the html that I want to display from the server and present it. Basically mimicking the hiding and showing of divs.
As it's not completely clear to me what you're trying to do I'll give you some options:
Replace the content of a element on your page see
Since you're using a JSP, you can use server side logic to display certain fragments
You're using a JSP, use that to render some server side content
Ad 1:
(assuming jQuery) $('body').load('serverSide.html'); see http://api.jquery.com/load/
Ad 2:
<% if ("a".equals(request.getParameter("aOrB"))) { %>
<jsp:include page="/a.jspf">
<% } else { %>
<jsp:include page="/b.jspf">
<% } %}
Ad 3:
<%= request.getAttribute('content') %>
Hope that helps
Could you please help me in displaying message after certain work done like 1 record deleted after
deleting data from database.
In JFrame there is a method like setVisible(true), similarly is there any way in JSP?
JSP is just a HTML code generator. All you need to do is to make sure that it generates HTML the way you want. You can use JSTL core tags to control the flow of HTML code generation.
For example, assuming that you've a servlet which sets a message like follows,
request.setAttribute("message", "Record successfully deleted");
then you can conditionally display it in JSP as follows with help of JSTL <c:if>.
<c:if test="${not empty message}">
<span class="message">${message}</span>
</c:if>
See also:
Our Servlets wiki page - contains Hello World example with similar validation/messaging approach
Our JSTL wiki page - contains info how to install and use JSTL
I want to dispaly a result parameter which return from servlet to jsp after an event happen.
so when i add new user for example a result parameter returns with value that the user has been added successfully , and i want to display the result in jsp page.
i make like this in jsp page
<%String result = String.valueOf(request.getAttribute("result"));%>
<%if (result != null){%>
<%= result%>
<%}%>
THE PROBLEM is that every time i open the page for the first time result prints as null to the browser even i write if not null print result value, where is the problem ? can someone help/
String.valueOf(..) returns "null" (a String with length 4) if the argument is null. So your quick solution would be not to use String.valueOf(..) at all.
However, it is not advisable to use scriptlets and java code like that in your JSPs. Use JSTL and EL instead. Your code would look like this:
<c:if test="${result != null}">
${result}
</c:if>