1.继承HttpServlet类,导入相应包
2.防止乱码,在doPost()方法中写上
req.setCharacterEncoding("utf-8");
resp.setContentType("text/html;charset=utf-8");
3.menu(),根据tag选择相应的操作
public void menu(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException,Exception {
String tag=req.getParameter("tag");
switch(tag)
{
case "ADD": this.add(req, resp);
break;
case "FINDBIND": this.findidbind(req, resp);
break;
case "REMOVE": this.remove(req, resp);
break;
case "UPDATE": this.update(req, resp);
break;
case "FINDALL": this.findall(req, resp);
break;
}
}
4.add()方法
a. 在manage.jsp页面中,做链接
<a href="categoryadd.jsp" target="midframe">信息增加</a>
b. add.jsp页面中做跳转,交给servlet处理
<form action="categorylet" method="post">
<input type="text" name="category" >
<select name="discount">
<option value="0.90">9折</option>
<option value="0.80">8折</option>
<option value="0.75">7.5折</option>
</select>
<input type="submit" value="保 存">
<input type="reset" value="取 消">
<input type="hidden" name="tag" value="ADD">
</form>
c. 在servlet中根据tag值,调用add()方法
public void add(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException,Exception {
Category c1=new Category();
c1.setCategory(req.getParameter("category"));
c1.setDiscount(Double.parseDouble(req.getParameter("discount")));
if(Factory.getCategoryInstence().add(c1))
{
req.getRequestDispatcher("categorylet?tag=FINDALL&ch=&keyword=").forward(req, resp);
}
else
{
System.out.println("增加失败");
}
}
5 findall()方法
a. 给信息列表做链接,交给servlet处理
<a href="categorylet?ch=<%=""%>&keyword=<%="" %>&tag=<%="FINDALL"%>" target="midframe">信息列表</a>
b.servlet中findall()方法
{
List<Category> all= new ArrayList<Category>();//定义一组对象集合,用于接收查询到的对象集
String tag=req.getParameter("ch");
String keyword=req.getParameter("keyword");
all=Factory.getCategoryInstence().findALL(tag, keyword);//执行查询操作
req.setAttribute("info", all); //把查询出来的数据放到属性对象中
req.getRequestDispatcher("categorylist.jsp").forward(req, resp);
}
C. 在categorylist.jsp页面中
<%
List<Category> all =(List<Category>)request.getAttribute("info");
Iterator<Category> iter=all.iterator();
Category c1=null;
%>
<table border="1" width="80%">
<tr align="center">
<td>编号</td>
<td>会员类别名称</td>
<td>折扣标准</td>
<td colspan="2">数据操作</td>
</tr>
<%
while(iter.hasNext())
{
c1=new Category();
c1=iter.next();
%>
<tr align="center">
<td><%=c1.getId() %></td>
<td><%=c1.getCategory() %></td>
<td><%=c1.getDiscount() %></td>
<td><a href="categorylet?tag=<%="FINDBIND"%>&id=<%=c1.getId()%>">修改</a></td>
<td><a href="categorylet?tag=<%="REMOVE"%>&id=<%=c1.getId()%>">删除</a></td>
</tr>
<%
}
%>
6.update()方法
a. 在categorylist.jsp页面中做链接,交给servlet处理
<a href="categorylet?tag=<%="FINDBIND"%>&id=<%=c1.getId()%>">修改</a>
b. servlet中,findidbind找到要修改的对象,跳转到修改页面
{
Category c1=new Category();//定义一个对象为要修改的对象
Category c2=new Category();//定义一个对象用于接收修改后数据
Integer id=Integer.parseInt(req.getParameter("id"));
c1.setId(id);
c2=Factory.getCategoryInstence().findID(c1);//执行修改操作
req.setAttribute("name", c2);
req.getRequestDispatcher("categoryupdate.jsp").forward(req, resp);
}
C. 在update.jsp中,做跳转交给servlet处理
<%
Category c1=new Category();
c1=(Category)request.getAttribute("name");
%>
<form action="categorylet" method="post">
<input type="text" name="id" value="<%=c1.getId() %>" readonly>
<input type="text" name="category" value="<%=c1.getCategory() %>" >
<select name="discount">
<option value="0.90">9折</option>
<option value="0.80">8折</option>
<option value="0.75">7.5折</option>
</select>
<input type="submit" value="保 存">
<input type="reset" value="取 消">
<input type="hidden" name="tag" value="UPDATE">
</form>
d.在servlet中根据tag标签,调用update()方法
public void update(HttpServletRequest req, HttpServletResponse resp) {
Category c1=new Category();
c1.setId(Integer.parseInt(req.getParameter("id")));
c1.setCategory(req.getParameter("category"));
c1.setDiscount(Double.parseDouble(req.getParameter("discount")));
if(Factory.getCategoryInstence().update(c1))
{
req.getRequestDispatcher("categorylet?tag=FINDALL&ch=&keyword=").forward(req, resp);
}
}