0

This is my code for one servlet in my web.xml file:

        <context-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>/WEB-INF/applicationContext.xml</param-value>
        </context-param>
        <listener>
            <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
        </listener>

        <servlet>
            <servlet-name>dispatcher</servlet-name>
            <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
            <load-on-startup>2</load-on-startup>
        </servlet>

        <servlet-mapping>
            <servlet-name>dispatcher</servlet-name>
            <url-pattern>/</url-pattern>
        </servlet-mapping>

        <session-config>
            <session-timeout>
                30
            </session-timeout>
        </session-config>

        <welcome-file-list>
            <welcome-file>redirect.jsp</welcome-file>
        </welcome-file-list>

Can I just copy and paste and change certain values and use that again?

SECOND QUESTION (WHICH MAY HELP ANSWER THE FIRST)

currently when i deploy my application in glassfish with this url localhost/HelloSpring I get a 404. However, when I add /home on the end like so: localhost/HelloSpring/home it goes to the correct page. I can also add /index localhost/HelloSpring/index on the end and that works correctly too. So how can I load localhost/HelloSpring/home from startup rather than having to type it in manually and thus avoiding the 404 error?

NOTE: when changing the url pattern from / to /home in the servlet mappings, fixes the /home problem but then means /index will not work

So in summary, how can I load up localhost/home from deployment and then type in index into the url /index and display the correct page?

p.s. stackoverflow would not let me type in my full address links so assume where I have said localhost it contains the port :8080 directly after

  • i think it's your url pattern, i'm using something like this *.html to have all .html requests handled so you probably need to play with this. Off the top of my head i don't remember if /* is legal or not. just a general idea though – vector Oct 30 '14 at 15:17
  • that's what I was working with before but I thought web applications look a bit neater without the .html extensions. I was hoping just to do it without the extensions. It is definitely possible as most websites do this. i had thought of /* too but there was no joy to be found there either –  Oct 30 '14 at 16:05

2 Answers2

0

Your servlet does not recognize automatically a home page. You have to tell it by either using your redirect page (you already include it in welcome-file-list), or map your controller that serves the home page to the root as well:

@RequestMapping(value = { "/", "/home" }, method = RequestMethod.GET)
gregdim
  • 2,031
  • 13
  • 15
  • Unfortunately it didnt like that. copied and pasted your line of code into mine and then tried the url pattern with both / and /home. It gave me the same results as before. this is the url that loads up from deployment http://localhost:8080/HelloSpring/ might it have something to do with that? –  Oct 30 '14 at 16:03
  • Can you post the controller that handles the /home request? – gregdim Oct 30 '14 at 16:10
  • package service; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; @Controller public class WebController { @RequestMapping(value = { "/", "/home" }, method = RequestMethod.GET) public String home() { return "home"; } @RequestMapping(value = "/index", method = RequestMethod.GET) public String index() { return "index"; } } –  Oct 30 '14 at 16:17
  • sorry that looks very messy. reposted below without imports: @Controller public class WebController { @RequestMapping(value = { "/", "/home" }, method = RequestMethod.GET) public String home() { return "home"; } @RequestMapping(value = "/index", method = RequestMethod.GET) public String index() { return "index"; } } –  Oct 30 '14 at 16:17
  • Seems ok. Have you redeployed the app? btw you can re-edit your question next time you need to append code. Dont do it in comments. – gregdim Oct 30 '14 at 16:25
  • my apologies. Yes I have redeployed and still no success in the root issue. shut down netbeans, cleaned and built again and still no luck annoyingly. –  Oct 30 '14 at 16:31
  • Look at the answer of Serge Ballesta. I forgot to mention that I always serve explicitly static resources using mvc:resources, so maybe that's what you need. – gregdim Oct 30 '14 at 16:36
  • okay i'll have a look. just going back to one other thing for me to clarify. when i deploy and get this page http://localhost:8080/HelloSpring. how can i the value of HelloSpring? or can i not? –  Oct 30 '14 at 16:43
  • also can you explain what the first part: context-param, all the way down to the closing listener tag does? –  Oct 30 '14 at 17:02
  • With context-param you set the location of the application context file where your beans or other configuration is defined in case you are using complementary configuration files to your servlet configuration. Take some time to look into the spring reference documentation if you intend to work with spring. It covers all these concepts very well. – gregdim Oct 30 '14 at 17:21
0

For first question, you can of course declare as many servlets as you want in the web.xml file, but I really cannot figure a use case for declaring more than one DispatcherServlet.

Your second question seems related to the problem of hitting root url in a Spring MVC application. I tried some time ago to post in SO what I found on the problem : Match for root url and serving of static resources

Community
  • 1
  • 1
Serge Ballesta
  • 143,923
  • 11
  • 122
  • 252
  • so can i copy and paste all the code for my current servlet and then just rename things right? –  Oct 30 '14 at 16:54
  • Of course you can, but then you will have to worry which servlet will be hit by which request, and configure controllers accordingly – Serge Ballesta Oct 30 '14 at 17:35