Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8536933
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T10:46:49+00:00 2026-06-11T10:46:49+00:00

Today I just had my first class on Java ee and dynamic web project…

  • 0

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T10:46:50+00:00Added an answer on June 11, 2026 at 10:46 am

    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:

    • 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
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am getting my foot into contracting and have had today my first round
today I just opened my 1,5 month old project and wanted to add a
I'm running Mac OSX. Until today I had Python 2.6 with psycopg2 running just
Today I had to define the WIN32_MEAN_AND_LEAN preprocessor macro in a native C++ project
I had ever used the following code snippet in one class. Today, I copied
I had asked a question about this earlier, but it didn't get answered right
Just had my mind going today. I spent some time in IE debug mode,
The problem I have just started today, without really changing the code at all.
Possibly similar question: Do you ever use the volatile keyword in Java? Today I
Okay so heres the situation. I have just had to move servers due to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.