我正在尝试将我的Jsp页面与我的servlet链接,但是我收到了这个错误:
javax.servlet.ServletException: Servlet.init() for servlet ImageServlet threw exception
java.lang.IllegalStateException: No WebApplicationContext found: no ContextLoaderListener registered`
以下是我的Servlet代码:
package servlet;
@Component("ImageServlet")
public class ImageServlet implements HttpRequestHandler {
@Autowired
imageDA imageda = new imageDA();
ResultSet rs = null;
byte[] thumb ;// get the thumb from the user entity
@Override
public void handleRequest(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
int generatedDocId = Integer.parseInt(request.getParameter("generatedDocId"));
try{
rs = imageda.getAllImage(generatedDocId);
if(rs.next()){
thumb = rs.getBytes("IMAGE");
}
}catch(SQLException ex){
ex.getMessage();
}
String name = "images";
response.setContentType("image/jpeg");
response.setContentLength(thumb.length);
response.setHeader("Content-Disposition", "inline; filename=\"" + name+ "\"");
BufferedInputStream input = null;
BufferedOutputStream output = null;
try {
input = new BufferedInputStream(new ByteArrayInputStream(thumb));
output = new BufferedOutputStream(response.getOutputStream());
byte[] buffer = new byte[8192];
int length;
while ((length = input.read(buffer)) > 0) {
output.write(buffer, 0, length);
}
} catch (IOException e) {
System.out.println("There are errors in reading/writing image stream "
+ e.getMessage());
} finally {
if (output != null) {
try {
output.close();
} catch (IOException ignore) {
}
}
if (input != null) {
try {
input.close();
} catch (IOException ignore) {
}
}
}
}
}
这是我的XML代码链接到我的viewData.jsp JSP页面:
<servlet>
<servlet-name>ImageServlet</servlet-name>
<servlet-class>org.springframework.web.context.support.HttpRequestHandlerServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>ImageServlet</servlet-name>
<url-pattern>/viewData.jsp</url-pattern>
</servlet-mapping>
</web-app>
编辑,这是我的完整版XML代码。我将大部分内容用于我的Request.getPart(),尝试为spring配置XML,但它不起作用。谢谢帮助
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
<session-config>
<session-timeout>
30
</session-timeout>
</session-config>
<servlet>
<servlet-name>UploadFile</servlet-name>
<jsp-file>/projectAddData.jsp</jsp-file>
<multipart-config>
<max-file-size>20848820</max-file-size>
<max-request-size>418018841</max-request-size>
<file-size-threshold>1048576</file-size-threshold>
</multipart-config>
</servlet>
<servlet-mapping>
<servlet-name>UploadFile</servlet-name>
<url-pattern>/projectAddData.jsp</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>UploadFile1</servlet-name>
<jsp-file>/addClaim.jsp</jsp-file>
<multipart-config>
<max-file-size>20848820</max-file-size>
<max-request-size>418018841</max-request-size>
<file-size-threshold>1048576</file-size-threshold>
</multipart-config>
</servlet>
<servlet>
<servlet-name>UploadServlet1</servlet-name>
<servlet-class>UploadServlet1</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>UploadFile1</servlet-name>
<url-pattern>/addClaim.jsp</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>UploadFile2</servlet-name>
<jsp-file>/addInvoice.jsp</jsp-file>
<multipart-config>
<max-file-size>20848820</max-file-size>
<max-request-size>418018841</max-request-size>
<file-size-threshold>1048576</file-size-threshold>
</multipart-config>
</servlet>
<servlet>
<servlet-name>UploadServlet2</servlet-name>
<servlet-class>UploadServlet2</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>UploadFile2</servlet-name>
<url-pattern>/addInvoice.jsp</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>imageServlet</servlet-name>
<servlet-class>org.springframework.web.context.support.HttpRequestHandlerServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>imageServlet</servlet-name>
<url-pattern>/viewData.jsp</url-pattern>
</servlet-mapping>
<context-param>
<param-name>contextConfigLocation</param-name>
<!-- The path to your main spring xml file, for example: /WEB-INF/spring-config.xml -->
<param-value>/WEB-INF/applicationContext.xml</param-value>
</context-param>
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
</web-app>
答案 0 :(得分:1)
例外情况告诉您,虽然您打算使用spring,但由于缺少必需的侦听器配置,无法加载ApplicationContext
。 Spring需要知道主bean配置文件的路径,缺少的侦听器就是它用来查找信息的。
将此添加到您的web.xml(作为根<web-app>
元素的子项,并且您的网络应用将启动(或至少使其超过您现在获得的错误)。
<context-param>
<param-name>contextConfigLocation</param-name>
<!-- The path to your main spring xml file, for example: /WEB-INF/spring-config.xml -->
<param-value>/WEB-INF/spring-config.xml</param-value>
</context-param>
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>