Sunday, 22 November 2015

How to configure a JSP file in web.xml?

In java web applications, you can directly access jsp files without any configuration. For example, http://localhost:8080/SimpleWebapp/Index.jsp, in this link we are requesting index.jsp directly without any config.

But if you have placed your jsp files inside WEB-INF directory(see in the image - Welcome.jsp), you can't access directly. For this you need to configure the jsp in web.xml.


Let's see how to do,

1. In web.xml, define both servlet & servlet-mapping tags.
2. Inside servlet() tag, add your jsp location inside the tag.
"< jsp-file>/WEB-INF/Welcome.jsp< /jsp-file>"
3. Complete configuration is
< servlet>
     < servlet-name> Welcome< /servlet-name>
     < jsp-file> /WEB-INF/Welcome.jsp< /jsp-file>
< /servlet>

< servlet-mapping>
     < servlet-name> Welcome< /servlet-name>
     < url-pattern> /Welcome< /url-pattern>
< /servlet-mapping>
The result is showed below


0 comments:

Post a Comment

Note: only a member of this blog may post a comment.