出现500错误?
<%--
Created by IntelliJ IDEA.
User: adm
Date: 2019/11/7
Time: 9:19 上午
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8"%>
<html>
<head>
<title>MessageBoard</title>
<meta http-equiv="refresh" content="0;url=<%=request.getContextPath()%>/message/list.do">
</head>
</html>
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
version="4.0">
<welcome-file-list>
<welcome-file>/index.jsp</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>MessageListServlet</servlet-name>
<servlet-class>com.bloom.test.servlet.MessageListServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>MessageListServlet</servlet-name>
<url-pattern>/message/list.do</url-pattern>
</servlet-mapping>
<error-page>
<error-code>404</error-code>
<location>/WEB-INF/views/error/404.jsp</location>
</error-page>
<error-page>
<error-code>500</error-code>
<location>/WEB-INF/views/error/500.jsp</location>
</error-page>
</web-app>
package com.bloom.test.servlet;
import com.bloom.test.bean.Message;
import com.bloom.test.service.MessageService;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
/**
* 消息列表的Servlet
*/
public class MessageListServlet extends HttpServlet {
private MessageService messageService;
@Override
public void init() throws ServletException {
super.init();
messageService = new MessageService();
}
@Override
protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String pageStr = request.getParameter("page");//当前页码
int page = 1;//页面默认值为1
if(null != pageStr && (!"".equals(pageStr))) {
try {
page = Integer.parseInt(pageStr);
} catch (NumberFormatException e) {
e.printStackTrace();
}
}
List<Message> messages = messageService.getMessages(page,5);//分页查询全部留言
int count = messageService.countMessage();
int last = count % 5 == 0 ?(count / 5) : ((count / 5) + 1);
request.setAttribute("last",last);
request.setAttribute("messages",messages);
request.setAttribute("page",page);
//返回页面
request.getRequestDispatcher("WEB-INF/views/biz/message_list.jsp").forward(request,response);
}
@Override
public void destroy() {
super.destroy();
messageService = null;
}
}
package com.bloom.test.service;
import com.bloom.test.bean.Message;
import com.bloom.test.dao.MessageDAO;
import java.util.List;
/**
* 消息Service
*/
public class MessageService {
private MessageDAO messageDAO;
public MessageService() {
messageDAO = new MessageDAO();
}
/**
* 分页查询全部留言
* @param page 当前页码
* @param pageSize 每页记录数
* @return
*/
public List<Message> getMessages(int page,int pageSize){
return messageDAO.getMessages(page,pageSize);
}
/**
* 计算所有留言数量
* @return
*/
public int countMessage(){
return messageDAO.countMessage();
}
}
package com.bloom.test.dao;
import com.bloom.test.bean.Message;
import com.bloom.test.common.ConnectionUtil;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
/**
* 消息DAO
*/
public class MessageDAO {
/**
* 分页查询全部留言
* @param page 当前页码
* @param pageSize 每页记录数
* @return
*/
public List<Message> getMessages(int page,int pageSize){
Connection con = ConnectionUtil.getConnection();
String sql = "select * from message order by createTime desc limit ?,? ";//limit m,n 从m条开始共n条记录
PreparedStatement stmt = null;
ResultSet rs = null;
List<Message> messages = new ArrayList<>();
try {
stmt = con.prepareStatement(sql);
stmt.setInt(1,(page - 1 ) * pageSize);
stmt.setInt(2,pageSize);
stmt.executeQuery();
while(rs.next()){
messages.add(new Message(rs.getLong("id"),
rs.getLong("userId"),
rs.getString("userName"),
rs.getString("title"),
rs.getString("content"),
rs.getTimestamp("createTime")));
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
ConnectionUtil.release(rs,stmt,con);
}
return messages;
}
/**
* 计算所有留言数量
* @return
*/
public int countMessage(){
Connection con = ConnectionUtil.getConnection();
String sql = "select count(*) total from message ";
PreparedStatement stmt = null;
ResultSet rs = null;
try {
stmt = con.prepareStatement(sql);
rs = stmt.executeQuery();
while(rs.next()){
return rs.getInt("total");
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
ConnectionUtil.release(rs,stmt,con);
}
return 0;
}
}
package com.bloom.test.common;
import java.sql.*;
/**
* 数据库操作公共类
* @version
*/
public final class ConnectionUtil {
private static String url = "jdbc:mysql://127.0.0.1:3306/bloom";
private static String username = "root";
private static String password = "123456";
private ConnectionUtil(){}
static {
try {
Class.forName("com.mysql.cj.jdbc.Driver");
} catch (ClassNotFoundException e) {
System.out.println("找不到驱动程序类,驱动加载失败!");
e.printStackTrace();
}
}
/**
* 获得数据库链接
* @return
*/
public static Connection getConnection(){
try {
return DriverManager.getConnection(url,username,password);
} catch (SQLException e) {
System.out.println("创建数据库连接失败!");
e.printStackTrace();
}
return null;
}
/**
* 释放数据库资源
* @param rs ResultSet
* @param stmt Statement
* @param con Connection
*/
public static void release(ResultSet rs, Statement stmt,Connection con){
try {
if(rs != null){
rs.close();
}
} catch (SQLException e) {
e.printStackTrace();
}finally{
try {
if(stmt != null){
stmt.close();
}
} catch (SQLException e) {
e.printStackTrace();
}finally{
try {
if(con != null){
con.close();
}
} catch (SQLException e) {
e.printStackTrace();
}finally{
}
}
}
}
}
package com.bloom.test.bean;
import java.util.Date;
/**
* 消息实体
*/
public class Message {
private long id;
private long userId;
private String userName;
private String title;
private String content;
private Date createTime;
public Message() {
}
public Message(long id, long userId, String userName, String title, String content, Date createTime) {
this.id = id;
this.userId = userId;
this.userName = userName;
this.title = title;
this.content = content;
this.createTime = createTime;
}
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public long getUserId() {
return userId;
}
public void setUserId(long userId) {
this.userId = userId;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
public Date getCreateTime() {
return createTime;
}
public void setCreateTime(Date createTime) {
this.createTime = createTime;
}
}
正在回答 回答被采纳积分+1
- 参与学习 人
- 提交作业 277 份
- 解答问题 4297 个
Java数据库开发的必备技能,从流行的MySQL数据库开始,到Java原生的数据库管理接口JDBC的使用,再到常用的数据持久化框架MyBatis,让你向Java工程师的目标又迈进了一步!
了解课程
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星