Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: MVC - Split the Java servlet(help needed)

  1. #1
    Junior Member
    Join Date
    Oct 2010
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default MVC - Split the Java servlet(help needed)

    I am trying to split the servlet (ControlServlet) so that only the control code remains and the rest should be moved to the other two servlets (Errorservlet and ReportServlet). I am not quiet sure about the RequestDispatcher method if i wrote it correctly. I am not getting compilation errors but when i try to run project it says no servlet mapping specified on the xml file for the servlets. what i am doing wrong?I am new to java.here is my code:

    ControlServlet

    package servlets;
     
     
     
    import javax.servlet.*;
    import javax.servlet.http.*;
    import java.io.*;
     
     
    public class ControlServlet_a003241a extends HttpServlet
    {
     
     @Override
     public void doGet(HttpServletRequest request,
            HttpServletResponse response)
                    throws ServletException, IOException
        {
            RequestDispatcher dispatcher = null;
            String userName = request.getParameter("userName");
     
     
            if (!userName.equals(""))
            {
                dispatcher =
                   request.getRequestDispatcher("ResportServlet_a003241a");
            }
            else
            {
                dispatcher =
                   request.getRequestDispatcher("ErrorServlet_a003241a");
            }
            dispatcher.forward(request, response);
        }
    }

    ErrorServlet
    package servlets;
     
    import java.io.*;
    import javax.servlet.*;
    import javax.servlet.http.*;
     
     
    public class ErrorServlet_a003241a extends HttpServlet {
     
     @Override
        public void doGet(HttpServletRequest request,
                          HttpServletResponse response)
                    throws ServletException, IOException
        {
            response.setContentType("text/html");
            PrintWriter out = response.getWriter();
     
            String userName = request.getParameter("userName");
     
                out.println("<html>");
                out.println("<head>");
                out.println("<title>Week 02, PortfolioExercise</title>");
                out.println("<link rel=\"stylesheet\" type=\"text/css\" href=\"Week02_Portfolio_a003241a.css\">");
                out.println("</head>");
                out.println("<body>");
                out.println("<h1>Week 02, Portfolio Exercise </h1>");
     
                out.println("</body>");
                out.println("</html>");
     
            if (userName.equals(""))
            {
     
                out.println("<p You typed:\" ");
     
            }
     
            out.println("<p>Please go <a href= \"index_a003241a.html\"> back  </a> \"and type your name \"  </p>");
            out.println("</body></html>");
        }
    }
    ReportServlet

    package servlets;
     
    import javax.servlet.*;
    import javax.servlet.http.*;
    import java.io.*;
     
     
    public class ReportServlet_a003241a extends HttpServlet {
     
      @Override
        public void doGet(HttpServletRequest request,
                          HttpServletResponse response)
                    throws ServletException, IOException
        {
            response.setContentType("text/html");
            PrintWriter out = response.getWriter();
     
            String userName = request.getParameter("userName");
     
     
     
                out.println("<html>");
                out.println("<head>");
                out.println("<title>Week 02, PortfolioExercise</title>");
                out.println("<link rel=\"stylesheet\" type=\"text/css\" href=\"Week02_Portfolio_a003241a.css\">");
                out.println("</head>");
                out.println("<body>");
                out.println("<h1>Week 02, Portfolio Exercise </h1>");
                out.println("<p Hello, " + userName);
                out.println("</body>");
                out.println("</html>");
     
     
        }
    web.xml file
    <?xml version="1.0" encoding="UTF-8"?>
    <web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
        <servlet>
            <servlet-name>ReportServlet_a003241a</servlet-name>
            <servlet-class>servlets.ReportServlet_a003241a</servlet-class>
        </servlet>
        <servlet>
            <servlet-name>ErrorServlet_a003241a</servlet-name>
            <servlet-class>servlets.ErrorServlet_a003241a</servlet-class>
        </servlet>
        <servlet>
            <servlet-name>ControlServlet_a003241aservlet</servlet-name>
            <servlet-class>servlets.ControlServlet_a003241a</servlet-class>
        </servlet>
        <servlet-mapping>
            <servlet-name>ReportServlet_a003241a</servlet-name>
            <url-pattern>/ReportServlet_a003241a </url-pattern>
        </servlet-mapping>
        <servlet-mapping>
            <servlet-name>ErrorServlet_a003241a</servlet-name>
            <url-pattern>/ErrorServlet_a003241a </url-pattern>
        </servlet-mapping>
        <servlet-mapping>
            <servlet-name>ControlServlet_a003241a</servlet-name>
            <url-pattern>/ControlServlet_a003241a </url-pattern>
        </servlet-mapping>
        <session-config>
            <session-timeout>
                30
            </session-timeout>
        </session-config>
    </web-app>
    html file:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
    <html>
      <head>
        <title>Week02, Portfolio Exercise</title>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <link rel="stylesheet" type="text/css" href="Week02_Portfolio_a003241a.css"/>
      </head>
      <body>
          <h1> Week02, Portfolio Exercise</h1>
          <form action="ControlServlet_a003241a" method="post"
          <p>Type your name in the box:
                <input type="text" name="userName"></p>
          <p>Then click the button:
              <input type="submit" value="Submit"></p>
    </form>
      </body>
    </html>


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,301
    Thanks
    181
    Thanked 824 Times in 767 Posts
    Blog Entries
    5

    Default Re: MVC - Split the Java servlet(help needed)

    Are the html file and servlets as mapped in the same directory? Typically, a servlet which is mapped to the root directory (eg '/servletname') is mapped to the root directory of the war and has a url of '/servlet-war-name/servlet-name'.

Similar Threads

  1. simple java help needed
    By miss confused in forum What's Wrong With My Code?
    Replies: 6
    Last Post: June 27th, 2010, 12:29 PM
  2. Java servlet read file
    By peliukasss in forum Java Servlet
    Replies: 0
    Last Post: April 15th, 2010, 07:02 PM
  3. Help needed on java array
    By rossfally in forum Collections and Generics
    Replies: 2
    Last Post: March 4th, 2010, 07:49 PM
  4. Urgent Help needed with java codes
    By makarov in forum Java Theory & Questions
    Replies: 0
    Last Post: November 13th, 2009, 06:23 AM
  5. Java NullPointer Exception in Server chat program
    By Valtros in forum Exceptions
    Replies: 1
    Last Post: May 8th, 2009, 05:06 AM

Tags for this Thread