📄 input.jsp
字号:
<%@ page contentType="text/html;charset=GB2312" %>
<%@ page import ="java.io.*" %>
<%@ page import ="java.util.*" %>
<HTML>
<BODY>
<%! //声明创建一个散列表对象,被所有的客户共享:
Hashtable hashtable=new Hashtable();
//一个向散列表填加信息的同步方法:
synchronized void putGoodsToHashtable(String key,String list)
{hashtable.put(key,list);
}
//从散列表移去信息的同步方法:
synchronized void removeGoodsToHashtable(String key)
{hashtable.remove(key);
}
%>
<%--获取客户提交的书号名字和数量--%>
<% String name=request.getParameter("N");
if(name==null)
{name="have no any goods number";
}
byte c[]=name.getBytes("ISO-8859-1");
name=new String(c);
String mount=request.getParameter("M");
if(mount==null)
{mount="have no any recoder";
}
byte d[]=mount.getBytes("ISO-8859-1");
mount=new String(d);
%>
<%--从文件中读散列表,如果文件不存在,你就是第一个录入的人,负责写散列表到文件--%>
<%File f=new File("e:/","goods.txt");
if(f.exists())
{ try{FileInputStream in=new FileInputStream(f);
ObjectInputStream object_in=new ObjectInputStream(in);
hashtable=(Hashtable)object_in.readObject();
object_in.close();
in.close();
String temp=(String)hashtable.get(name);
if(temp==null)
{temp="";
}
if((temp.length())!=0)
{ out.print("<BR>"+"该货号已经存在,您已经修改了数量");
String s="货号:"+name+" ,数量:"+mount;
removeGoodsToHashtable(name); //首先移去旧的信息。
putGoodsToHashtable(name,s); //填加新的信息。
//将新的散列表写入到文件:
try{FileOutputStream o=new FileOutputStream(f);
ObjectOutputStream object_out=new ObjectOutputStream(o);
object_out.writeObject(hashtable);
object_out.close();
o.close();
}
catch(Exception eee)
{ }
}
else
{String s="货号:"+name+" ,数量:"+mount;
putGoodsToHashtable(name,s); //向散列表填加新的货物信息。
//再将新的散列表写入到文件:
try{FileOutputStream o=new FileOutputStream(f);
ObjectOutputStream object_out=new ObjectOutputStream(o);
object_out.writeObject(hashtable);
object_out.close();
o.close();
}
catch(Exception eee)
{ }
out.print("<BR>"+"您已经将货物存入文件");
out.print("<BR>"+"货物的货号:"+name);
}
}
catch(IOException e) {}
}
else
{ //如果文件不存在,您就是第1个录入信息的人。
String s="货号:"+name+"数量:"+mount;
putGoodsToHashtable(name,s);//放信息到散列表。
//负责创建文件,并将散列表写入到文件:
try{ FileOutputStream o=new FileOutputStream(f);
ObjectOutputStream object_out=new ObjectOutputStream(o);
object_out.writeObject(hashtable);
object_out.close();
o.close();
out.print("<BR>"+"您是第一个录入货物的人");
out.print("<BR>"+"货物的货号:"+name);
}
catch(Exception eee)
{ }
}
%>
<FORM action="showgoods.jsp" method=post >
<P>查看库存情况
<BR> <INPUT type=submit value="去查看库存">
</FORM>
<A href="Ex4_11.jsp"><BR>返回录入页面
</BODY>
</HTML>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -