package bdqn.newsManageServlet.Servlet;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import bdqn.newsManageServlet.Dao.categoryTBDao;
import bdqn.newsManageServlet.Dao.Impl.categoryTBDaoImpl;
import bdqn.newsManageServlet.entity.categoryTB;
public class doCategoryModifyServlet extends HttpServlet {
/**
* Destruction of the servlet. <br>
*/
public void destroy() {
super.destroy(); // Just puts "destroy" string in log
// Put your code here
}
/**
* The doGet method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to get.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=utf-8");
PrintWriter out = response.getWriter();
//获取id
request.setCharacterEncoding("utf-8");
String name=request.getParameter("categoryName");
int id=0;
if(request.getParameter("cgid")!=null){
id=Integer.parseInt(request.getParameter("cgid"));
}
//int id=Integer.parseInt(request.getParameter("cgid"));
//调用访问数据称
categoryTBDao cdao=new categoryTBDaoImpl();
//1.判断类别是否重复(在数据库中查询)
//2.如果有重复,提示错误信息,转发到添加
if(cdao.getCateByName(name)!=null){
request.setAttribute("ermsg", "类别名已存在!");
request.getRequestDispatcher("adminManage/modifyCategory.jsp").forward(request, response);
return;
}
//在数据库中修改
categoryTB cate=new categoryTB();
cate.setCategoryName(name);
cate.setCategoryID(id);
int rel=cdao.updateCategory(cate);
if(rel>0){
//修改成功
out.print("<script>alert('修改成功');location.href='adminManage/manageTopic.jsp';</script>");
}else{
request.setAttribute("ermsg", "修改失败");
request.getRequestDispatcher("adminManage/modifyCategory.jsp").forward(request, response);
return;
}
}
/**
* The doPost method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to post.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
/**
* Initialization of the servlet. <br>
*
* @throws ServletException if an error occurs
*/
public void init() throws ServletException {
// Put your code here
}
}