效果图:在jsp页面展示从后台读取的数据
思路:以查询数据库用户信息为例
1.前台jsp页面点击“查询”,提交请求到查询控制器SelectServlet。
2.查询控制器调用服务层的UserService类的userList()方法,获取后台返回的用户列表(list)数据,并将数据赋值给前台对应属性(key_list,自定义命名)渲染即可。
【注】需要用到jstl标签来实现(参考资料:http://www.runoob.com/jsp/jsp-jstl.html),jstl需要导入jar包,并引入jsp页面,步骤如下:
第一步:首先下载如下图的压缩包
官网下载地址:http://archive.apache.org/dist/jakarta/taglibs/standard/binaries/
解压后,将解压文件bin文件夹下的jstl和standard两个jar包导入项目,buildpath一下,并粘贴至tomcat的bin目录下。
第二步:jsp引入核心jstl标签库
<%@ taglib prefix=“c” uri=“http://java.sun.com/jsp/jstl/core” %>
实现过程:
jsp代码:search.jsp
...略头部及尾部信息,该页面只有一个按钮
显示用户信息列表页面:userinfo.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
用户页面
id
username
password
${a.id}
${a.username}
${a.password}
SelectServlet
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
IUserService userService = new UserServiceImpl();
//设置“key_list”属性的值为userService的userList返回值(list)
request.setAttribute("key_list", userService.userList());
//跳转至用户信息页面
request.getRequestDispatcher("userInfo.jsp").forward(request, response);
}
IUserService
package service;
import java.util.List;
import bean.User;
public interface IUserService {
//登录
User login(User user);
//注册
boolean registerUser(User user);
//查询用户信息
List userList();
}
UserServiceImpl
...略
public class UserServiceImpl implements IUserService{
IUserDao userDao = new UserDaoImpl();
@Override
public List userList() {
return userDao.userList();//调用dao层的方法,访问数据库
}
}
IUserDao
package dao;
import java.util.List;
import bean.User;
public interface IUserDao {
//登录
User find(User user);
//注册
boolean registerUser(User user);
//查询用户信息
List userList();
}
UserDaoImpl
...略
public List userList() {
//获取数据库连接
conn = db.getConnection();
String sql = "select id,username,password from users";
//创建list集合,存储用户对象
List
javabean
package bean;
public class User {
private String username;
private String password;
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public User() {
super();
}
public User(String username, String password) {
super();
this.username = username;
this.password = password;
}
}
数据库连接
package utils;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class DB_Connection {
private static String DRIVER = "oracle.jdbc.OracleDriver";//驱动文件
private static String URL = "jdbc:oracle:thin:@localhost:1521:orcl";//数据库访问地址
private static String USER = "***";//数据库账号
private static String PASS = "***";//数据库密码
Connection connection = null;
public Connection getConnection() {
try {
Class.forName(DRIVER);
connection = DriverManager.getConnection(URL, USER, PASS);
} catch (ClassNotFoundException | SQLException e) {
e.printStackTrace();
}
return connection;
}
}
数据库users 用户表创建。
pl/sql developer可视化工具
create table users(
id number(12) not null primary key,
username varchar(20) not null,
password varchar(30)
)