HTML not passing validation - java

The below line is not passing validation in my application. The error is in Netbeans is...
Bad value " /content/edit" for attribute href on element "a": WHITESPACE in PATH
Add Content
The runtime error is:
org.apache.jasper.JasperException: /base.jsp(9,25) PWC6213: quote symbol expected
I am passing an attribute for this value. Why am I getting this error when I pass a value?

Don't use scriptlets in JSP. Use the JSP EL:
Add Content

Add Content
Use single quotes with urlPrefix. It should work.

Try this:
<% String urlPrefix = (String)request.getAttribute("urlPrefix"); %>
Add Content
or better this:
<%
String urlPrefix = (String)request.getAttribute("urlPrefix");
String url = urlPrefix + "/content/edit";
%>
Add Content
or even better use EL:
Add Content
It's worth mentioning the protection against XSS attacks as Asaph pointed out in his comment:
Add Content
might do the trick if you include
<%# taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn" %>
at the top of your JSP.

I've just done a simple test and the following line has no syntax error and runs without throwing an exception whether the urlPrefix attribute is set or not:
Add Content
There is no syntax error at all. In the case of there being no urlPrefix attribute set, the resulting html is:
Add Content
In the case of urlPrefix being equal to http://example.com, the resulting html is:
Add Content
Here is a quick little standalone test.jsp file to demonstrate:
<% request.setAttribute("urlPrefix", "http://example.com"); %>
Add Content
You can remove the first line to test the null case.
So we've demonstrated that the line you posted as the alleged offending line is not actually problematic. Some possibilities:
Are you sure you're looking at the correct line?
Are you sure you're looking at the correct file?
Are you sure you've deployed your application?
Are you sure you're looking at the correct url/environment?

Related

How to print JSON response from JAVA method to HTML Table?

I have a jsp code, where I fetch some JSON data from JAVA Class file. [Basically openfire users]
Now I get the data successfully, but I want to show this data in HTML table format.
How do I Do this ?
My JSP Code :
<%# page language="java" import="prov.*, java.util.*, java.io.*,java.text.*" contentType="text/html"%>
<%# page errorPage="error.jsp" %>
<%
Openfire tc = new Openfire();
tc.getUsers("192.168.50.218","epvFjHq5RHA614C7");
out.println("Data Is As Below : " + tc.getUsers("192.168.50.218","epvFjHq5RHA614C7"));
%>
And I get Response from the JAVA Class method like this :
[{"username":"abcd","name":"","properties":null},{"username":"admin","email":"admin#example.com","name":"Administrator","properties":null},{"username":"bizdd456d454mnc","email":"bizMNC#bizrtc.com","name":"bidzMNC","properties":null},{"username":"bizddd454mnc","email":"bizMNC#bizrtc.com","name":"bidzMNC","properties":null},{"username":"bizmnc","email":"admin#example.com","name":"511515151515151","properties":{"property":[{"#key":"console.order","#value":"session-summary=1"},{"#key":"console.rows_per_page","#value":"user-summary=8"}]}},{"username":"dhaval","email":"dhaval#bizrtc.com","name":"dhaval","properties":null},{"username":"keyur","email":"keyur#bizrtc.com","name":"keyur","properties":null},{"username":"minz","email":"bizMNC#bizrtc.com","name":"bidzMNC","properties":null},{"username":"patel","email":"rau#example.com","name":"patelbhai","properties":{"property":[{"#key":"console.order","#value":"session-summary=1"},{"#key":"console.rows_per_page","#value":"user-summary=8"}]}},{"username":"rajan","email":"rajan#bizrtc.com","name":"rajan","properties":null},{"username":"+username+","email":"+email+","name":"+name+","properties":null}]
As I am very new to JAVA and JSP I don't know how to parse this data to HTML Table.
So Please help.
You can see here how to do it. You can populate it in Javasript or jQuery, but it is better to use JSTL and not just call java code inside JSPs.
I would suggest you use mustache als template engine.
It allows you to use a HTML fragment as template (store it as resource) where double curly brackets (hence the name Mustache) denote the insertion points.
The full documentation of the Mustache syntax is here and a Java example here. Let us know how it is going.

When do (jsp) scriptlets run their (Java) code?

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.

is it possible to set page content type on a condition in jsp or to set different content type for a single jsp

I want to display JSON & XML using a single jsp page.
at a time only one attribute will come from the java class.
My code something look like this.
<%
String json = (String) request.getAttribute("userRequestedJsonById");
if (!StringUtility.isNullOrEmpty(json)) {%>
<%=json%>
<% } else { %>
<%
String xml = (String) request.getAttribute("searcherRespondedXmlById");
if(!StringUtility.isNullOrEmpty(xml)) {%>
<%#page contentType="text/xml"%>
<%=xml%>
<%}%>
<%}%>
I am having a plugin called JSONVIEW to display the json properly.which doesn't work if it finds content type xml.
Content type is set only on the condition,jsp is including this content type even condition is not satisfied.
I don't know much how jsp set content type works,is there any other way to do this or to restrict to set content type xml on a particular condition.
Thanks.
Setting the content type needs to be done before printing anything out, so you need to get rid of the pointless opening and closing of tags that causes whitespace to be printed. Then you will use response.setContentType():
<%
String json = (String) request.getAttribute("userRequestedJsonById");
if (!StringUtility.isNullOrEmpty(json))
{
response.setContentType("application/json");
out.print(json);
}
else
{
String xml = (String) request.getAttribute("searcherRespondedXmlById");
if(!StringUtility.isNullOrEmpty(xml))
{
response.setContentType("text/xml");
out.print(xml);
}
}
%>
Its also just cleaner if you're going to use Scriptlets to just keep your code block open and use out.print() rather than opening, closing, and then <%=var%>, and opening again. That's just so unreadable.

Get url minus current filename in JSP or CQ5

I wish to get the current url minus the file name that is being currently referenced. Whether the solution is in JSP or CQ5 doesn't matter. However, I am trying to use the latter more to get used to it.
I'm using this documentation but it's not helping. CQ5 Docs.
The example I found retrieves the full current path, but I don't know how to strip the file name from it:
<% Page containingPage = pageManager.getContainingPage(resourceResolver.getResource(currentNode.getPath()));
%>
Profile
Assuming you are accessing the following resource URL.
/content/mywebsite/english/mynode
if your current node is "mynode" and you want to get the part of url without your current node.
then the simplest way to do is, call getParent() on currentNode(mynode)
therefore, you can get the path of your parent like this.
currentNode.getParent().getPath() will give you "/content/mywebsite/english/"
full code :
<% Page containingPage = pageManager.getContainingPage(resourceResolver.getResource(currentNode.getParent().getPath()));
%>
Profile
A much simpler approach.
You can use the currentPage object to get the parent Page.
The code looks like this
Profile
In case you are getting an error while using this code, check whether you have included the global.jsp file in the page. The one shown below.
<%#include file="/libs/foundation/global.jsp"%>
I don't know anything about CQ5, but since getPath() returns an ordinary Java string I expect you could just take the prefix up to the last slash, which for a string s can be done with s.substring(0, s.lastIndexOf('/')+1). If you have to make it into a one-liner, you could do containingPage.getPath().substring(0, containingPage.getPath().lastIndexOf('/')+1).

Arabic characters appears like ??? after adding a Filter to JSP page

When I add a Filter to a particular JSP file, the Arabic characters in the output appears like ???, even when the page encoding is been set to UTF-8 by <% #page pageEncoding="UTF-8"%> and <% response.setCharacterEncoding("UTF-8");%>.
The strange thing is, before I added the Filter, the output of all Arabic pages appears with correct encoding. Can someone tell how this problem is caused and how I can solve it?
The filter is either directly or indirectly commiting the response and/or accessing the Writer or OutputStream of the HttpServletResponse which causes that the encoding cannot be changed anymore in the JSP. Fix the code in the filter accordingly. The filter should in any way not be writing anything to the response body. There the JSP (for HTML) or Servlet (for other content) is for.
By the way, you don't need to call <% response.setCharacterEncoding("UTF-8");%>. The <%#page pageEncoding="UTF-8"%> already implicitly does that.

Categories