我开始处理一个项目,管理员在登录时应通过网络界面执行以下操作:
我试图实现这一点,但我的arraylist一直将自己重置为默认值。
到目前为止已经创建了这些文件,并试图指出谁知道谁:
login.html ---> admin.jsp(当你登录时,你会看到admin.jsp)
我在admin.jsp中创建了2个按钮,名为showList和AddNewOperator。
第一个场景:
当您点击AddNewOperator时,您将被重定向到--->你在哪里创建.jsp 必须输入名称,cpr和ini。 在那之后你将被指向一个名为" newOperator"的servlet,这里我运行了doPost方法,我在这里发布了。
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
boolean completed = false;
RequestDispatcher rd = null;
String cpr = request.getParameter("cpr-nr");
String name = request.getParameter("name");
String ini = request.getParameter("ini");
Function func = new Function();
try {
completed = func.addNewOperator(cpr, name, ini);
} catch (Exception e) {
System.out.println("Exception, error found!!!");
}
if (completed == true) {
rd = request.getRequestDispatcher("Succes.jsp");
}
else {
rd = request.getRequestDispatcher("Error.jsp");
}
rd.forward(request, response);
因此,当您运行此代码时,它会向我的arraylist添加一个新运算符。我的问题是,如果我回到admin.jsp页面并点击showList,我的arraylist会自动重置。
成功创建新运算符后,它会将管理员转发到Succes.jsp,并告知它已创建。
你在这里看到的Succes.jsp:
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Succes!</title>
</head>
<body>
New user created!
<form method="GET" action="Administration.jsp">
<input type="submit" value="OK">
</form>
</body>
</html>
您会看到一个表单,您将返回admin.jsp。在这里,您可以单击showList按钮,然后将运行一个新的servlet!
第二个场景:
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
PrintWriter out = response.getWriter();
Function func = new Function();
List opr = null;
out.println("Operators in system:" + "<br>");
try {
opr = func.showlist(opr);
for(int i = 0; i < opr.size(); i++){
out.println(opr.size());
out.println(opr.get(i) + "<br>");
}
} catch (Exception e) {
out.print("Error found, when trying to show list");
}
}
因此,如果我运行此代码,它将在我的arraylist中打印出什么,但新创建的运算符不再存在,因为它已经以某种方式重置它自己。
尝试将此代码放入另一个servlet(第一个scenarie) 在这里它将正确打印,我看到新创建的运算符。
有什么问题?为什么会自行重置?它只应在我关闭程序时重置。