0

Question:

  1. I want to set servlet 'main' as welcome file in web.xml but it shows warning "File name references to main that does not exist in web content"
  2. Browser can't access CSS files but can access Images which both fall under same parent directory 'assests'
  3. I want to know if both these problems are related and how to solve them

What I've already referenced:

  1. how-to-include-external-css-file-in-jsp
  2. Similar SO questions: 1, 2, 3

Result:

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" 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">
 <display-name>SampleApplication</display-name>
 <welcome-file-list>
  <welcome-file>main</welcome-file>
 </welcome-file-list>
</web-app>

MainServlet.java

@WebServlet(name = "main", urlPatterns = { "/main" })
public class MainServlet extends HttpServlet {

    private static final long serialVersionUID = 1L;

    public MainServlet() {
        super();
    }

    /**
     * Forwards to the main page.
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        RequestDispatcher dispatcher
        = this.getServletContext().getRequestDispatcher("/WEB-INF/views/MainView.jsp");
        dispatcher.forward(request, response);

    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //Additional Logic
    }

MainView.jsp

<!DOCTYPE html>
<html lang="en">

<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Ship Short Dated Products</title>

<!-- ---------------------- BOOTSTRAP AND CUSTOM STYLESHEETS ----------------------- -->
<link rel="stylesheet" type="text/css"
    href="<c:url value="http://fonts.googleapis.com/css?family=Roboto:400,100,300,500" />">
<link rel="stylesheet" type="text/css"
    href="<c:url value="/assets/bootstrap-3.3.7-dist/css/bootstrap.min.css" />">
<link rel="stylesheet" type="text/css"
    href="<c:url value="/assets/font-awesome/css/font-awesome.min.css" />">
<link rel="stylesheet" type="text/css"
    href="<c:url value="/assets/css/form-elements.css" />">
<link rel="stylesheet" type="text/css"
    href="<c:url value="/assets/css/main-layout.css" />">

<!-- 
--------------------------- JQUERY AND BOOTSTRAP PLUGINS -------------------------------
--------------------------- Please maintain the order for libs -------------------------
-->
<script
    src="<c:url value="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js" />"></script>
<script
    src="<c:url value="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js" />"></script>

<!--[if lt IE 9]>
<script src="https://oss.maxcdn.com/html5shiv/3.7.2/html5shiv.min.js"></script>
<script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
<![endif]-->

</head>

<body>
    <!-- -------------------------------- IMAGE CONTENT -------------------------------- -->
    <img class="img-responsive" src="assests/images/HMSGradient.jpg">
    <!-- IMAGE CONTENT End -->

</body>
</html>

Directory Structure

enter image description here

Thanks!

babybear
  • 804
  • 1
  • 10
  • 24

1 Answers1

0

You should define a list of welcome files in the <welcome-file> element. So please try changing it to <welcome-file>MainView.jsp</welcome-file> and move the jsp file into WebContent/main/. In addition, you don't need to have MainServlet.java.

Kohei TAMURA
  • 4,970
  • 7
  • 25
  • 49
  • I prefer keeping jsp files into `WEB-INF` where it will not be visible to the users. And if I put the path as `/WEB-INF/views/MainView.jsp` it works without any warnings. Instead, without hardcoding that path, I want to use servlet name to fetch that page. Also, if I change it to your approach, would it load the CSS files for MainView.jsp? – babybear Apr 13 '18 at 12:59