📄
字号:
</HTML>
例子10(效果如图4.11、4.12所示)
Example4_10.jsp:
<%@ page contentType="text/html;charset=GB2312" %>
<%@ page import ="java.io.*" %>
<HTML>
<BODY>
<P> 在下面的表格输入成绩:
<FORM action="Example4_10.jsp" method=post name=form>
<Table align="CENTER" Border>
<TR>
<TH width=50> 姓名</TH>
<TH width=50> 数学</TH>
<TH width=50>英语</TH>
</TR>
<% int i=0;
while(i<=6)
{out.print("<TR>");
out.print("<TD>");
out.print("<INPUT type=text name=name value=姓名>");
out.print("</TD>");
out.print("<TD>");
out.print("<INPUT type=text name=math value=0>");
out.print("</TD>");
out.print("<TD>");
out.print("<INPUT type=text name=english value=0>");
out.print("</TD>");
out.print("</TR>");
i++;
}
%>
<TR>
<TD>
<INPUT type=submit name="g" value="写入成绩" >
</TD>
<TD> Math</TD>
<TD> English</TD>
</TR>
</Table>
</FORM>
<%
String name[]=request.getParameterValues("name");
String math[]=request.getParameterValues("math");
String english[]=request.getParameterValues("english");
try{ File f=new File("f:/2000","student.txt");
FileOutputStream o=new FileOutputStream(f);
DataOutputStream DataOut=new DataOutputStream(o);
for(int k=0;k<name.length;k++)
{DataOut.writeUTF(name[k]);
DataOut.writeUTF(math[k]);
DataOut.writeUTF(english[k]);
}
DataOut.close();
o.close();
}
catch(IOException e)
{ }
catch(NullPointerException ee)
{ }
%>
<P><BR>查看成绩单:
<A href=showresult.jsp><BR> 连接到成绩单页面>
</BODY>
</HTML>
showresult.jsp:
<%@ page contentType="text/html;charset=GB2312" %>
<%@ page import ="java.io.*" %>
<HTML>
<BODY>
<P>成绩单:
<% try{ File f=new File("f:/2000","student.txt");
FileInputStream in=new FileInputStream(f);
DataInputStream DataIn=new DataInputStream(in);
String name="ok";
String math="0",english="0";
out.print("<Table Border>");
out.print("<TR>");
out.print("<TH width=50> 姓名</TH>");
out.print("<TH width=50> 数学</TH>");
out.print("<TH width=50>英语</TH>");
out.print("</TR>");
while((name=DataIn.readUTF())!=null)
{ byte bb[]=name.getBytes("ISO-8859-1");
name=new String(bb);
math=DataIn.readUTF();
english=DataIn.readUTF();
out.print("<TR>");
out.print("<TD width=200>");
out.print(name);
out.print("</TD>");
out.print("<TD width=100>");
out.print(math);
out.print("</TD>");
out.print("<TD width=100>");
out.print(english);
out.print("</TD>");
out.print("</TR>");
}
out.print("</Table>");
DataIn.close(); in.close();
}
catch(IOException ee)
{ }
%>
</BODY>
</HTML>
例子11(效果如图4.13、4.14所示)
Example4_11.jsp:
<%@ page contentType="text/html;charset=GB2312" %>
<%@ page import ="java.io.*" %>
<%@ page import ="java.util.*" %>
<HTML>
<BODY>
<P> 输入货物有关信息:
<FORM action="input.jsp" method=post >
<P>货号:
<INPUT type=text name="N">
<P>数量:
<INPUT type=text name="M">
<BR>
<INPUT type=submit value="提交">
</FORM>
<FORM action="showgoods.jsp" method=post >
<P>查看库存情况
<BR>
<INPUT type=submit value="去查看库存">
</FORM>
</BODY>
</HTML>
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("F:/2000","goods_name.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="Example4_11.jsp"><BR>返回录入页面
</BODY>
</HTML>
showgoods.jsp:
<%@ page contentType="text/html;charset=GB2312" %>
<%@ page import ="java.io.*" %>
<%@ page import ="java.util.*" %>
<HTML>
<BODY>
<P> 已有货物的有关信息:
<%
try{File f=new File("F:/2000","goods_name.txt");
FileInputStream in=new FileInputStream(f);
ObjectInputStream object_in=new ObjectInputStream(in);
Hashtable hashtable=(Hashtable)object_in.readObject();
object_in.close();
in.close();
Enumeration enum=hashtable.elements();
while(enum.hasMoreElements()) //遍历当前散列表。
{ String goods=(String)enum.nextElement();
out.print("<BR>"+"货号及库存:"+goods);
}
hashtable.clear();
}
catch(ClassNotFoundException event)
{ out.println("无法读出");
}
catch(IOException event)
{out.println("无法读出");
}
%>
<A href="Example4_11.jsp"><BR>返回录入页面
</BODY>
</HTML>
例子12(效果如图4.15、4.16、4.17所示)
Example4_12.jsp:
<%@ page contentType="text/html;charset=GB2312" %>
<%@ page import ="java.io.*" %>
<HTML>
<BODY bgcolor=cyan><Font size=1>
<% String str=response.encodeURL("continueWrite.jsp");
%>
<P>选择您想续写小说的名字:<BR>
<FORM action="<%=str%>" method=post name=form>
<BR><INPUT type="radio" name="R" value="spring.doc" >美丽的故事
<BR><INPUT type="radio" name="R" value="summer.doc" >火热的夏天
<BR><INPUT type="radio" name="R" value="autumn.doc" >秋天的收获
<BR><INPUT type="radio" name="R" value="winter.doc" >冬天的大雪
<BR> <INPUT type=submit name ="g" value="提交">
</FORM>
</BODY>
</HTML>
continueWrite.jsp:
<%@ page contentType="text/html;charset=GB2312" %>
<%@ page import ="java.io.*" %>
<HTML>
<BODY bgcolor=cyan><Font size=1>
<P>小说已有内容:
<Font size=1 Color=Navy>
<%String str=response.encodeURL("continue.jsp");
%>
<%--获取客户提交的小说的名字--%>
<%String name=(String)request.getParameter("R");
if(name==null)
{name="";
}
byte c[]=name.getBytes("ISO-8859-1");
name=new String(c);
session.setAttribute("name",name);
File storyFileDir=new File("F:/2000","story");
storyFileDir.mkdir();
File f=new File(storyFileDir,name);
//列出小说的内容:
try{ RandomAccessFile file=
new RandomAccessFile(f,"r");
String temp=null;
while((temp=file.readUTF())!=null)
{ byte d[]=temp.getBytes("ISO-8859-1");
temp=new String(d);
out.print("<BR>"+temp);
}
file.close();
}
catch(IOException e){}
%>
</Font>
<P>请输入续写的新内容:
<Form action="<%=str%>" method=post name=form>
<TEXTAREA name="messages" ROWs="12" COLS=80 WRAP="physical">
</TEXTAREA>
<BR>
<INPUT type="submit" value="提交信息" name="submit">
</FORM>
continue.jsp:
<%@ page contentType="text/html;charset=GB2312" %>
<%@ page import ="java.io.*" %>
<%@ page import ="java.util.*" %>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -