servetlet+html在jb9中求救(急急急)
8484 2003-10-09 05:37:31 小第写第一个servlet,请大家帮帮忙
package showbuyservet;
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.util.*;
public class ShowBuy extends HttpServlet {
private static final String CONTENT_TYPE = "text/html; charset=GBK";
//Initialize global variables
public void init() throws ServletException {
}
//Process the HTTP Get request
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String[] item={"搪果","收音机","练习簿"};
//获取会话对象
HttpSession session=request.getSession(true);
//选取选择的商品数目
Integer itemCount=(Integer)session.getValue("itemCount");
//如果没有放入商品则数目为0
if(itemCount==null)
{
itemCount=new Integer(0);
}
response.setContentType(CONTENT_TYPE);
PrintWriter out = response.getWriter();
String itemsSelected[];
String itemName;
itemsSelected=request.getParameterValues("item");
//将选中的商品入和会话对象中
if(itemsSelected!=null)
{
for(int i=0;i<itemsSelected.length;i++)
{
itemName=itemsSelected[i];
itemCount=new Integer(itemCount.intValue()+1);
session.putValue("item"+itemCount,itemName);
//将商品名称定义为Itemx
session.putValue("intemCount",itemCount);
//将商品数量放入会话对象中
}
}
out.println("<html>");
out.println("<head><title>购物的内容</title></head>");
out.println("<body bgcolor=\"#008080\">");
//out.println("<p>The servlet has received a GET. This is the reply.</p>");
out.println("<center><h1>放入在购物袋中的商品是:</h1></center>");
//将购物袋的内容写入页面
for(int i=1;i<itemCount.intValue();i++)
{
String intem=(String)session.getValue("Intem"+i);
//取出商品名称
out.println(item[Integer.parseInt(intem)]);
out.println("<br>");
}
out.println("</body></html>");
out.close();
}
//Clean up resources
public void destroy() {
}
}
在jb9中编译出错:
"ShowBuy.java": putValue(java.lang.String,java.lang.Object) in javax.servlet.http.HttpSession has been deprecated at line 37, column 18
"ShowBuy.java": putValue(java.lang.String,java.lang.Object) in javax.servlet.http.HttpSession has been deprecated at line 39, column 18
"ShowBuy.java": getValue(java.lang.String) in javax.servlet.http.HttpSession has been deprecated at line 51, column 34
There have been deprecation warnings. Please consult the documentation for a better alternative.
另外写了一个html页:加到jb9中,运行真接就是:放入在购物袋中的商品是:
并没有我那个html一开始的效果
<html>
<head>
<title>购物袋我实例</title>
</head>
<body>
<center><h1>百货商场</h1></center>
<hr>
<form action="servlet/ShowBuy" method="post">
选购商品
<p><input type="checkbox" name="item" value="0">
第一种:糖果</p>
<p><input type="checkbox" name="item" value="1">
第二种:收音机</p>
<p><input type="checkbox" name="item" value="2">
第三种:练习簿</p>
</hr>
<input type="submit" name="bt_submit" value="加入购物袋">
</form>
</body>
</html>