简体   繁体   中英

Help creating servlet in existing jsf project

I'm trying to create a servlet inside a JSF project. This is the first servlet that I've created and I'm using http://www.servletworld.com/servlet-tutorials/simple-servlet-example.html as an example to get me started.

The only thing is my folder structure is a little different because of the existing jsf.

I can get the form.html page to display, but when I type my name in and click submit, I get a 404.

My folder structure is as follows (only relevant files shown)


catalog (my project in eclipse)
- src
  - a
    - b
      - c
        - catalog
          - backing
            - WelcomeServlet.java
- WebContent
  - catalog
    - form.html
  - WEB-INF
    - web.xml
    - classes
      - a
        - b
          - c
            - catalog
              - backing
                - WelcomeServlet.class

Here is the servlet mappings in web.xml

<servlet>
    <servlet-name>WelcomeServlet</servlet-name>
    <servlet-class>a.b.c.catalog.backing.WelcomeServlet</servlet-class>
</servlet>
<servlet-mapping>
    <servlet-name>WelcomeServlet</servlet-name>
    <url-pattern>/WelcomeServlet</url-pattern>
</servlet-mapping>
<welcome-file-list>
    <welcome-file>/form.html </welcome-file>
</welcome-file-list>

<servlet>
    <servlet-name>Faces Servlet</servlet-name>
    <servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
    <servlet-name>Faces Servlet</servlet-name>
    <url-pattern>*.jsf</url-pattern>
</servlet-mapping>

<welcome-file-list>
    <welcome-file>/index.jsp</welcome-file>
    <welcome-file>/index.html</welcome-file>
</welcome-file-list>

This url brings up the form.html page just fine http://localhost:8080/catalog/catalog/form.html

When you click the submit button, it brings up the url http://localhost:8080/catalog/catalog/WelcomeServlet and it gives a 404 error. I'm thinking that there is an issue in my web.xml file, but i'm not certain and I can't figure this out.

Thanks

Try adding this after the first servlet mapping:

<servlet-mapping>
    <servlet-name>WelcomeServlet</servlet-name>
    <url-pattern>/catalog/WelcomeServlet</url-pattern>
</servlet-mapping>

From what I see, your servlet is mapped to contextroot /WelcomeServlet, and you're navigating to contextroot /catalog/WelcomeServlet. They're not the same thing.

I'm guessing you forgot a slash in your html.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM