kamweshi 0 Newbie Poster

Hi, can anyone help me to split the servlet(week02_4) so that only control code remains.I need to forward/redirect request/responses to two other servlets(VowelFrequencyServlet and WordCountServlet).I am not sure where to place the RequestDispatcher.

Week02_04 servlet:

package servlets;

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;

public class Week02_04 extends HttpServlet
{
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException
    {
        String reportType = request.getParameter("reportType");
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        String userString = request.getParameter("userString");

        try
        {
            out.println("<html>");
            out.println("<head>");
            out.println("<title>Servlet Week02_04</title>");
            out.println("<link rel=\"stylesheet\" type=\"text/css\" href=\"Week02_04.css\">");
            out.println("</head>");
            out.println("<body>");
            out.println("<h1>Servlet Week 02, Exercise 04</h1>");
            out.println("<p class=\"inline\">You typed: '<pre class=\"inline\">" +
                        userString + "</pre>'</p>");
            
            if (reportType.equalsIgnoreCase("W"))
            {
                int numWords = countWords(userString);
                out.println("<p>It has " + numWords +
                            " word" + (numWords == 1 ? "" : "s") + "</p>");
            }
            else if (reportType.equalsIgnoreCase("V"))
            {
                char[] vowel = {'a', 'e', 'i', 'o', 'u'};
                int[] count = {0, 0, 0, 0, 0};

                for (int i = 0; i < userString.length(); i++)
                {
                    switch (userString.charAt(i))
                    {
                        case 'a':
                        case 'A':
                            count[0]++;
                            break;
                        case 'e':
                        case 'E':
                            count[1]++;
                            break;
                        case 'i':
                        case 'I':
                            count[2]++;
                            break;
                        case 'o':
                        case 'O':
                            count[3]++;
                            break;
                        case 'u':
                        case 'U':
                            count[4]++;
                            break;
                        default:
                            break;
                    }
                }

                out.println("<h3>Vowel usage:</h3>");
                out.println("<table>");
                for (int i = 0; i < vowel.length; i++)
                {
                    out.print("<tr" + (i % 2 == 0 ? " class=\"shaded\"" : "") + ">" +
                              "<td class=\"vowel\">'" + vowel[i] + "'</td>" +
                              "<td class=\"barchart\">");
                    if (count[i] == 0)
                    {
                        out.print("-");
                    }
                    else
                    {
                        for (int star = 0; star < count[i]; star++)
                        {
                            out.print("*");
                        }
                    }
                    out.println("</td></tr>");
                }

                out.println("</table>");
            }
            
            out.println("</body>");
            out.println("</html>");
        }
        finally
        {
            out.close();
        }
    }

    private int countWords(String str)
    {
        int numWords = str.trim().length();
        if (numWords > 0)
        {
            numWords = str.split(" ").length;
        }
        return numWords;
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException
    {
        processRequest(request, response);
    }

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException
    {
        processRequest(request, response);
    }

    /** 
     * Returns a short description of the servlet.
     */
    public String getServletInfo()
    {
        return "Short description";
    }// </editor-fold>
}

VowelFrequencyServlet:

package servlets;

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;

public class VowelFrequencyServlet extends HttpServlet
{
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException
    {

        PrintWriter out = response.getWriter();
        String userString = request.getParameter("userString");
}

WordCountServlet:

package servlets;

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;

public class WordCountServlet extends HttpServlet
{
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException
    {
        PrintWriter out = response.getWriter();
         String userString = request.getParameter("userString");
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.