我有以下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>
将元素添加到向量中并在屏幕上的页面中显示它们之后,我必须在该页面上具有对向量进行排序并再次显示表格的按钮,但这一次它是按名称排序的。看来,当我按下排序按钮时,会打开一个空白页,但新的有序表未显示在那里。关于如何解决此问题的任何建议?
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