⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 commentservlet.java

📁 有发布
💻 JAVA
字号:
package servlet;

import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.util.*;
import DB.*;
import bean.*;

public class commentServlet 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 {
        response.setContentType(CONTENT_TYPE);
        request.setCharacterEncoding("GBK");

        String type = formatStr(request.getParameter("type"));
        commentDAO cot = new commentDAO();

        try {

            if (type.equals("add")) {
                commentBean cbean = new commentBean();
                //获得页面输入的值
                String name = new String(formatStr(request.getParameter(
                        "name")));
                String count = new String(formatStr(request.getParameter(
                        "count")));
                String stars =request.getParameter("stars");

                cbean.setName(name);
                cbean.setComment(count);
                cbean.setStars(stars);

                //增加动作

                boolean host = false;
                host = cot.instercomment(cbean);
                //判断添加是否成功
                if (host == true) {
                    ArrayList list = (ArrayList) cot.Selectcomment();
                    request.setAttribute("list", list);
                    javax.servlet.RequestDispatcher dis = request.
                            getRequestDispatcher(
                                    "main/newsindex.jsp");
                    dis.forward(request, response);
                }

            }
            if (type.equals("del")) {

                commentBean cbean = new commentBean();
                //用循环获得被选中项的 id 号
                String newsid[] = request.getParameterValues("check");
                boolean flat = true;
               for (int i = 0; i < newsid.length; i++) {
                    cbean.setID(newsid[i]);
                    if(!cot.delcomment(cbean))
                    {
                        flat=false;
                        break;
                    }
                }

                //判断数据库的执行是否正确
                if (flat == true) {
                    ArrayList list = (ArrayList) cot.Selectcomment();
                    request.setAttribute("list", list);
                    RequestDispatcher dis = request.getRequestDispatcher(
                            "main/newscomment.jsp");
                    dis.forward(request, response);

                }
            }
            if (type.equals("pinglun")) {

                    //显示评论的列表
                    ArrayList list = (ArrayList) cot.Selectcomment();
                    request.setAttribute("list", list);
                    RequestDispatcher dis = request.getRequestDispatcher(
                            "main/newscomment.jsp");
                    dis.forward(request, response);
                }
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    //Clean up resources
    public void destroy() {
    }

    public void doPost(HttpServletRequest request, HttpServletResponse response) throws
            ServletException, IOException {
        doGet(request, response);
    }


    //格式化获取参数
    public String formatStr(String str) {
        return str == null ? "" : str;
    }

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -