Is that your entire web.xml
file? And by any chance are you accessing the JSP directly in the browser? Like:
http://localhost:8080/<yourAppContext>/result.jsp
If that is the case, then you will get this response:
Name is: Max
Email is: null
It is not wrong. It is correct.
The reason you get this result is that you are not accessing the JSP through the configuration you defined in web.xml
, you are just accessing the JSP directly, which behind the scene has a different implicit configuration, and it's not the one you think you are configuring.
If you want this response:
Name is: Max
Email is: [email protected]
Then you need to add a servlet mapping. The complete configuration is:
<servlet>
<servlet-name>test</servlet-name>
<jsp-file>/result.jsp</jsp-file>
<init-param>
<param-name>email</param-name>
<param-value>[email protected]</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>test</servlet-name>
<url-pattern>/test</url-pattern>
</servlet-mapping>
and you need to access this URL, not the JSP path, with:
http://localhost:8080/<yourAppContext>/test
You might want to also read these:
To further drive the point home, it's important to mention that you need a mapping for one of your servlets to be useful. If you just define a servlet in web.xml
, it just sits there. You need to tell the server how to use it, and for that you use the <servlet-mapping>
. It's saying to the server that for a request on a path, some specific servlet needs to be called to handle the request.
You can create this mapping to point to a servlet class using <servlet-class>
or to a JSP using <jsp-file>
. They are basically the same thing, since a JSP eventually becomes a servlet class.
What I think is confusing you (based on the comment below) is that for JSP files you already have some implicit mapping created by the server, as described here.
When you access the JSP directly, with
http://localhost:8080/<yourAppContext>/result.jsp
you are using the implicit server mapping which contains no special configuration attached (like the email you want to send to it).
When you access the JSP with
http://localhost:8080/<yourAppContext>/test
you are accessing your mapping. And this you can configure however you want, and send it whatever parameters you want, and your JSP will now be able to read them.