Servlet 商品留言


商品留言效果如圖,實現某個商品留言的添加,刪除,顯示功能。


wKioL1j0LbXS9N1GAABEuQtFQm0749.jpg-wh_50

1、數據庫設計

wKiom1j0LTKSDHByAABaq_xFHwY796.jpg-wh_50

2、Javabean設計--Comment.java


package myshop;

public class Comment {
 private int id;
 private int gid;
 private String content;
 private String add_time;
 private String add_user;
 public int getId() {
  return id;
 }
 public void setId(int id) {
  this.id = id;
 }
 public int getGid() {
  return gid;
 }
 public void setGid(int gid) {
  this.gid = gid;
 }
 public String getContent() {
  return content;
 }
 public void setContent(String content) {
  this.content = content;
 }
 public String getAdd_time() {
  String add_time=this.add_time.substring(0, 10);
  
  return add_time;
 }
 public void setAdd_time(String add_time) {
  this.add_time = add_time;
 }
 public String getAdd_user() {
  return add_user;
 }
 public void setAdd_user(String add_user) {
  this.add_user = add_user;
 }
 

}


3、DAO設計--CommentDAO.java

package myshop;

import java.sql.*;
import java.util.*;

public class CommentDAO {

  public LinkedList<Comment> search(int gid){
   LinkedList<Comment> cs=new LinkedList<Comment>();
   Connection conn=JDBC.getConnection();
   String sql="select * from tb_comment where gid=?";
   ResultSet rs=null;
   try {
   PreparedStatement ps=conn.prepareStatement(sql);
   ps.setInt(1, gid);
   rs=ps.executeQuery();
   while(rs.next()){
    Comment c=new Comment();
    c.setId(rs.getInt("id"));
    c.setGid(rs.getInt("gid"));
    c.setContent(rs.getString("content"));
    c.setAdd_user(rs.getString("add_user"));
    c.setAdd_time(rs.getString("add_time"));
    cs.add(c);
   }
  } catch (SQLException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
  

  
   return cs;
  
  }
 
 public boolean add(Comment comment){
  boolean flag=false;
  
  
  Connection conn=JDBC.getConnection();
  PreparedStatement ps=null;
  String sql="insert into tb_comment(gid,content,add_user) "
    + "values(?,?,?)";
  //1
  try {
   ps=conn.prepareStatement(sql);
   //2

   ps.setInt(1, comment.getGid());
         ps.setString(2, comment.getContent());
         ps.setString(3, comment.getAdd_user());
   
   int n=ps.executeUpdate();
   if(n>0){
    flag=true;
   }
  } catch (SQLException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
  
  return flag;
 }
 
 public boolean delete(int id,String username){
  boolean flag=false;
 
  return flag;

  
 }
 public boolean delete(int id){
  boolean flag=false;
  
  Connection conn=JDBC.getConnection();//1
  String sql="delete tb_comment where id=?";
  PreparedStatement ps=null;
  try {
   ps=conn.prepareStatement(sql);//2
   ps.setInt(1, id);
   int n=ps.executeUpdate();  //3
   if(n>0)
   {
    flag=true;
   }
   
  } catch (SQLException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }finally{
   
   try{
    
    if(ps!=null){
     ps.close();
    }
    if(conn!=null){
     conn.close();
    }
   }catch(Exception e){
   
  }
  }
  
  return flag;
  
 }
}


4、Servlet實現處理商品留言功能,主要完成
    (1)是添加還是刪除留言?
    (2)如果爲刪除,需要獲得要刪除留言的id,調用CommentDAO.delete(id),
         返回detail.jsp頁(要提交商品gid,否則會異常)
    (3)添加留言
         Servlet獲得商品留言的信息content,作者(登錄用戶),商品gid
         調用CommentDAO.add(comment)
         返回detail.jsp頁(要提交商品gid,否則會異常)
   

package myservlet;

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;

import myshop.Comment;
import myshop.CommentDAO;

public class CommentServlet extends HttpServlet {
 protected void doPost(HttpServletRequest request, HttpServletResponse response)
   throws ServletException, IOException {

  this.doGet(request, response);
 }
 
 public void doGet(HttpServletRequest request, HttpServletResponse response)
   throws ServletException, IOException {
 
  String action=request.getParameter("action");
  CommentDAO dao=new CommentDAO();
  
  if(action.equals("add")){

   
   Comment comment=new Comment();
   comment.setGid(Integer.parseInt(request.getParameter("gid")));
   comment.setContent(request.getParameter("content"));
   String add_user="無名氏";
   comment.setAdd_user(add_user);
   dao.add(comment);
   
   
   request.getSession().setAttribute("gid",request.getParameter("gid"));
   response.sendRedirect("/chap5/detail.jsp");

   
  }else if(action.equals("delete")){
   int id=Integer.parseInt(request.getParameter("id"));
   dao.delete(id);
   
   request.getSession().setAttribute("gid",request.getParameter("gid"));
   response.sendRedirect("/chap5/detail.jsp");

  
  
  }
 
 }

 

}

  

web.xml

<servlet>
    <servlet-name>CommentServlet</servlet-name>
    <servlet-class>myservlet.CommentServlet</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>CommentServlet</servlet-name>
    <url-pattern>/servlet/comment</url-pattern>
  </servlet-mapping>

   
5、detail.jsp實現留言顯示

 <h2>評價</h2>
    
     <%

 CommentDAO cdao=new CommentDAO();
 LinkedList<Comment> cs=cdao.search(gid);

for(Comment c:cs){
      %>
      <div class="msg">
        <div class="msg_content"><%=c.getContent()%></div>
           <div class="msg_time"><%=c.getAdd_time() %>  <a href="/chap5/servlet/comment?action=delete&id=<%=c.getId()%>&gid=<%=gid%>">刪除</a></div>
         
   </div>
   <%} %>
6、實現商品留言刪除

<a href="/chap5/servlet/comment?action=delete&id=<%=c.getId()%>&gid=<%=gid%>">刪除</a>

紅色部分,另外detail.jsp部分代碼更改,Servlet刪除留言後,返回detail.jsp頁需商品gid值

<%
 int gid;
 if(session.getAttribute("gid")!=null){
   gid=Integer.parseInt((String)session.getAttribute("gid"));
 }else{
 
 gid= Integer.parseInt(request.getParameter("gid"));
 }

 GoodsDAO dao=new GoodsDAO();
 Goods g=dao.getById(gid);

%>



7、實現商品留言

detail.jsp

<div class="msg">
   <form action="/chap5/servlet/comment?action=add&gid=<%=gid%>" method="post">
            <textarea class="msg_txt" name="content" ></textarea>
            <input type="submit" value="發表">
            </form>
   </div>



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章