Friday 15 August 2014

Add Servlet Programmatically to Servlet Context

ServletContext interface provides below methods to add servlet to servlet context programmatically.
    addServlet(String servletName, Class<? extends Servlet> servletClass)
    addServlet(String servletName, Servlet servlet)
    addServlet(String servletName, String className)

<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>Get Books</title>
    </head>
    <body>
        <form method="get" action="/servlet/Books">
           <select name="author">
                <option>Yashavant Kanetkar</option>
                <option>Deitel</option>
                <option>Herbert Schildt</option>
                <option>Bruce Eckel</option>
          </select> 
            <input type="submit" value="submit" />
        </form>
    </body>
</html>

import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.*;

public class GetBooks extends HttpServlet {
    
    Map<String, List<String>> booksInfo = new HashMap<> ();
    
    List<String> Yashavant = new ArrayList<> ();
    List<String> Deitel = new ArrayList<> ();
    List<String> Herbert = new ArrayList<> ();
    List<String> Bruce = new ArrayList<> ();
    
    {
        Yashavant.add("C Projects");
        Yashavant.add("Data Structure Through C ");
        Yashavant.add("Let Us C");
        Yashavant.add("Test Your C Skill");
        
        Deitel.add("How To Program C");
        Deitel.add("How To Program C++");
        Deitel.add("How To Program Java");
        Deitel.add("How To Program Android");
        
        Herbert.add("Java Programming Cook Book");
        Herbert.add("C++ Programming Cook Book");
        
        Bruce.add("Using C++");
        Bruce.add("Thinking in C++");
        Bruce.add("Thinking in Java");
        
        booksInfo.put("Yashavant Kanetkar", Yashavant);
        booksInfo.put("Deitel", Deitel);
        booksInfo.put("Herbert Schildt", Herbert);
        booksInfo.put("Bruce Eckel", Bruce);  
    }
    
    @Override
    public void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException{
       PrintWriter out = res.getWriter();
       String author;
       List<String> books;
       Iterator<String> iter;
       StringBuilder body;
       res.setContentType("text/html");
      
       String header = "<html>" +
                    "<head>" +
                    "<title> Hello World </title>" +
                    "</head>" +
                    "<body>" +
                    "<h1> <br />";                     
       String footer = "</h1> </body>" +
                    "</html>";
       
       body = new StringBuilder();
       author = req.getParameter("author");
       body.append("Books written by ").append(author).append(" are <br />");
       
       books = booksInfo.get(author);
       iter = books.iterator();
       
       while(iter.hasNext()){
           body.append(iter.next()).append("<br />");
       }
       
       out.println(header + "" + body + footer);
    }
}

import javax.servlet.*;
import javax.servlet.annotation.WebListener;

@WebListener
public class ContextListener1 implements ServletContextListener {

    @Override
    public void contextInitialized(ServletContextEvent sce) {
        ServletContext context = sce.getServletContext();
        /* Adding servlet to Servlet Context */
        ServletRegistration.Dynamic reg = context.addServlet("GetBooks", GetBooks.class);
        reg.addMapping("/Books");
    }

    @Override
    public void contextDestroyed(ServletContextEvent sce) {
        System.out.println("Context Listener1 Destroyed");
    }
}

reg.addMapping("/Books")
Above statemene adds a servlet mapping with the given URL patterns for the Servlet represented by this ServletRegistration.

Output





Prevoius                                                 Next                                                 Home

No comments:

Post a Comment