This question already has answers here:
How do you import classes in JSP?
(6 answers)
java.lang.IllegalArgumentException: Page directive: invalid value for import
(1 answer)
Closed 1 year ago.
I'm facing this kind of error only today. Before that, I have run many times successfully. Please help me to find out the error. The error and code are given below.
I am working on a JSP project. While running the project using Eclipse with Tomcat 9.0 server, I got the following exception,
Message org.apache.jasper.JasperException: 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: org.apache.jasper.JasperException: Unable to compile class for JSP
org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:605)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:423)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:385)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:329)
javax.servlet.http.HttpServlet.service(HttpServlet.java:733)
servlet.userlogin.doPost(userlogin.java:32)
javax.servlet.http.HttpServlet.service(HttpServlet.java:652)
javax.servlet.http.HttpServlet.service(HttpServlet.java:733)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)
Root Cause
org.apache.jasper.JasperException: Unable to compile class for JSP
org.apache.jasper.JspCompilationContext.compile(JspCompilationContext.java:621)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:400)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:385)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:329)
javax.servlet.http.HttpServlet.service(HttpServlet.java:733)
servlet.userlogin.doPost(userlogin.java:32)
javax.servlet.http.HttpServlet.service(HttpServlet.java:652)
javax.servlet.http.HttpServlet.service(HttpServlet.java:733)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)
Root Cause
java.lang.IllegalArgumentException: Page directive: invalid value for import
org.apache.jasper.compiler.Node$PageDirective.validateImport(Node.java:619)
org.apache.jasper.compiler.Node$PageDirective.addImport(Node.java:599)
org.apache.jasper.compiler.Parser.parsePageDirective(Parser.java:366)
org.apache.jasper.compiler.Parser.parseDirective(Parser.java:478)
org.apache.jasper.compiler.Parser.parseFileDirectives(Parser.java:1797)
org.apache.jasper.compiler.Parser.parse(Parser.java:141)
org.apache.jasper.compiler.ParserController.doParse(ParserController.java:244)
org.apache.jasper.compiler.ParserController.parseDirectives(ParserController.java:127)
org.apache.jasper.compiler.Compiler.generateJava(Compiler.java:202)
org.apache.jasper.compiler.Compiler.compile(Compiler.java:386)
org.apache.jasper.compiler.Compiler.compile(Compiler.java:362)
org.apache.jasper.compiler.Compiler.compile(Compiler.java:346)
org.apache.jasper.JspCompilationContext.compile(JspCompilationContext.java:605)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:400)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:385)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:329)
javax.servlet.http.HttpServlet.service(HttpServlet.java:733)
servlet.userlogin.doPost(userlogin.java:32)
javax.servlet.http.HttpServlet.service(HttpServlet.java:652)
javax.servlet.http.HttpServlet.service(HttpServlet.java:733)
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.
Apache Tomcat/9.0.41`
The code of userlogin.java is given below. Which shows error at line 32 and this line is request.getRequestDispatcher("user.jsp").include(request, response);
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import bean.userbean;
import dao.userdao;
#WebServlet("/userlogin")
public class userlogin extends HttpServlet {
private static final long serialVersionUID = 1L;
public userlogin() {
super();
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String email = request.getParameter("email");
String password = request.getParameter("password");
if(userdao.authenticate(email, password)) {
HttpSession session = request.getSession();
session.setAttribute("email",email);
request.getRequestDispatcher("user.jsp").include(request, response);
}else{
request.getRequestDispatcher("userlogin.jsp").include(request, response);
PrintWriter out=response.getWriter();
out.println("<h3>Username or password error</h3>");
}
}
}
And my user.jsp file is below
<%# page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%# page import="java.io.PrintWriter;" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<h1>User Section</h1>
<li>Sell</li>
<li>Logout</li>
</body>
</html>
Please help me to solve out it.
Related
This question already has answers here:
Servlet returns "HTTP Status 404 The requested resource (/servlet) is not available"
(19 answers)
Closed 2 months ago.
//index.jsp file
<%# page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<form action="IndexServlet" >
<input type="text" name="username">
<input type="submit">
</form>
</body>
</html>
//servlet file
import java.io.IOException;
import java.io.PrintWriter;
import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
public class Index extends HttpServlet{
#Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
PrintWriter out = resp.getWriter();
out.println("Hi There!");
}
}
enter image description here
I want to print "Hi There!" when hitting submit from jsp file. but when I do I either get a 404 or 500 error
//////////////////////////
HTTP Status 404 – Not Found
Type Status Report
Description The origin server did not find a current representation for the target resource or is not willing to disclose that one exists.
Apache Tomcat/10.0.27
//////////////////////////
HTTP Status 500 – Internal Server Error
Type Exception Report
Message Error instantiating servlet class [com.bootcamp.IndexServlet]
Description The server encountered an unexpected condition that prevented it from fulfilling the request.
Exception
jakarta.servlet.ServletException: Error instantiating servlet class [com.bootcamp.IndexServlet]
org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:542)
org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:92)
org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:690)
org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:356)
org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:399)
org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:65)
org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:870)
org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1762)
org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)
org.apache.tomcat.util.threads.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1191)
org.apache.tomcat.util.threads.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:659)
org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
java.base/java.lang.Thread.run(Thread.java:833)
Root Cause
java.lang.ClassNotFoundException: com.bootcamp.IndexServlet
Okay, so I think a .class file is missing. I created a new project where the class file is visible and it worked
not sure how it's working. lol
enter image description here
I've read about the articles here:
Tomcat: the origin server did not find a current representation for the target resource or is not willing to disclose that one exists
Unable to instantiate a servlet, getting class not found exception
Eclipse doesn't compile java files into classes
I have tried to make a simple custom tag JSP application, bu talternatively I keep receiving this error, or sometimes Unable to load tag handler class "HelloTag" for tag "ex:Hello".
My index.jsp looks like this:
<%#page contentType="text/html;charset=UTF-8" language="java" %>
<%#taglib prefix="ex" uri="WEB-INF/custom.tld"%>
<html lang="en">
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<title>customised tag</title>
</head>
<body>
<ex:Hello/>
</body>
</html>
My custom.tld from WEB-INF looks like this:
<taglib>
<tlib-version>1.0</tlib-version>
<jsp-version>2.0</jsp-version>
<short-name>Example TLD</short-name>
<tag>
<name>Hello</name>
<tag-class>HelloTag</tag-class>
<body-content>empty</body-content>
</tag>
</taglib>
And this is the Java class HelloTag.java from WEB-INF/classes:
import javax.servlet.jsp.tagext.*;
import javax.servlet.jsp.*;
import java.io.*;
public class HelloTag extends SimpleTagSupport {
public void doTag() throws JspException, IOException {
JspWriter out = getJspContext().getOut();
out.write("Hello Custom Tag!");
}
}
The entire error log:
exception
org.apache.jasper.JasperException: java.lang.ClassNotFoundException: org.apache.jsp.labJavaIJ.web.index_jsp
org.apache.jasper.servlet.JspServletWrapper.getServlet(JspServletWrapper.java:177)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:376)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:395)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:339)
javax.servlet.http.HttpServlet.service(HttpServlet.java:731)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
root cause
java.lang.ClassNotFoundException: org.apache.jsp.labJavaIJ.web.index_jsp
java.net.URLClassLoader.findClass(URLClassLoader.java:381)
org.apache.jasper.servlet.JasperLoader.loadClass(JasperLoader.java:132)
org.apache.jasper.servlet.JasperLoader.loadClass(JasperLoader.java:63)
org.apache.jasper.servlet.JspServletWrapper.getServlet(JspServletWrapper.java:172)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:376)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:395)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:339)
javax.servlet.http.HttpServlet.service(HttpServlet.java:731)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
This question already has answers here:
java.lang.IllegalStateException: Cannot (forward | sendRedirect | create session) after response has been committed
(9 answers)
Closed 6 years ago.
I create one JSP form and when i click on submit button my below doPost method call and this redirect to another jsp Error page with attribute "errorMessage"
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
try {
request.setAttribute("errorMessage", "You are not authorized to access the Hub System.");
RequestDispatcher view = request.getRequestDispatcher("/error.jsp");
view.forward(request, response);
}
} catch (ServletException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
My Error jsp page
<%# 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">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<table align="center">
<tr align="center">
<td><img src="images/logo.jpg" /></td>
</tr>
<tr>
<td>
<p style="color: red;">${errorMessage}</p>
</td>
</tr>
</table>
</body>
</html>
This scenario work perfectly but on my tomcat log i am getting below Error.
java.lang.IllegalStateException: Cannot forward after response has been committed
at org.apache.catalina.core.ApplicationDispatcher.doForward(ApplicationDispatcher.java:328)
at org.apache.catalina.core.ApplicationDispatcher.forward(ApplicationDispatcher.java:318)
at otn.aitc.io.MainServlet.doPost(MainServlet.java:203)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:648)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:292)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:207)
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:240)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:207)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:212)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:106)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:141)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79)
at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:616)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:88)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:522)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1095)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:672)
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1502)
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.run(NioEndpoint.java:1458)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
at java.lang.Thread.run(Thread.java:745)
I am using Java with Tomcat 8.
Tomcat maintains an internal buffer where it stores (part of) the response. The most important part is the status code and the headers. If an error occurs it can discard that buffer and send an error page for instance.The same happens if you try to send a redirect.
All these is possible because that buffer can be discarded. But if it has been sendt to the client, then there is nothing to be done, the client would have already received the response code and the headers, and the redirect is performed exactly by headers (Location) and a status code (301 or 302).
So the solution to your problem is not to commit the response - e.g, calling flush() on the response output stream, not filling up that buffer thus forcing tomcat to flush it, or even better do not try to send anything to the client if you intend to make a redirect.
This is the code I have put up in my jsp page just to test if it works or not correctly.
The jsp page works fine without any use of opencv classes.
But I got this error while using objects of the opencv library.
<%# page import="org.opencv.core.*" %>
<%# page import="org.opencv.highgui.Highgui" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Title</title>
</head>
<body>
<%
System.loadLibrary("opencv_java248");
Mat img = Highgui.imread("F:/project/im2.jpg");
%>
</body>
</html>
Attaching the error page for the details:
The error code is :
Stacktrace:
org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:553)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:442)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:391)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:334)
javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
root cause
javax.servlet.ServletException: java.lang.UnsatisfiedLinkError: org.opencv.highgui.Highgui.imread_1(Ljava/lang/String;)J
org.apache.jasper.runtime.PageContextImpl.doHandlePageException(PageContextImpl.java:911)
org.apache.jasper.runtime.PageContextImpl.handlePageException(PageContextImpl.java:840)
org.apache.jsp.first_jsp._jspService(first_jsp.java:79)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:419)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:391)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:334)
javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
root cause
java.lang.UnsatisfiedLinkError: org.opencv.highgui.Highgui.imread_1(Ljava/lang/String;)J
org.opencv.highgui.Highgui.imread_1(Native Method)
org.opencv.highgui.Highgui.imread(Highgui.java:359)
org.apache.jsp.first_jsp._jspService(first_jsp.java:68)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:419)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:391)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:334)
javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
The unsatisfied link error is telling you, that there was no library with such a name found.
See here: OpenCV + Java = UnsatisfiedLinkError.
On a side note, you really shouldn't be using code inside JSPs. Create a Servlet that does what you want with the image, add it's path/URL as an attribute request.setAttribute("key", "value").
Use a RequestDispatcher:
getServletContext().getRequestDispatcher("/path/to/page.jsp").forward(request, response)
to forward the request and response to the JSP. You can then reference it there with ${key}.
I am having Jsp file under root folder in Tomcat and i want to include another jsp file which is placed under webapps folder. while i am trying following code
<%#page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body>
<h1>Hello World!</h1>
<jsp:include page="../docs/index.html"/>
</body>
</html>
i am getting the following exception
HTTP Status 500 - An exception occurred processing JSP page /trail.jsp at line 16
type Exception report
message An exception occurred processing JSP page /trail.jsp at line 16
description The server encountered an internal error that prevented it from fulfilling this request.
exception
org.apache.jasper.JasperException: An exception occurred processing JSP page /trail.jsp at line 16
13: </head>
14: <body>
15: <h1>Hello World!</h1>
16: <jsp:include page="../docs/index.html"/>
17: </body>
18: </html>
Stacktrace:
org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:568)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:470)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:390)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:334)
javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
root cause
java.lang.NullPointerException
org.apache.jasper.runtime.JspRuntimeLibrary.include(JspRuntimeLibrary.java:954)
org.apache.jsp.trail_jsp._jspService(trail_jsp.java:73)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:432)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:390)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:334)
javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
note The full stack trace of the root cause is available in the Apache Tomcat/7.0.42 logs.
Please help me to include the jsp under webapps folder in tomcat.
Thanks in advance
the file in webapps/doc/ will not be in the same context as trail.jsp. You can not include a jsp from another context. Although according to the code you are trying to include a static file. If this is right please update your description.
Try from the directory WEB-INF/views (src/main/webapp/WEB-INF/views)
<jsp:include page="/WEB-INF/views/index.jsp" />