How to config a JSP file in web.xml?
Declare the following tags in web.xml against your jsp
<servlet>
<servlet-name>myjsp</servlet-name>
<jsp-file>/myjsp.jsp</jsp-file>
<init-param>
<param-name>hello</param-name>
<param-value>test</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>myjsp</servlet-name>
<url-pattern>/myjsp</url-pattern>
</servlet-mapping>
You need a servlet mapping, When you're dealing with a JSP as a Servlet.In order to be able to access servlet init params, you will need to create a mapping and call the JSP with the url-pattern in the mapping. JSP provides the "config" implicit object variable that eliminates the need to call getServletConfig(). You can get the init parameters from the implicit "config" object by calling getInitParameter() method or getInitParameters() method. For example,
<%= config.getInitParameter("hello");%>
If you make the url-pattern match the name of the JSP, people won't be able to skirt the web.xml configurations by typing the url of the JSP in the browser's address window.
If you want to add init-params to a JSP via web.xml, you need to create a servlet mapping for your JSP and use the url defined in that mapping when making the request.
Context init params, on the other hand are bound to the Context object (which is available to all components of the app) will be available, regardless of how your JSP was invoked.
Most Recent jsp Faqs
- How to get the real requested URI from inside the error page?
- Can I define a method in a JSP page?
- What is the difference between request.getParameter() and request.getAttribute()?
- What is difference between page and pageContext in JSP pages?
- How to config a JSP file in web.xml?
- What is the difference between jsp:forward and response.sendRedirect?
- How to retrieve values from HTML form input elements in JSP?
Most Viewed jsp Faqs
- How to config a JSP file in web.xml?
- What is difference between page and pageContext in JSP pages?
- How to retrieve values from HTML form input elements in JSP?
- How to disable browser caching for a specific JSP?
- What is the difference between jsp:forward and response.sendRedirect?
- What is the difference between request.getParameter() and request.getAttribute()?
- How to use comments in JSP pages?