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
ReportServletpackage 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>"); } }
web.xml filepackage 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>"); }
html 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>
<!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>