我有简单的 Java Web 应用程序。
有一个index.html文件,其链接指向路径“test/aaa”
<html>
<head>
<title></title>
</head>
<body>
<a href="test/aaa"> click </a>
</body>
</html>
这是 web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.1" xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd">
<servlet>
<servlet-name>servlet1</servlet-name>
<servlet-class>web.Servlet1</servlet-class>
</servlet>
<servlet>
<servlet-name>servlet2</servlet-name>
<servlet-class>web.Servlet2</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>servlet1</servlet-name>
<url-pattern>/test/*</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>servlet2</servlet-name>
<url-pattern>/haha/*</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
</welcome-file-list>
</web-app>
所以路径“test/aaa”通向servlet web.Servlet1
package web;
import java.io.IOException;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class Servlet1 extends HttpServlet {
@Override
public void doGet(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
System.out.println("1");
RequestDispatcher view = request.getRequestDispatcher("/test.jsp");
view.forward(request, response);
System.out.println("2");
}
}
Servlet1 只指向 test.jsp
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body>
<h1>Hello World!</h1>
<img src="haha/bbb">
</body>
</html>
test.jsp 只显示“Hello World!”以及“位于”路径“haha/bbb”的图像
在 web.xml 中设置路径“haha/bbb”通向生成并返回简单图像的 servlet web.Servlet2
package web;
import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class Servlet2 extends HttpServlet {
@Override
public void doGet(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
System.out.println("3");
BufferedImage image = new BufferedImage(256, 256,
BufferedImage.TYPE_INT_RGB);
Graphics2D imageGraphics = image.createGraphics();
imageGraphics.setColor(Color.BLACK);
imageGraphics.fillRect(10, 10, 200, 200);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ImageIO.write(image, "png", baos);
baos.flush();
byte[] imageInByte = baos.toByteArray();
baos.close();
response.setContentType("image/png");
response.setContentLength(imageInByte.length);
response.getOutputStream().write(imageInByte);
System.out.println("4");
}
}
我期望在浏览器中看到图像并在控制台中看到此输出:
1
2
3
4
相反,我在浏览器中看不到图像,而在控制台中我看到了这个:
1
2
1
2
此外,如果我在浏览器中手动转到路径“haha/bbb”,我会在控制台中看到图像和“34”
有人可以解释一下为什么 web.Servlet1 调用了 2 次而 web.Servlet2 根本没有调用吗?
最佳答案
请记住,当您调用 Servlet1
RequestDispatcher view = request.getRequestDispatcher("/test.jsp");
view.forward(request, response);
您的网址没有改变,所以它仍然是
server/YourProject/test/aaa
这意味着当浏览器加载由 test.jsp
生成的代码时,包括
<img src="haha/bbb">
该路径将引用
server/YourProject/test/haha/bbb
^^^^^^^^^^^^^^^^^^^^^^^^
since this part is location of `aaa`, `haha/bbb` will be simply added to it.
(并且由于 /test/*
映射,Servlet1 将需要处理此调用,这解释了控制台中的附加 1 2
输出)
不
server/YourProject/haha/bbb
要解决此问题,请不要使用相对路径,而是使用资源的完整路径。您可以在 EL 的帮助下创建它
<img src="${pageContext.request.contextPath}/haha/bbb">
其中 ${pageContext.request.contextPath}/
返回您的 server/projectName
关于java - Servlet 未调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32461421/