首页 > 代码库 > Servlet 商品留言

Servlet 商品留言


商品留言效果如图,实现某个商品留言的添加,删除,显示功能。


技术分享

1、数据库设计

技术分享

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="http://www.mamicode.com/chap5/servlet/comment?action=delete&id=&gid=">删除</a></div>
         
   </div>
   <%} %>
6、实现商品留言删除

<a href="http://www.mamicode.com/chap5/servlet/comment?action=delete&id=&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="http://www.mamicode.com/发表">
            </form>
   </div>



本文出自 “学而不思则罔” 博客,请务必保留此出处http://dyzyxy.blog.51cto.com/944775/1916593

Servlet 商品留言