Showing posts with label Servlet. Show all posts
Showing posts with label Servlet. Show all posts

Monday, 30 January 2012

Servlets: Get in a Servlet a parameter specified in web.xml

Fragmet of web.xml:
<servlet>
  <servlet-name>Foo Servlet</servlet-name>
  <servlet-class>foo.FooServlet</servlet-class>
  <load-on-startup>1</load-on-startup>
  <init-param>
    <param-name>testParamName</param-name>
    <param-value>value1,value2</param-value>
  </init-param>
</servlet>

<servlet-mapping>
  <servlet-name>Foo Servlet</servlet-name>
  <url-pattern>/*</url-pattern>
</servlet-mapping>

Fragmet of servlet FooServlet.java:
public class FooServlet extends HttpServlet {
  public void init() throws ServletException {
     super.init();
     //...
      String[] params = this.getInitParameter("testParamName").split(",");
        
        for(String param : params) {
            //...
        }
  }

// ...
}

Friday, 4 November 2011

How to register a JSF application scoped managed bean from a Servlet

JSF application. scoped managed beans are basically stored as an attribute of the ServletContext. In servlet:
@Override
public void init() {
    getServletContext().setAttribute("managedBeanName", new BackingBean());
}
Access from web page with #{managedBeanName}
References http://stackoverflow.com/questions/6746149/how-to-register-a-jsf-managed-bean-programmatically

Saturday, 23 April 2011

Set Servlet response to UTF-8


// setting response to UTF-8
String contentType= "text/html;charset=UTF-8";
response.setContentType(contentType);

Write to Servlet response


protected void doGet(HttpServletRequest request, HttpServletResponse response) 
  throws ServletException, IOException {
  response.getWriter().write("foo");
}