0

How to set ServeltConfig param name and value in index.jsp page with out xml configuration like initParams in Servlet ? is Possible ?

<servlet>
    <servlet-name>welcome</servlet-name>
    <jsp-file>/index.jsp</jsp-file>

    <init-param>
        <param-name>website</param-name>
        <param-value>www.google.com</param-value>
    </init-param>
</servlet>
Markus W Mahlberg
  • 19,711
  • 6
  • 65
  • 89
Balaji
  • 21
  • 5

2 Answers2

0

In JSP, we have implicit objects, in which we have config (This is the ServletConfig object associated with the page) object. But I guess we can't add parameters manually as their is not method for adding init parameter in ServletConfig interface.

If you want to save any parameters you can save in 4 scopes of JSP (page,request,session and application) and use it wherever you needed.

<c:set var="user" value="TestUser" scope="session"> //can set any value
Shoaib Chikate
  • 8,665
  • 12
  • 47
  • 70
0

You can do this with ServletContextListener. When the container starts up, it will call the ServletContextListener class. There you can set your params:

@WebListener
public class ContextListener implements ServletContextListener {

  public void contextInitialized(ServletContextEvent servletContextEvent) {
    ServletContext ctx = servletContextEvent.getServletContext();
     ctx.setAttribute("website", "www.google.com");
  }
}
Sas
  • 2,473
  • 6
  • 30
  • 47