parent
a9a7015274
commit
43d042a468
@ -0,0 +1,144 @@
|
||||
package com.demo;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
import java.sql.Connection;
|
||||
import java.sql.DriverManager;
|
||||
import java.sql.PreparedStatement;
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
import java.util.*;
|
||||
|
||||
import javax.servlet.RequestDispatcher;
|
||||
import javax.servlet.ServletException;
|
||||
import javax.servlet.annotation.WebServlet;
|
||||
import javax.servlet.http.HttpServlet;
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
|
||||
import com.dao.Dao;
|
||||
import com.dao.ArticleDao;
|
||||
import com.dao.ArticleDaoImpl;
|
||||
import com.domain.Article;
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Servlet implementation class PostCheckServlet
|
||||
*/
|
||||
@WebServlet(name ="ArticleServlet",urlPatterns={"/Article.do"})
|
||||
public class ArticleServlet extends HttpServlet implements Dao {
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
// public void init() {
|
||||
// String driver = "com.mysql.cj.jdbc.Driver";
|
||||
// String dburl = "jdbc:mysql://127.0.0.1:3306/tooth?useSSL=true";
|
||||
// String username = "root";
|
||||
// String password = "root";
|
||||
// try{
|
||||
// Class.forName(driver); // 加载驱动程序
|
||||
// // 创建连接对象
|
||||
// dbconn = DriverManager.getConnection(
|
||||
// dburl,username,password);
|
||||
// }catch(ClassNotFoundException e1){
|
||||
// System.out.println(e1);
|
||||
// getServletContext().log("驱动程序类找不到!");
|
||||
// }catch(SQLException e2){
|
||||
// System.out.println(e2);
|
||||
// }
|
||||
// }
|
||||
// java.sql.Connection dbconn = null; public void init() { String driver =
|
||||
// "com.mysql.jdbc.Driver"; String dburl =
|
||||
// "jdbc:mysql://127.0.0.1:3306/xinxin?useSSL=true"; String username = "root";
|
||||
// String password = "zhao157"; try{ Class.forName(driver); // 加载驱动程序 // 创建连接对象
|
||||
// dbconn = DriverManager.getConnection(dburl,username,password);
|
||||
// }catch(ClassNotFoundException e1){ System.out.println(e1);
|
||||
// getServletContext().log("驱动程序类找不到!"); }catch(SQLException e2){
|
||||
// System.out.println(e2); } }
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
|
||||
*/
|
||||
public void doGet(HttpServletRequest request,
|
||||
HttpServletResponse response)
|
||||
throws ServletException,IOException{
|
||||
doPost(request,response);
|
||||
|
||||
}
|
||||
// public void doGet(HttpServletRequest request,
|
||||
// HttpServletResponse response)
|
||||
// throws ServletException,IOException{
|
||||
//
|
||||
//
|
||||
//ArrayList<Dynamic> dynamicList = null;
|
||||
//dynamicList = new ArrayList<Dynamic>();
|
||||
//try{
|
||||
// String sql="SELECT * FROM Dynamic";
|
||||
// PreparedStatement pstmt = dbconn.prepareStatement(sql);
|
||||
//ResultSet rst = pstmt.executeQuery();
|
||||
//while(rst.next()){
|
||||
//Dynamic s = new Dynamic();
|
||||
//s.setDyid(rst.getInt("dyid"));
|
||||
// s.setUid(rst.getString("uid"));
|
||||
// s.setUname(rst.getString("uname"));
|
||||
// s.setContent(rst.getString("content"));
|
||||
// s.setRtime(rst.getDate("rtime"));
|
||||
//
|
||||
//dynamicList.add(s);
|
||||
//}
|
||||
//if(!dynamicList.isEmpty()){
|
||||
// request.getSession().setAttribute("DynamicList",dynamicList);
|
||||
// response.sendRedirect("da-dynamic.jsp");
|
||||
//}else{
|
||||
//response.sendRedirect("error.jsp");
|
||||
//}
|
||||
//}catch(SQLException e){
|
||||
//e.printStackTrace();
|
||||
//}
|
||||
// }
|
||||
|
||||
|
||||
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
|
||||
// TODO Auto-generated method stub
|
||||
String action = request.getParameter("action");
|
||||
if(action!=null&&action.equals("remove")){
|
||||
removeArticle(request,response);
|
||||
} else if (action.equals("list")){
|
||||
listArticle(request,response);
|
||||
}else {
|
||||
System.out.println("失败");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private void listArticle(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
|
||||
// TODO Auto-generated method stub
|
||||
|
||||
ArticleDao dao = new ArticleDaoImpl();
|
||||
try{
|
||||
|
||||
List<Article> list= dao.listArticle();
|
||||
request.setAttribute("ArticleList",list);
|
||||
|
||||
}catch(SQLException e) {}
|
||||
RequestDispatcher rd = request.getRequestDispatcher("da-article.jsp");
|
||||
|
||||
rd.forward(request, response);
|
||||
|
||||
}
|
||||
|
||||
|
||||
private void removeArticle(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
|
||||
// TODO Auto-generated method stub
|
||||
int Aid=Integer.parseInt(request.getParameter("Aid"));
|
||||
ArticleDao dao=new ArticleDaoImpl();
|
||||
try {
|
||||
int success=dao.removeArticle(Aid);
|
||||
if(success>0) {
|
||||
listArticle(request,response);
|
||||
}
|
||||
}catch(SQLException e) {}
|
||||
}
|
||||
}
|
Loading…
Reference in new issue