0

I have to deploy a webapp with two server (ex. servlet1 and servlet2)

In servlet1 I have to show a form, and when is submitted servlet2 return the data inserted in the form.

As shown in the code below I made the two classes and configured the web.xml file, but it doesn't work. Indeed:

Message The requested resource [/bin_form/servlet2] is not available

Description The origin server did not find a current representation for the target resource or is not willing to disclose that one exists.

I'm doing it wrong in the configuration of xml file/tomcat configuration repository or in the java code?

Here is my configuration and code.

-Repository structure on tomcat:

webapp
 |
 -form
   |
   -index.html
   -WEB_INF
     |
     -web.xml
     -classes
         |
          -servlet1.java
          -servlet1.class
          -servlet2.java
          -servlet2.class

-Form on index.html file:

<form action="form" method="get">
<label for="fname">First name:</label>
<input type="text" name="fName">
<label for="lname">Last name:</label>
<input type="text" name="lName">
<label for="lname">Age:</label>
<input type="text" name="age">
<input type="submit" value="submit">

-web.xml file:

<web-app>

<servlet>
<servlet-name>servlet1</servlet-name>
<servlet-class>servlet1</servlet-class>
</servlet>

<servlet-mapping>
<servlet-name>servlet1</servlet-name>
<url-pattern>/form</url-pattern>
</servlet-mapping>

<servlet>
<servlet-name>servlet2</servlet-name>
<servlet-class>servlet2</servlet-class>
</servlet>

<servlet-mapping>
<servlet-name>servlet2</servlet-name>
<url-pattern>/form-resp</url-pattern>
</servlet-mapping>

</web-app>
  • java classes:
  public void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
    res.setContentType("text/html");
    PrintWriter pw = res.getWriter();

    String fName = req.getParameter("fName");
    String lName = req.getParameter("lName");
    String age = req.getParameter("age");

    req.setAttribute("fName", fName);
    req.setAttribute("lName", lName);
    req.setAttribute("age", age);

    RequestDispatcher rd = req.getRequestDispatcher("servlet2");
    rd.forward(req, res);

  }
} 

public class servlet2 extends HttpServlet {
  public void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
    res.setContentType("text/html");
    RequestDispatcher rd = req.getRequestDispatcher("servlet1");
    rd.include(req, res);

    PrintWriter pw = res.getWriter();
    String fName = (String)req.getAttribute("fName");
    String lName = (String)req.getAttribute("lName");
    String age = (String)req.getAttribute("age");

    pw.println("Name " + fName);
    pw.println("Last Name " + lName);
    pw.println("Age " + age);

  }
}
     


       
pierbin24
  • 31
  • 5

0 Answers0