主页上4个Tab的功能基本就已经完善了,接下来准备实现跳转详情页的功能。在此之前,明确一下详情页将会实现的功能,优先处理后台代码
- 根据内容ID查找到与内容相关的所有信息——内容简述、内容详情、内容作者、点赞数、内容类型
- 点赞——点击后,点赞数加一
- 用户是否已经收藏该内容——通过传过来的用户昵称和文章内容ID判断用户是否已经收藏该内容
- 取消收藏——从收藏表中将对应的收藏行删除
- 添加到我的收藏——将用户和内容信息插入到收藏表中
- 评论功能——用户可以评论
- 删除内容——当作者就是用户时,可以删除内容,且在删除文章时对应删除收藏表的对应行以及评论表对应的内容
1.明确上述几个后台处理逻辑之后,首先要在数据库ishare中新建一张收藏表——focus,还有1张评论表——comment
- 和之前的用户表、内容表一样,在数据库ishare中新建一张表,命名为focus具体的表结构如下:
- focus_id为int类型,自增;username对应收藏的用户;info_id对应收藏的内容
- 在ishare中新建一张表,命名为comment,具体表结构设计如下:
- comment_id是int类型,自增;comment_user是评论者;comment_info是评论文章的id;comment_detail是评论的具体内容
2. 建好表之后就开始处理通过Id查找内容的servlet文件QueryInfoById.java
- 和之前新建servlet一样,在myServlet.data包下新建servlet命名为QueryInfoById
- 在web.xml中添加配置:
<servlet>
<servlet-name>QueryInfoById</servlet-name>
<servlet-class>myServlet.data.QueryInfoById</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>QueryInfoById</servlet-name>
<url-pattern>/servlet/QueryInfoById</url-pattern>
</servlet-mapping>
- 具体实现也是和之前查询类似,将数据库返回的内容封装成JsonArray返回,具体代码如下
package myServlet.data;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
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 org.utils.JdbcUtils;
import org.utils.JsonUtils;
import com.sun.rowset.CachedRowSetImpl;
import net.sf.json.JSONArray;
/**
* Servlet implementation class QueryInfoById
*/
@WebServlet("/QueryInfoById")
public class QueryInfoById extends HttpServlet {
private static final long serialVersionUID = 1L;
CachedRowSetImpl rowSet = null; //存储表中全部记录的行集对象
int pageSize=10; //每页加载数量
int pageNum=1; //第几页
int totalRecord; //总记录数
/**
* @see HttpServlet#HttpServlet()
*/
public QueryInfoById() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
// TODO Auto-generated method stub
response.setContentType("text/html;charset=utf-8");
request.setCharacterEncoding("utf-8");
response.setCharacterEncoding("utf-8");
PrintWriter out = response.getWriter();
String infoId = request.getParameter("infoId");
if(infoId == null||infoId.length()==0) {
return;
}
String condition ="select * from info where info_id="+infoId; //按id查找
Connection connection = null;
Statement sql = null;
ResultSet rs = null;
try {
connection = JdbcUtils.getConnection();
sql = connection.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_READ_ONLY);
rs = sql.executeQuery(condition);
rowSet = new CachedRowSetImpl(); //创建行集对象
rowSet.populate(rs);
response.setContentType("text/html;charset=utf-8");
response.setCharacterEncoding("utf-8");
JSONArray jsonArray = new JSONArray();//存放返回的jsonOjbect数组
//将rowSet的数据提取到Map
List<Map<String,Object>> data = new ArrayList<Map<String,Object>>();
try {
rowSet.last(); //移到随后一行
totalRecord = rowSet.getRow();
int index = (pageNum-1)*pageSize+1;
rowSet.absolute(index); //查询位置移动到查询页的起始记录位置
boolean boo = true;
for(int i=1; i<=totalRecord&&boo;i++){
int id = rowSet.getInt(1); //内容ID
String infoTitle = rowSet.getString(2); //内容标题
String infoDescribe = rowSet.getString(3); //内容简述
String infoDetail = rowSet.getString("info_detail"); //内容详情
String type = rowSet.getString(5); //类型:0表示日记,1表示趣事
String support = rowSet.getString(6); //点赞数
String infoAuthor = rowSet.getString(7); //作者
Map<String,Object> map = new HashMap<String,Object>();
map.put("infoId", id);
map.put("infoTitle", infoTitle);
map.put("infoDescribe", infoDescribe);
map.put("infoDetail", infoDetail);
map.put("infoType", type);
map.put("infoSupport", support);
map.put("infoAuthor", infoAuthor);
data.add(map);
boo = rowSet.next();
}
jsonArray = JsonUtils.formatRsToJsonArray(data);
out.println(jsonArray.toString()); //返回json
}catch(Exception e) {
out.println("null");
}
} catch (Exception e) {
out.println(condition+"异常:"+e.toString());
e.printStackTrace();
}finally {
//5.释放资源 connection prepareStatement
JdbcUtils.statementClose(connection, sql, rs);
}
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
doGet(request, response);
}
}
3. 实现点赞,对应servlet文件AddSupport.java
- 一样在myServlet.data包下新建servlet文件,命名为AddSupport
- 在web.xml中配置:
<servlet>
<servlet-name>AddSupport</servlet-name>
<servlet-class>myServlet.data.AddSupport</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>AddSupport</servlet-name>
<url-pattern>/servlet/AddSupport</url-pattern>
</servlet-mapping>
- AddSupport.java通过参数获取到info_id,查询到info表中的对应点赞数,然后在当前点赞数的基础上加一之后更新到info表对应的内容点赞数中,具体代码如下:
package myServlet.data;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
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 org.utils.JdbcUtils;
import org.utils.JsonUtils;
import net.sf.json.JSONArray;
/**
* Servlet implementation class AddSupport
*/
@WebServlet("/AddSupport")
public class AddSupport extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public AddSupport() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
response.setContentType("text/html;charset=utf-8");
request.setCharacterEncoding("utf-8");
response.setCharacterEncoding("utf-8");
PrintWriter out = response.getWriter();
String infoId = request.getParameter("InfoId").trim();
/* String supportNum = request.getParameter("supportNum").trim();*/
if(infoId == null) {
infoId = "";
}
Connection connection = null;
PreparedStatement prepareStatement = null;
PreparedStatement prepareStatement2 = null;
ResultSet rs = null;
ResultSet rs2 =null;
String backnews="";
boolean boo = false;
boo = infoId.length()>0;
try {
connection = JdbcUtils.getConnection();
//3.获取statement
String sql ="select * from info where info_id=?";
prepareStatement = connection.prepareStatement(sql);
prepareStatement.setString(1, infoId);
if(boo) {
//4.执行sql
rs = prepareStatement.executeQuery();
boolean m = rs.next();
int support = rs.getInt(6);
support = support+1;
if(m==true) {
//查询成功
String changeSql = "update info set info_support=? where info_id=?";
prepareStatement2 = connection.prepareStatement(changeSql);
prepareStatement2.setInt(1, support);
prepareStatement2.setString(2, infoId);
prepareStatement2.execute();
JSONArray jsonArray = new JSONArray();//存放返回的jsonOjbect数组
List<Map<String,Object>> data = new ArrayList<Map<String,Object>>();
Map<String,Object> map = new HashMap<String,Object>();
map.put("addOk", true);
data.add(map);
jsonArray = JsonUtils.formatRsToJsonArray(data);
out.println(jsonArray.toString()); //返回json
}else {
JSONArray jsonArray = new JSONArray();//存放返回的jsonOjbect数组
List<Map<String,Object>> data = new ArrayList<Map<String,Object>>();
Map<String,Object> map = new HashMap<String,Object>();
map.put("addOk", false);
data.add(map);
jsonArray = JsonUtils.formatRsToJsonArray(data);
out.println(jsonArray.toString()); //返回json
}
}else {
JSONArray jsonArray = new JSONArray();//存放返回的jsonOjbect数组
List<Map<String,Object>> data = new ArrayList<Map<String,Object>>();
Map<String,Object> map = new HashMap<String,Object>();
map.put("addOk", false);
data.add(map);
jsonArray = JsonUtils.formatRsToJsonArray(data);
out.println(jsonArray.toString()); //返回json
}
out.print(backnews.toString());
} catch (Exception e) {
backnews="修改失败"+e.toString();
out.print(backnews);
e.printStackTrace();
}finally {
//5.释放资源 connection prepareStatement
JdbcUtils.close(connection, prepareStatement, rs);
JdbcUtils.close(connection, prepareStatement2, rs2);
}
out.flush();
out.close();
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
doGet(request, response);
}
}
4.判断用户是否已经收藏内容——IfAddFocus.java
- 在myServlet.java包下新建servlet命名为IfAddFocus
- 在web.xml中配置:
<servlet>
<servlet-name>IfAddFocus</servlet-name>
<servlet-class>myServlet.data.IfAddFocus</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>IfAddFocus</servlet-name>
<url-pattern>/servlet/IfAddFocus</url-pattern>
</servlet-mapping>
- 通过参数获取到用户名和内容id,根据用户名和id查询focu表是否已经存在该信息,若是则返回true,具体代码如下:
package myServlet.data;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
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 org.utils.JdbcUtils;
import org.utils.JsonUtils;
import net.sf.json.JSONArray;
/**
* Servlet implementation class IfAddlike
*/
@WebServlet("/IfAddlike")
public class IfAddFocus extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public IfAddFocus() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
response.setContentType("text/html;charset=utf-8");
request.setCharacterEncoding("utf-8");
response.setCharacterEncoding("utf-8");
PrintWriter out = response.getWriter();
String username = request.getParameter("username").trim();
String infoId = request.getParameter("infoId").trim();
int focusId = 0;
if(username == null) {
username = "";
}
if(infoId == null) {
infoId = "";
}
Connection connection = null;
PreparedStatement prepareStatement = null;
ResultSet rs = null;
boolean boo = false;
boo = username.length()>0&&infoId.length()>0;
try {
connection = JdbcUtils.getConnection();
//3.获取statement
//String sql ="insert into user(logname,password,phone,address,realname) value(?,?,?,?,?) ";
String sql ="select * from focus where username=? and info_id=?";
prepareStatement = connection.prepareStatement(sql);
prepareStatement.setString(1, username);
prepareStatement.setString(2, infoId);
if(boo) {
//4.执行sql
boolean m =false;
rs = prepareStatement.executeQuery();
while(rs.next()) {
m = true;
focusId = rs.getInt(1);
}
if(m==true) {
success(request,response,focusId);
}else {
fail(request,response);
}
}else {
fail(request,response);
}
} catch (Exception e) {
e.printStackTrace();
fail(request,response);
}finally {
//5.释放资源 connection prepareStatement
JdbcUtils.close(connection, prepareStatement, rs);
}
out.flush();
out.close();
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
doGet(request, response);
}
public void success(HttpServletRequest request, HttpServletResponse response, int focusId) {
try {
JSONArray jsonArray = new JSONArray();//存放返回的jsonOjbect数组
List<Map<String,Object>> data = new ArrayList<Map<String,Object>>();
PrintWriter out = response.getWriter();
Map<String,Object> map = new HashMap<String,Object>();
map.put("isFocus", true);
map.put("focusId", focusId);
data.add(map);
jsonArray = JsonUtils.formatRsToJsonArray(data);
out.println(jsonArray.toString()); //返回json
}catch(IOException e) {
e.printStackTrace();
}
}
public void fail(HttpServletRequest request, HttpServletResponse response) {
response.setContentType("text/html;charset=utf-8");
try {
JSONArray jsonArray = new JSONArray();//存放返回的jsonOjbect数组
List<Map<String,Object>> data = new ArrayList<Map<String,Object>>();
PrintWriter out = response.getWriter();
Map<String,Object> map = new HashMap<String,Object>();
map.put("isFocus", false);
data.add(map);
jsonArray = JsonUtils.formatRsToJsonArray(data);
out.println(jsonArray.toString()); //返回json
}catch(IOException e) {
e.printStackTrace();
}
}
}
5.取消收藏——RemoveFocus.java
- 在myServlet.data包下创建servlet并命名为RemoveFocus
- 在web.xml添加配置
<servlet>
<servlet-name>RemoveFocus</servlet-name>
<servlet-class>myServlet.data.RemoveFocus</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>RemoveFocus</servlet-name>
<url-pattern>/servlet/RemoveFocus</url-pattern>
</servlet-mapping>
- 通过获取到用户名和内容Id,将focus表中对应的收藏信息删除,具体代码如下:
package myServlet.data;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
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 org.utils.JdbcUtils;
import org.utils.JsonUtils;
import net.sf.json.JSONArray;
/**
* Servlet implementation class RemoveFocus
*/
@WebServlet("/RemoveFocus")
public class RemoveFocus extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public RemoveFocus() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
response.setContentType("text/html;charset=utf-8");
request.setCharacterEncoding("utf-8");
response.setCharacterEncoding("utf-8");
PrintWriter out = response.getWriter();
String focusId = request.getParameter("focusId").trim();
if(focusId == null||focusId == "") {
return;
}
Connection connection = null;
PreparedStatement prepareStatement = null;
ResultSet rs = null;
boolean boo = false;
boo = focusId.length()>0;
try {
connection = JdbcUtils.getConnection();
//3.获取statement
String sql ="delete from focus where focus_id=?";
prepareStatement = connection.prepareStatement(sql);
prepareStatement.setString(1, focusId);
if(boo) {
//4.执行sql
prepareStatement.execute();
JSONArray jsonArray = new JSONArray();//存放返回的jsonOjbect数组
List<Map<String,Object>> data = new ArrayList<Map<String,Object>>();
Map<String,Object> map = new HashMap<String,Object>();
map.put("removeOk", true);
data.add(map);
jsonArray = JsonUtils.formatRsToJsonArray(data);
out.println(jsonArray.toString()); //返回json
}else {
JSONArray jsonArray = new JSONArray();//存放返回的jsonOjbect数组
List<Map<String,Object>> data = new ArrayList<Map<String,Object>>();
Map<String,Object> map = new HashMap<String,Object>();
map.put("removeOk", false);
data.add(map);
jsonArray = JsonUtils.formatRsToJsonArray(data);
out.println(jsonArray.toString()); //返回json
}
} catch (Exception e) {
JSONArray jsonArray = new JSONArray();//存放返回的jsonOjbect数组
List<Map<String,Object>> data = new ArrayList<Map<String,Object>>();
Map<String,Object> map = new HashMap<String,Object>();
map.put("removeOk", false);
data.add(map);
jsonArray = JsonUtils.formatRsToJsonArray(data);
out.println(jsonArray.toString()); //返回json
}finally {
//5.释放资源 connection prepareStatement
JdbcUtils.close(connection, prepareStatement, rs);
}
out.flush();
out.close();
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
doGet(request, response);
}
}
6.添加到我的收藏
- 在myServlet.data下新建文件AddFocus.java
- 在web.xml下添加配置:
<servlet>
<servlet-name>AddFocus</servlet-name>
<servlet-class>myServlet.data.AddFocus</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>AddFocus</servlet-name>
<url-pattern>/servlet/AddFocus</url-pattern>
</servlet-mapping>
- 通过参数获取到用户名和内容ID,将信息插入到收藏表focus中,具体代码如下:
package myServlet.data;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
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 org.utils.JdbcUtils;
import org.utils.JsonUtils;
import net.sf.json.JSONArray;
/**
* Servlet implementation class AddFocus
*/
@WebServlet("/AddFocus")
public class AddFocus extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public AddFocus() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
response.setContentType("text/html;charset=utf-8");
request.setCharacterEncoding("utf-8");
response.setCharacterEncoding("utf-8");
PrintWriter out = response.getWriter();
String infoId = request.getParameter("InfoId").trim();
String username = request.getParameter("username").trim();
if(infoId == null||infoId == "") {
return;
}
if(username == null||username == "") {
return;
}
Connection connection = null;
PreparedStatement prepareStatement = null;
PreparedStatement prepareStatement2 = null;
ResultSet rs = null;
ResultSet rs2 =null;
String backnews="";
boolean boo = false;
boo = infoId.length()>0;
try {
connection = JdbcUtils.getConnection();
//3.获取statement
String sql ="select * from focus where info_id=? and username=?";
prepareStatement = connection.prepareStatement(sql);
prepareStatement.setString(1, infoId);
prepareStatement.setString(2, username);
if(boo) {
//4.执行sql
rs = prepareStatement.executeQuery();
boolean m = rs.next();
if(m!=true) {
String changeSql = "INSERT INTO focus VALUES(?,?,?)";
prepareStatement2 = connection.prepareStatement(changeSql);
prepareStatement2.setInt(1, 0);
prepareStatement2.setString(2, username);
prepareStatement2.setString(3, infoId);
prepareStatement2.execute();
JSONArray jsonArray = new JSONArray();//存放返回的jsonOjbect数组
List<Map<String,Object>> data = new ArrayList<Map<String,Object>>();
Map<String,Object> map = new HashMap<String,Object>();
map.put("addOk", true);
data.add(map);
jsonArray = JsonUtils.formatRsToJsonArray(data);
out.println(jsonArray.toString()); //返回json
}else {
JSONArray jsonArray = new JSONArray();//存放返回的jsonOjbect数组
List<Map<String,Object>> data = new ArrayList<Map<String,Object>>();
Map<String,Object> map = new HashMap<String,Object>();
map.put("addOk", false);
data.add(map);
jsonArray = JsonUtils.formatRsToJsonArray(data);
out.println(jsonArray.toString()); //返回json
}
}else {
JSONArray jsonArray = new JSONArray();//存放返回的jsonOjbect数组
List<Map<String,Object>> data = new ArrayList<Map<String,Object>>();
Map<String,Object> map = new HashMap<String,Object>();
map.put("addOk", false);
data.add(map);
jsonArray = JsonUtils.formatRsToJsonArray(data);
out.println(jsonArray.toString()); //返回json
}
out.print(backnews.toString());
} catch (Exception e) {
backnews="修改失败"+e.toString();
out.print(backnews);
e.printStackTrace();
}finally {
//5.释放资源 connection prepareStatement
JdbcUtils.close(connection, prepareStatement, rs);
JdbcUtils.close(connection, prepareStatement2, rs2);
}
out.flush();
out.close();
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
doGet(request, response);
}
}
7.评论内容
- 在myServlet.data下新建servlet并命名为AddComment
- 在web.xml中添加配置
<servlet>
<servlet-name>AddComment</servlet-name>
<servlet-class>myServlet.data.AddComment</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>AddComment</servlet-name>
<url-pattern>/servlet/AddComment</url-pattern>
</servlet-mapping>
- 同参数获取到用户的ID、内容ID、评论的内容插入到评论表中,具体代码如下
package myServlet.data;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
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 org.utils.JdbcUtils;
import org.utils.JsonUtils;
import net.sf.json.JSONArray;
/**
* Servlet implementation class AddComment
*/
@WebServlet("/AddComment")
public class AddComment extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public AddComment() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
response.setContentType("text/html;charset=utf-8");
request.setCharacterEncoding("utf-8");
response.setCharacterEncoding("utf-8");
PrintWriter out = response.getWriter();
Integer commentId =0; //内容ID,默认设置为0
String infoIdStr = request.getParameter("infoId"); //内容标题
String commentUser =request.getParameter("commentUser"); //内容简述
String commentDetail = request.getParameter("commentDetail"); //内容详情
if(infoIdStr == null||infoIdStr == "") {
infoIdStr = "";
}
if(commentUser == null||commentUser == "") {
commentUser = "";
}
if(commentDetail == null||commentDetail == "") {
commentDetail = "";
}
Boolean m = infoIdStr.length()>0&&commentUser.length()>0&&commentDetail.length()>0;
int infoId = 0;
try {
infoId = Integer.parseInt(infoIdStr); //内容类型
} catch (NumberFormatException e) {
e.printStackTrace();
}
Connection connection = null;
PreparedStatement prepareStatement = null;
try {
connection = JdbcUtils.getConnection();
//3.获取statement
String sql ="INSERT INTO comment VALUES(?,?,?,?)";
prepareStatement = connection.prepareStatement(sql);
if(m) {
prepareStatement.setInt(1, commentId); //自增
prepareStatement.setString(2, commentUser);
prepareStatement.setInt(3, infoId);
prepareStatement.setString(4, commentDetail);
//4.执行sql
prepareStatement.execute();
JSONArray jsonArray = new JSONArray();//存放返回的jsonOjbect数组
List<Map<String,Object>> data = new ArrayList<Map<String,Object>>();
Map<String,Object> map = new HashMap<String,Object>();
map.put("addOk", true);
data.add(map);
jsonArray = JsonUtils.formatRsToJsonArray(data);
out.println(jsonArray.toString()); //返回json
}else {
JSONArray jsonArray = new JSONArray();//存放返回的jsonOjbect数组
List<Map<String,Object>> data = new ArrayList<Map<String,Object>>();
Map<String,Object> map = new HashMap<String,Object>();
map.put("addOk", false);
data.add(map);
jsonArray = JsonUtils.formatRsToJsonArray(data);
out.println(jsonArray.toString()); //返回json
}
} catch (Exception e) {
JSONArray jsonArray = new JSONArray();//存放返回的jsonOjbect数组
List<Map<String,Object>> data = new ArrayList<Map<String,Object>>();
Map<String,Object> map = new HashMap<String,Object>();
map.put("addOk", false);
data.add(map);
jsonArray = JsonUtils.formatRsToJsonArray(data);
out.println(jsonArray.toString()); //返回json
}finally {
//5.释放资源 connection prepareStatement
JdbcUtils.close(connection, prepareStatement, null);
}
out.flush();
out.close();
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
doGet(request, response);
}
}
8.删除分享内容
- 在myServlet.data下新建servlet并命名为DeleteInfo
- 在web.xml中添加配置:
<servlet>
<servlet-name>DeleteInfo</servlet-name>
<servlet-class>myServlet.data.DeleteInfo</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>DeleteInfo</servlet-name>
<url-pattern>/servlet/DeleteInfo</url-pattern>
</servlet-mapping>
- 通过参数获取到内容ID,对应删除收藏表中的相关内容,然后再删除info表中的内容,具体代码如下:
package myServlet.data;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
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 org.utils.JdbcUtils;
import org.utils.JsonUtils;
import net.sf.json.JSONArray;
/**
* Servlet implementation class DeleteInfo
*/
@WebServlet("/DeleteInfo")
public class DeleteInfo extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public DeleteInfo() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
response.setContentType("text/html;charset=utf-8");
request.setCharacterEncoding("utf-8");
response.setCharacterEncoding("utf-8");
PrintWriter out = response.getWriter();
String infoId = request.getParameter("infoId").trim();
if(infoId == null||infoId == "") {
return;
}
Connection connection = null;
PreparedStatement prepareStatement = null;
ResultSet rs = null;
PreparedStatement prepareStatement3 = null;
ResultSet rs3 =null;
String backnews="";
boolean boo = false;
boo = infoId.length()>0;
try {
connection = JdbcUtils.getConnection();
//3.获取statement
String sql ="delete from focus where info_id=?";
prepareStatement = connection.prepareStatement(sql);
prepareStatement.setString(1, infoId);
if(boo) {
//4.执行sql
prepareStatement.execute();
String changeSql = "delete from info where info_id=?";
prepareStatement3 = connection.prepareStatement(changeSql);
prepareStatement3.setString(1, infoId);
prepareStatement3.execute();
JSONArray jsonArray = new JSONArray();//存放返回的jsonOjbect数组
List<Map<String,Object>> data = new ArrayList<Map<String,Object>>();
Map<String,Object> map = new HashMap<String,Object>();
map.put("deleteOk", true);
data.add(map);
jsonArray = JsonUtils.formatRsToJsonArray(data);
out.println(jsonArray.toString()); //返回json
}else {
JSONArray jsonArray = new JSONArray();//存放返回的jsonOjbect数组
List<Map<String,Object>> data = new ArrayList<Map<String,Object>>();
Map<String,Object> map = new HashMap<String,Object>();
map.put("deleteOk", false);
data.add(map);
jsonArray = JsonUtils.formatRsToJsonArray(data);
out.println(jsonArray.toString()); //返回json
}
out.print(backnews.toString());
} catch (Exception e) {
backnews="修改失败"+e.toString();
out.print(backnews);
e.printStackTrace();
}finally {
//5.释放资源 connection prepareStatement
JdbcUtils.close(connection, prepareStatement, rs);
JdbcUtils.close(connection, prepareStatement2, rs2);
JdbcUtils.close(connection, prepareStatement3, rs3);
}
out.flush();
out.close();
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
doGet(request, response);
}
}