I am new to JSP and working with Tomcat 8. The problem is that JSP EL is not working properly. Like when i write ${1>2} it gives the right output but when I put a variable name in it it does not give the output. I have this in my code
<%# page import = "java.util.*" isELIgnored="false"%>
but it is still not working. This is my code:
<%
String GuessErrorMsg = null;
if (GuessErrorMsg != null) {
%>
<div class='bad-field-error-message'>${GuessErrorMsg}</div>
<%
}
%>
i found the problem in my code due which i could not get the EL expresion evaluated.
so i am sharing it with everyone. it may be a very beginner's mistake thats what I am.
the problem was that i have not set the variable using setAtrribute(Expression,variable) .thats why i wasnt getting the value .
Related
I wrote the below code to test the status of msgCode. If the msgCode is not Success it should redirect to error.jsp file. If it is a Success it should stay on the same page . When I ran the code the page always redirects to error.jsp although msgCode is Success. What mistake did I do in my code. Can you please help me if you can. Thank in advance.
<%# page import="com.siebel.SurveyWebService.SurveyTester" %>
<%
SurveyTester tc = new SurveyTester();
tc.getResult();
java.lang.String msgCode = tc.getResult2().getStatusCode();
%>
<%= msgCode%>
<%
if (msgCode.toString() != "Success")
{
response.sendRedirect("error.jsp");
}
%>
First of all, you should use equal method for string comparison. Secondly, even though it's not a problem at the moment but you are creating objects in your jsp and faking the response. tc.getResult2().getStatusCode() is not an actual HTTP response from server.
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 wrote one line of code in my jsp code
User u = (User)request.getSession().getAttribute("user");
and I get this error:
Multiple annotations found at this line:
- User cannot be resolved to a type
Why is this so? What should I look into?
Try adding the following line in your jsp:
<%# page import="....User" %>
Use of scriptlets <% %> in JSP is deprecated now. Use EL as
<p>Welcome, ${user.name}</p>
Check out the el tag page for more info.
I have some javascript
<script>
// some java code that doesn't matter right now
localStorage.setItem("myName", "Bob");
alert(localStorage.myName);
<script>
it works just fine (giving an alert message that says Bob). that's fine and dandy but what I really want is to pass a java variable to a javascript variable and have that print out instead.
But when I put these lines into it...
var hi5 = <%= "getMyName();" %>
localStorage.someName = hi5;
It quits. Any javascript before that works fine. but any javascript after it just doesn't show up.
now the <% %> tags might not be in the exact syntax but it doesn't really give me any errors
I'm sure I'm overlooking something but I'm not sure what it would be. What can I do?
Because look at the source code of the page that this line generates
var hi5 = <%= "getMyName();" %>
It would render something this
var hi5 = BOB
Do you have a variable BOB? No. You are missing the quotes which would make it a string.
var hi5 = "<%= getMyName(); %>";
^ ^^
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?