1. Integrate database connection operations
Based on previous experiences in the news publishing system, it is best to write a class dbhelper or sqlhelper for the content related to database connection and so on. Through various static methods in this class, you can easily connect to the database, you do not need to write the same operation again when you need it. For example:
The code for finding the blog to be modified before the blog is modified:
public void doGet(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {String blogid = request.getParameter("blogid");DataSource ds = null;try {Context context = new InitialContext();ds = (DataSource) context.lookup("java:comp/env/jdbc/mysqlds");QueryRunner qr = new QueryRunner(ds);String sql = "SELECT id,title,content,categoryid from blog where id="+ blogid;List<Blog> bloglist = (List<Blog>) qr.query(sql,new BeanListHandler(Blog.class));Blog blog = bloglist.get(0);request.setAttribute("blog", blog);request.getRequestDispatcher("/ModifyBlog.jsp").forward(request,response);} catch (Exception e) {e.printStackTrace();}}
The red part is the part that connects to the database and obtains the queryrunner object of dbutils. This part is used in many Servlets, so it can be integrated into a class and then called.
Public class dbhelper {public static queryrunner getqueryrunner () {queryrunner QR = NULL; try {// in context. set the name of the data source object in XML to obtain the data source object context = new initialcontext (); datasource DS = (datasource) context. lookup ("Java: COMP/ENV/jdbc/mysqlds"); // Note: The first word is Java, not JDBC! // Core class of dbutils, which is passed to the data source object during generation so that it can generate the database connection object by itself, and put it back to the data pool QR = new queryrunner (DS);} catch (exception e) {e. printstacktrace () ;}return QR ;}}
Call:
public void doGet(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {String blogid=request.getParameter("blogid");QueryRunner qr=Dbhelper.getQueryRunner();String sql="SELECT id,title,content,created_time as createTime,categoryid from blog where id="+blogid;try {List<Blog> bloglist=(List<Blog>)qr.query(sql, new BeanListHandler(Blog.class));Blog blog=bloglist.get(0);request.setAttribute("blog", blog);} catch (Exception e) {e.printStackTrace();}request.getRequestDispatcher("/DisplayBlog.jsp").forward(request, response);}
2. JavaScript to delete the prompt
According to common sense, before deleting a blog post, you must first determine whether to delete the post to avoid accidental deletion. This is implemented using JavaScript.
Just add an onclick = "Return confirm ('Do you want to delete this blog? ')"
For example:
<A href = "http: // localhost: 8080/blog/servlet/deleteblogservlet? Blogid = <% = blog. GETID () %> "onclick =" Return confirm ('Do you want to delete this blog? ') "> Delete </a>
There is another way: in fact, it is similar
First, write a javascript code:
<script type="text/javascript" language="javascript"> function delblog(){ var message="Do you want to delete this blog?"; if(confirm(massage)==true){ return true; }else{ return false; } } </script>
Then add onclick = "javascript: Return delblog ()" to the deletion, but I didn't succeed. hehahaha, I don't know why! You have to use the first method.