Im sending data from login.java to stu.jsp
below is the login.java code
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
boolean message = true;
request.setAttribute("message", message); // This will be available as ${message}
request.getRequestDispatcher("stu.jsp").forward(request, response);
}
[this is the stu.jsp code (showing error)] Please click on the image to view the code with error
below is just the same code as above in the image which is showing an error in the if condition in EL
<% if ( ${message} ) { %>
<p> Today is weekend</p>
<% } else { %>
<p> Today is not weekend</p>
<% } %>
Below is the complete error message when i run the server
HTTP Status 500 – Internal Server Error
Type Exception Report
Message Unable to compile class for JSP:
Description The server encountered an unexpected condition that prevented it from fulfilling the request.
Exception
org.apache.jasper.JasperException: Unable to compile class for JSP:
An error occurred at line: [89] in the jsp file: [/stu.jsp]
Syntax error, insert ") Statement" to complete IfStatement
86:
87:
88:
89: <% if ( ${message} ) { %>
90: <p> Today is weekend</p>
91: <% } else { %>
92: <p> Today is not weekend</p>
An error occurred at line: [89] in the jsp file: [/stu.jsp]
$ cannot be resolved to a variable
86:
87:
88:
89: <% if ( ${message} ) { %>
90: <p> Today is weekend</p>
91: <% } else { %>
92: <p> Today is not weekend</p>
An error occurred at line: [89] in the jsp file: [/stu.jsp]
Syntax error on tokens, delete these tokens
86:
87:
88:
89: <% if ( ${message} ) { %>
90: <p> Today is weekend</p>
91: <% } else { %>
92: <p> Today is not weekend</p>
An error occurred at line: [91] in the jsp file: [/stu.jsp]
Syntax error on token "else", delete this token
88:
89: <% if ( ${message} ) { %>
90: <p> Today is weekend</p>
91: <% } else { %>
92: <p> Today is not weekend</p>
93: <% } %>
94:
An error occurred at line: [647] in the generated java file: [D:\Browser Downloads\setup -2\eclipse\TOMCAT\apache-tomcat-9.0.54\work\Catalina\localhost\PPP\org\apache\jsp\stu_jsp.java]
Syntax error, insert "}" to complete Block
Stacktrace:
org.apache.jasper.compiler.DefaultErrorHandler.javacError(DefaultErrorHandler.java:102)
org.apache.jasper.compiler.ErrorDispatcher.javacError(ErrorDispatcher.java:213)
org.apache.jasper.compiler.JDTCompiler.generateClass(JDTCompiler.java:487)
org.apache.jasper.compiler.Compiler.compile(Compiler.java:397)
org.apache.jasper.compiler.Compiler.compile(Compiler.java:367)
org.apache.jasper.compiler.Compiler.compile(Compiler.java:351)
org.apache.jasper.JspCompilationContext.compile(JspCompilationContext.java:605)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:399)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:379)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:327)
javax.servlet.http.HttpServlet.service(HttpServlet.java:764)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)
pack.login.doPost(login.java:28)
javax.servlet.http.HttpServlet.service(HttpServlet.java:681)
javax.servlet.http.HttpServlet.service(HttpServlet.java:764)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)
Note The full stack trace of the root cause is available in the server logs.
Please help me in resolving this issue Thank you in advance
CodePudding user response:
message
is an attribute in JSP and everything inside <%... %>
is java code.
Inside Java code, you cannot use ${}
as you can only use Java syntax.
Knowing this, you have two possibilities:
Either access message
using request.getAttribute()
inside <% %>
:
<% if ((Boolean)request.getAttribute("message")) { %>
<p> Today is weekend</p>
<% } else { %>
<p> Today is not weekend</p>
<% } %>
Or you could use c:if
:
<c:if test="${message}">
<p> Today is weekend</p>
</c:if>
<c:if test="${! message}">
<p> Today is not weekend</p>
</c:if>