Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
3.1k views
in Technique[技术] by (71.8m points)

model view controller - Loading images/css/javascript in a jsp/java ee web application

Today I just had my first class on Java ee and dynamic web project... and I have a question for you.

My teacher asked us to create a controller in an very basic mvc concept.

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...

Here is my servlet Controller.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);
}

}

Here is my web.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>

Here is my 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>

Files Structure: Files Structure

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.

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>

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.

See also:


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...