Java Servlet响应表

时间:2017-03-14 17:42:53

标签: java http servlets java-ee java-ee-7

我正在开发一个项目,我必须修改Java Servlet应用程序以添加其他字段供用户输入信息。然后在按下“提交”按钮后,响应应显示在与初始字段页面相同的页面上。我的问题是,响应页面没有出现。响应页面应该显示用户在初始页面中输入的内容,但是当前,我的初始页面只清除输入字段并且响应从不显示。我对Java很新,所以如果你看到我需要改进的其他内容,请告诉我!以下是我目前的代码:

hello2问候语:

package javaeetutorial.hello2;

import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@WebServlet("/greeting")
public class GreetingServlet extends HttpServlet {

    @Override
    public void doGet(HttpServletRequest request,
            HttpServletResponse response)
            throws ServletException, IOException {

        response.setContentType("text/html");
        response.setBufferSize(8192);
        try (PrintWriter out = response.getWriter()) {
            out.println("<html lang=\"en\">"
                    + "<head><title>Servlet Hello</title></head>");

            out.println("<body  bgcolor=\"#ffffff\">"
                    + "<img src=\"resources/images/duke.waving.gif\" "
                    + "alt=\"Duke waving his hand\">"
                    + "<form method=\"get\">"
                    + "<h2>Please complete our Customer Survey</h2>"
                    + "<h3>First name:</h3>"
                    + "<input title=\"First Name \" type=\"text\" "
                    + "fname=\"firstname\" size=\"25\"/>"
                    + "<h4>Last name:</h4>"
                    + "<input title=\"lastname\" type=\"text\" "
                    + "lname=\"lastname\" size=\"25\"/>"
                    + "<h5>E-mail:</h5>"
                    + "<input title=\"E-mail \" type=\"text\" "
                    + "emailaddress=\"email\" size=\"25\"/>"
                    + "<h6>Birth date:</h6>"
                    + "<input title=\"Birthday \" type=\"text\" "
                    + "bdate=\"birthdate\" size=\"25\"/>"
                    + "<p></p>"
                    + "<input type=\"submit\" value=\"Submit\"/>"
                    + "<input type=\"reset\" value=\"Reset\"/>"
                    + "</form>");

            String firstname = request.getParameter("fname");
            String lastname = request.getParameter("lname");
            String email = request.getParameter("emailaddress");
            String birthdate = request.getParameter("bdate");

            if (firstname != null && firstname.length() > 0) {

                RequestDispatcher dispatcher
                        = getServletContext().getRequestDispatcher("/response");

                if (dispatcher != null) {
                    dispatcher.include(request, response);
                }
            }
            if (lastname != null && lastname.length() > 0) {
                RequestDispatcher dispatcher
                        = getServletContext().getRequestDispatcher("/response");

                if (dispatcher != null) {
                    dispatcher.include(request, response);
                }
            }
            if (email != null && email.length() > 0) {
                RequestDispatcher dispatcher
                        = getServletContext().getRequestDispatcher("/response");

                if (dispatcher != null) {
                    dispatcher.include(request, response);
                }
            }
            if (birthdate != null && birthdate.length() > 0) {
                RequestDispatcher dispatcher
                        = getServletContext().getRequestDispatcher("/response");

                if (dispatcher != null) {
                    dispatcher.include(request, response);
                }
            }
            out.println("</body></html>");
        }
    }

    @Override
    public String getServletInfo() {
        return "The Hello servlet says hello.";
    }

}

hello2回复:

package javaeetutorial.hello2;

import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@WebServlet("/response")
public class ResponseServlet extends HttpServlet {

    @Override
    public void doGet(HttpServletRequest request,
            HttpServletResponse response)
            throws ServletException, IOException {
        try (PrintWriter out = response.getWriter()) {
            out.println("Thank you for completing our Customer Survey form on !");
            String firstname = request.getParameter("firstname");
            String lastname = request.getParameter("lastname");
            String email = request.getParameter("email");
            String birthdate = request.getParameter("birthdate");
            
            if (firstname != null && firstname.length() > 0) {
                out.println("<h2>First name: " + firstname + "</h2>");
            }
            if (lastname != null && lastname.length() > 0) {
                out.println("<h3>Last name: " + lastname + "</h3>");
            }
            if (email != null && email.length() > 0) {
                out.println("<h4>E-mail address: " + email + "</h4>");
            }
            if (birthdate != null && birthdate.length() > 0) {
                out.println("<h5>Birth date: " + birthdate + "</h5>");
            }
        }
    }

    @Override
    public String getServletInfo() {
        return "The Response servlet says hello.";

    }
}

1 个答案:

答案 0 :(得分:0)

问题太小了......你忘记了表单的 action 属性。由于这个原因,你的响应没有被发送到RespponseServlet。 应该是这样的

<form action='response' method=\"get\">