问题描述
今天我刚刚上了Java ee和动态网络项目的第一堂课......我有一个问题。
Today I just had my first class on Java ee and dynamic web project... and I have a question for you.
我的老师要求我们创建一个控制器在一个非常基本的mvc概念。
My teacher asked us to create a controller in an very basic mvc concept.
她给了我们一些代码示例,并要求我们从控制器调用一个视图。好的,它的确有效!
但是,如果我尝试添加图像< img src =images / img.jpg/>
,我认为我的控制器重新路由文件夹images / img.jpg以及我的图片/ img.jpg是文件标题中的类型文本...
She gave us some code example and asked us to call a view from the controller. Ok, it works!But then, if I try to add an image <img src="images/img.jpg" />
, I think my controller re-route the folder images/img.jpg and well, my images/img.jpg is a type text in the file headers...
任何帮助将不胜感激...
Any help would be appreciated...
这是我的servlet
Controller.java
Here is my servletController.java
package ca.qc.lacmegantic.ville;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* Servlet implementation class Controller
*/
@WebServlet("/Controller")
public class Controller extends HttpServlet
{
private static final long serialVersionUID = 1L;
/**
* Processes requests for both HTTP <code>GET</code> and <code>POST</code>
* methods.
*
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
String urlCP = request.getRequestURI();
String url = urlCP.substring(request.getContextPath().length());
if (url.equals("/") || url.equals(""))
{
request.getRequestDispatcher("WEB-INF/views/view.jsp").forward(request, response);
}
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
* response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
processRequest(request, response);
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
* response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
processRequest(request, response);
}
}
这是我的网页.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
version="2.5">
<servlet>
<servlet-name>Controller</servlet-name>
<servlet-class>ca.qc.lacmegantic.ville.Controller</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Controller</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<session-config>
<session-timeout>30</session-timeout>
</session-config>
</web-app>
这是我的 view.jsp
<%@ 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>Hello World!</title>
</head>
<body>
<h1>Hello World!</h1>
<img src="images/img.jpg" />
</body>
</html>
文件结构:
推荐答案
你不应该是将前端控制器servlet映射到 /
的URL模式。这将覆盖servletcontainer的默认servlet,它负责提供静态资源,如图像。这不是你想要的。
You should not be mapping a front controller servlet on an URL pattern of /
. This would override servletcontainer's "default" servlet which is responsible for serving static resources such as images. This is not what you want.
将控制器映射到更具体的URL模式,例如 / pages / *
或者其他的东西。或者 / Controller
,就像你在 @WebServlet
注释中那样,实际上根本没有注册因为 web.xml
未声明为符合Servlet 3.0。
Map the controller on a more specific URL pattern such as /pages/*
or something. Or maybe /Controller
, exactly as you've there in the @WebServlet
annotation which is actually not been registered at all because your web.xml
is not declared conform Servlet 3.0.
<servlet-mapping>
<servlet-name>Controller</servlet-name>
<url-pattern>/Controller</url-pattern>
</servlet-mapping>
您的另一个问题是您已将图像放入 / WEB- INF
文件夹。此文件夹中的内容无法公开访问。它仅用于JSP文件,它们应该由前端控制器servlet或其他JSP转发或包含。将 / images
文件夹向上移动一级,位于 / WEB-INF
文件夹之外。
Your another problem is that you've placed the image in /WEB-INF
folder. The content in this folder is not publicly accessible. It's intented for JSP files only which are supposed to be forwarded or included by a front controller servlet or another JSP. Move the /images
folder one level up, outside the /WEB-INF
folder.
- - 包含一些hello world示例
- Our servlets wiki page - contains some hello world examples as well
- Difference between / and /* in servlet mapping url pattern
- Browser can't access/find relative resources like CSS, images and links when calling a Servlet which forwards to a JSP
- Moving resources under WEB-INF
这篇关于在jsp / java ee Web应用程序中加载images / css / javascript的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!