上一篇讲了python 爬取糗事百科的数据到数据库中,本篇讲解java web接口开发,以json的形式展示接口
准备:java 环境,tomcat环境,eclipse,eclipse集成tomcat ,json.jar,mysql-connect-java .jar
eclipse新建Dynamic web project,目录层级如下:
将jar包复制到lib目录下,然后add build to path
下面详述代码:
1.创建糗事百科实体类Qiushibaike
package com.entity;
public class Qiushibaike {
private int id;
private String imgurl;
private String username;
private String vote;
private String comments;
private String content;
private String imgpath;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getImgurl() {
return imgurl;
}
public void setImgurl(String imgurl) {
this.imgurl = imgurl;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getVote() {
return vote;
}
public void setVote(String vote) {
this.vote = vote;
}
public String getComments() {
return comments;
}
public void setComments(String comments) {
this.comments = comments;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
public String getImgpath() {
return imgpath;
}
public void setImgpath(String imgpath) {
this.imgpath = imgpath;
}
}
2.写一个分页实体类QiushiPage:
package com.entity;
import java.util.List;
public class QiushiPage {
private int currentPage;//当前页
private int totalPage;//总页数
private int count;//一页多少条数据
private int totalCount;//数据总条数
private List qiushibaikes;//当前页的数据
public int getTotalCount() {
return totalCount;
}
public void setTotalCount(int totalCount) {
this.totalCount = totalCount;
}
public List getQiushibaikes() {
return qiushibaikes;
}
public void setQiushibaikes(List qiushibaikes) {
this.qiushibaikes = qiushibaikes;
}
public int getCurrentPage() {
return currentPage;
}
public void setCurrentPage(int currentPage) {
this.currentPage = currentPage;
}
public int getTotalPage() {
return totalPage;
}
public void setTotalPage(int totalPage) {
this.totalPage = totalPage;
}
public int getCount() {
return count;
}
public void setCount(int count) {
this.count = count;
}
}
3.数据库操作类QiushiDaoImpl:
package com.dao;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import com.entity.Qiushibaike;
public class QiushiDaoImpl {
private PreparedStatement ptmt = null;
private ResultSet rs = null;
//获取表中的数据,放入list,并返回
public List getCaseAll(int page, int count) {
// TODO Auto-generated method stub
List list = new ArrayList();
try {
Class.forName("com.mysql.jdbc.Driver");
String url="jdbc:mysql://localhost:3306/shop?useunicuee=true& characterEncoding=utf8";
String username="root";
String password="root";
Connection conn=DriverManager.getConnection(url,username,password);
String sql="select id,imgurl,comments,username,content,vote,imgpath from qiushibaike LIMIT ?,?";
PreparedStatement ps = conn.prepareStatement(sql);
ps.setInt(1, (page-1)*count);
ps.setInt(2, count);
ResultSet rs=ps.executeQuery();
//查询实体放入list
while(rs.next()){
Qiushibaike qiushibaike=new Qiushibaike();
//查询的每个字段值放入到实体中
qiushibaike.setId(rs.getInt("id"));
qiushibaike.setImgurl(rs.getString("imgurl"));
qiushibaike.setComments(rs.getString("comments"));
qiushibaike.setUsername(rs.getString("username"));
qiushibaike.setContent(rs.getString("content"));
qiushibaike.setVote(rs.getString("vote"));
qiushibaike.setImgpath(rs.getString("imgpath"));
list.add(qiushibaike);
}
rs.close();
ps.close();
conn.close();
} catch (Exception e) {
e.printStackTrace();
}
return list;
}
//查询表中的记录数,并返回
public int count() throws SQLException {
int count = 0;
try {
Class.forName("com.mysql.jdbc.Driver");
String url="jdbc:mysql://localhost:3306/test?useunicuee=true& characterEncoding=utf8";
String username="root";
String password="root";
Connection conn=DriverManager.getConnection(url,username,password);
if(conn==null){
throw new NullPointerException("conn is null");
}
PreparedStatement ps = conn.prepareStatement("SELECT COUNT(*) FROM qiushibaike");
if(ps==null){
throw new NullPointerException("ps is null");
}
ResultSet rs = ps.executeQuery();
if(rs==null){
throw new NullPointerException("rs is null");
}
if (rs.next()) {
count = rs.getInt(1);
}
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return count;
}
}
4.数据分页类QiushiServiceImpl:
package com.dao;
import java.sql.SQLException;
import java.util.List;
import com.entity.QiushiPage;
import com.entity.Qiushibaike;
public class QiushiServiceImpl {
private QiushiDaoImpl qiushiDaoImpl = new QiushiDaoImpl();
public QiushiPage findPage(int page, int count) {
if(qiushiDaoImpl==null){
qiushiDaoImpl = new QiushiDaoImpl();
}
try {
List qiushibaikes = qiushiDaoImpl.getCaseAll(page, count);
System.out.println(qiushibaikes);
int totle = qiushiDaoImpl.count();
System.out.println(totle);
QiushiPage p=new QiushiPage();
p.setQiushibaikes(qiushibaikes);;
p.setCurrentPage(page);
p.setCount(count);
p.setTotalCount(totle);
int totlePage = totle%count==0?totle/count:(totle/count)+1;
p.setTotalPage(totlePage);
return p;
} catch (SQLException e) {
e.printStackTrace();
}
return null;
}
}
5.servlet类QiushiServlet:
package com.servlet;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;
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.QiushiDaoImpl;
import com.dao.QiushiServiceImpl;
import com.entity.QiushiPage;
import com.entity.Qiushibaike;
import com.google.gson.Gson;
/**
* Servlet implementation class TestAdd
*/
public class QiushiServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public QiushiServlet() {
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
QiushiServiceImpl qiushiServiceImpl=new QiushiServiceImpl();
int currentPage=1;
//每一页15条数据
int count=15;
String value = request.getParameter("page");
if(value!=null&&!"".equals(value)){
currentPage = Integer.parseInt(value);
}
//转化成json
QiushiPage page = qiushiServiceImpl.findPage(currentPage, count);
Gson gson = new Gson();
String json = gson.toJson(page);
// 输出到界面
System.out.println(json);
//response.setCharacterEncoding("utf-8");
response.setContentType("text/json;charset=utf-8");
//PrintWriter out = response.getWriter();
PrintWriter out = new PrintWriter(response.getWriter());
out.print(json);
out.flush();
}
/**
* @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.WEB-INF 目录下新建一个xml文件web.xml
QiushiServlet
com.servlet.QiushiServlet
QiushiServlet
/QiushiServlet
index.jsp
7.WebContent下新建一个jsp文件:
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
首页
点击接口
代码部分就是这样,附上代码结构图:
最有右击项目,run on server结果如图
点击上图的按钮跳转到接口界面:
需要访问第二页时输入如下网址,page后面的数字表示页数:
http://localhost:8080/QiuShiSample/QiushiServlet?page=2