有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

在JavaServlet中按下按钮后,我看不到表

我有以下Java代码:


    package destinatie;

    import java.io.IOException;
    import java.io.PrintWriter;
    import java.text.ParseException;
    import java.text.SimpleDateFormat;
    import java.util.Calendar;
    import java.util.Collections;
    import java.util.Comparator;
    import java.util.Date;
    import java.util.Vector;

    import javax.servlet.ServletException;
    import javax.servlet.annotation.WebServlet;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;

    class Destin {
        private String denumire;
        private Calendar data;

        public String getDenumire() {
            return denumire;
        }

        public void setDenumire(String denumire) {
            this.denumire = denumire;
        }

        public Calendar getData() {
            return data;
        }

        public void setData(Calendar data) {
            this.data = data;
        }

        public Destin(String denumire, Calendar data) {
            this.denumire = denumire;
            this.data = data;
        }

        public String toString() {

            return "<html><body><td width=50>" + denumire + "</td>" + "<td width=50>" + data.get(Calendar.DATE) + "/"
                    + (data.get(Calendar.MONTH) + 1) + "/" + data.get(Calendar.YEAR) + "</td></body></html>";
        }
    }

    @WebServlet("/Destinatie")
    public class Destinatie extends HttpServlet {
        private static final long serialVersionUID = 1L;
        Vector<Destin> vect = new Vector<Destin>();

        public Destinatie() {
            super();

        }

        protected void doGet(HttpServletRequest request, HttpServletResponse response)
                throws ServletException, IOException {
            PrintWriter out = response.getWriter();
            if (request.getParameter("afisare") != null) {
                String destinatie;
                destinatie = request.getParameter("denumire");
                String zi = request.getParameter("zi");
                String luna = request.getParameter("luna");
                String an = request.getParameter("an");
                Calendar c = Calendar.getInstance();
                SimpleDateFormat form = new SimpleDateFormat("dd/MM/yyyy");

                Date data;
                try {
                    data = form.parse(zi + "/" + luna + "/" + an);
                    c.setTime(data);
                } catch (ParseException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }

                Destin d = new Destin(destinatie, c);

                vect.add(d);

                out.println("<html><title>Destinatii</title><body>");
                out.println("<form method='GET' action='Destinatie'");
                out.println("<p><table  align='center' width =100 border=1>");
                for (int i = 0; i < vect.size(); i++) {
                    out.println("<tr size=100>" + vect.elementAt(i)
                            + "</tr>");
                }
                out.println("</table></p>");
                out.println("<p><input type='submit' name='stergere' value='Afisare ordonata' align='center'</p>");
                out.println("</form>");
                if (request.getParameter("stergere") != null) {

                    Collections.sort(vect, Comparator.comparing(Destin::getDenumire));


                    out.println("<table  align='center' width =100 border=1>");
                    for (int i = 0; i < vect.size(); i++) {
                        out.println("<tr size=100>" + vect.elementAt(i)
                                + "</tr>");
                    }
                    out.println("</table>");
                }
                out.println("</body>");
                out.println("</html>");
            }

        }

    }

以及以下html代码:


    <html>
        <head>
            <title>Destinatie</title>
                <body>
                    <form method="GET" action="http://localhost:8081/Destinatie/Destinatie">
                    <p>Denumire:<input type="text" name="denumire"></p>
                    <p>Data vizitei (zi, luna, an):<input type="text" size=20 name="zi"><input type="text" size=20 name="luna"><input type="text" size=20 name="an"></p>
                    <p><input type="submit" name="afisare" value="Trimite"></p>
                    </form>
                </body>
        </head>
    </html>

在我将元素添加到向量并在屏幕上的页面中显示它们之后,我必须在该页面上有一个按钮来对向量进行排序并再次显示表,但这次它是按照名称排序的。似乎当我按下排序按钮时,会打开一个空白页,但新的排序表不会显示在那里。有没有关于如何解决这个问题的建议


共 (1) 个答案

  1. # 1 楼答案

    servlet需要查询参数afisare

    if (request.getParameter("afisare") != null) {
    

    但是,servlet生成的表单中缺少该参数。该表单发送的唯一参数是“Afisare ordonata”按钮生成的参数:

    <input type='submit' name='stergere' value='Afisare ordonata' ...
    

    单击按钮后,很容易在浏览器地址栏上看到:

    http://localhost:8081/Destinatie/Destinatie?stergere=Afisare+ordonata