如何从一个servlet重定向到另一个?

时间:2016-02-04 22:40:16

标签: java servlets

我接受来自html页面的名字并在“firstservlet”类中显示它。在第一个servlet类中,我试图通过创建html文本框并重定向到“secondservlet”类来接受姓氏,我可以在其中显示名字和姓氏。但我得到404错误。如何解决此错误。

FirstServlet.java

   protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    response.setContentType("text/html");  
    PrintWriter out=response.getWriter();

   HttpSession session=request.getSession();
    out.println(session.isNew());

    String name=request.getParameter("FirstName");

    request.setAttribute("name", name);

    out.println("First name is " +name);

    out.print("\n");
    out.println("<html>");
    out.println("<body>");
    out.println("<form action=SecondServlet/>");
    out.println("Enter LastName<input type=text name=LastName>");
    out.println("<br>");
    out.println("<input type=submit />");
    out.println("</form>");
    out.println("</body>");
    out.println("</html>");

    String lname=request.getParameter("LastName");

    request.setAttribute("lname", lname);

    RequestDispatcher rd=request.getRequestDispatcher("SecondServlet");
    rd.forward(request, response);
}

SecondServlet.java

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    response.setContentType("text/html");  

    PrintWriter out=response.getWriter();

    String name=request.getParameter("FirstName");
    request.setAttribute("name", name);
    out.println("First name is " +name);

    out.println("<br>");

    String lname=request.getParameter("LastName");      
    request.setAttribute("name", lname);
    out.println("Last name is " +lname);

    out.println("<br>");

}

0 个答案:

没有答案