user2409128 user2409128 - 24 days ago 8x
Java Question

How to include js and CSS in JSP with spring MVC

I want to include js and css file in my jsp but I'm not able to do so. I'm new in the concept os spring MVC. From long time I'm trying on this same topic
My index Page is like this

<!DOCTYPE html>
<link rel="stylesheet" type="text/css" href="${pageContext.request.contextPath}/style.css"/>
<script type="text/javascript" src="${pageContext.request.contextPath}/LoginPageScrip.js">


<style type="text/css">
body {
background-image: url("LoginPageBackgroundImage.jpg");
<body >
<h6>Please login in google Chrome</h6>
<h1 align="center">Welcome to my Twitter Clone</h1>
<div class="m" style="margin-left: 401px; margin-top: 70px;">
<form method="post" action="LoginForExistingUser" onsubmit="return Validate(this);">
<legend align="center">Login</legend>
<div class="a">
<div class="l">User Name</div>
<div class="r">
<INPUT type="text" name="userName">

<div class="a">
<div class="l">Password</div>
<div class="r">
<INPUT type="password" name="password">
<div class="a">
<div class="r">
<INPUT class="button" type="submit" name="submit"
<i align="center" style="margin-left: 183px;">New User? <a href="signup.html"><u>Signup</u></a></i>

And my spring-dispatcher-servlet.xml is like this.

<beans xmlns=""

<context:component-scan base-package="" />
<!--<bean id="HandlerMapping" class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping" />-->
<!-- <bean name="/welcome.html" class ="" /> -->
<bean id="viewResolver" class = "org.springframework.web.servlet.view.InternalResourceViewResolver" >
<property name="prefix">
<property name ="suffix">

My controller is like this.


import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

public class StudentInfoController {

@RequestMapping(value = "/indexPage.html", method = RequestMethod.GET)
public ModelAndView getAdmissionFrom() {
ModelAndView model = new ModelAndView("indexPage");
return model;

Can some one help me in this? I'm struggling very hard but I'm not getting any solution. I have kept my js and css file in WEB-INF folder.


First you need to declare your resources in dispatcher-servlet file like this :

<mvc:resources mapping="/resources/**" location="/resources/folder/" />

Any request with url mapping /resources/** will directly look for /resources/folder/.

Now in jsp file you need to include your css file like this :

<link href="<c:url value="/resources/css/main.css" />" rel="stylesheet">

Similarly you can include js files.

Hope this solves your problem.