这个代码大家可以拿去研究下,这个是我在开发过程中的增加资料时候的一段代码,我觉得很经。。。
public class SysmanServlet extends HttpServlet
{
public void doGet(HttpServletRequest request ,HttpServletResponse response)throws IOException, ServletException
{
SysmanBean sbean = null;
try
{
int errcode = 0;
DealString ds = new DealString();
String strType = ds.toString((String)request.getParameter("txt_type"));//取得菜单类别
String strID = ds.toString((String)request.getParameter("txt_id"));//编号
String strDict = ds.toGBK(ds.toString((String)request.getParameter("txt_dict")));//字典名称
String strEdit = ds.toString((String)request.getParameter("txt_edit"));//增删改类型
//建立实例
int ntype = Integer.parseInt(strType);
sbean = new SysmanBean();
sbean.setType(ntype);
sbean.setID(strID);
sbean.setDictname(strDict);
//删除记录时
if(strEdit.equals("3"))
{
errcode = sbean.delData();
if(ntype>10)
strType = ""+(ntype-10);
}
if(strEdit.equals("4"))
{
errcode = sbean.revert();
}
else
{
if(strType.equals("1"))//字典维护
{
//取得表单参数
String text_zdmc = ds.toGBK(ds.toString((String)request.getParameter("text_zdmc")));
String text_xmbh = ds.toString((String)request.getParameter("text_xmbh"));
String text_xmmc = ds.toGBK(ds.toString((String)request.getParameter("text_xmmc")));
String text_syzt = ds.toString((String)request.getParameter("text_syzt"));
String text_jbxh = ds.toString((String)request.getParameter("text_jbxh"));
//生成参数集
Hashtable hash = new Hashtable();
hash.put("ZDMC",text_zdmc);
hash.put("XMBH",text_xmbh);
hash.put("XMMC",text_xmmc);
hash.put("SYZT",text_syzt);
hash.put("JBXH",text_jbxh);
if(strEdit.equals("1"))//增加记录
{
// sbean.setID(text_xmbh);
errcode = sbean.addDict(hash);
}
else if(strEdit.equals("2"))//修改记录
{
sbean.setID(text_xmbh);
sbean.setDictname(text_zdmc);
errcode = sbean.modDict(hash);
}
}
}
//response.setContentType("text/html");
//PrintWriter out = response.getWriter();
String go="oa/sysman/index.jsp?txt_type="+strType+"&errcode="+errcode;
response.sendRedirect(go);
}
catch(Exception e){System.out.println("SysmanServlet处理时出错;错误为:"+e);}
finally{
if(sbean!=null)sbean.closeConn();
}
}
public void doPost(HttpServletRequest request ,HttpServletResponse response)throws IOException, ServletException
{
doGet(request,response);
}
}