使用JSTL标签库实现,将学生信息以表格的形式响应到jsp页面

mysql+DButiles+JSTL

新建数据库和表

问题:记得编码字符集的问题,让mysql支持中文。

create database web_test2_student;
use web_test2_student;
create table student(
	sid int primary key auto_increment,
    sname varchar(20),
    sex varchar(10),
    age int 
);
insert into student values(null,"小王","男",20),
(null,"小红","女",18),(null,"小明","男",15),(null,"张三","男",25),(null,"李四","男",28);

安装jar包

使用JSTL标签库实现,将学生信息以表格的形式响应到jsp页面_第1张图片

java代码目录

使用JSTL标签库实现,将学生信息以表格的形式响应到jsp页面_第2张图片

StudentServlet.java

package com.item.controller;

import java.io.IOException;
import java.sql.SQLException;
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.item.domain.Student;
import com.item.model.StudentModel;

/**
 * Servlet implementation class StudentServlet
 */
@WebServlet("/StudentServlet")
public class StudentServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * 
     */
    public StudentServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * 
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		//response.getWriter().write("1231231");
		//return ;
		//1.调用java类处理数据
		StudentModel studentmodel=new StudentModel();
		try {
			List<Student>list=studentmodel.findAll();
			request.setAttribute("list", list);
			request.getRequestDispatcher("jsp/list.jsp").forward(request, response);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		//2.显示到jsp页面
	}

	/**
	 * 
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}

Student.java

package com.item.domain;

public class Student {
	private Integer sid;
	private String sname;
	private String sex;
	private Integer age;
	public Integer getSid() {
		return sid;
	}
	public void setSid(Integer sid) {
		this.sid = sid;
	}
	public String getSname() {
		return sname;
	}
	public void setSname(String sname) {
		this.sname = sname;
	}
	public String getSex() {
		return sex;
	}
	public void setSex(String sex) {
		this.sex = sex;
	}
	public Integer getAge() {
		return age;
	}
	public void setAge(Integer age) {
		this.age = age;
	}
	
}

StudentModel.java

package com.item.model;

import java.sql.SQLException;
import java.util.List;

import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import com.item.domain.Student;
import com.item.utiles.JDBCutiles;

public class StudentModel {

	public List<Student> findAll() throws SQLException {
		QueryRunner queryRunner=new QueryRunner(JDBCutiles.getDataSource());
		List<Student>list=queryRunner.query("select * from student",new BeanListHandler<Student>(Student.class));
		return list;
	}
//	public static void main(String[] args) throws SQLException {
//		QueryRunner queryRunner=new QueryRunner(JDBCutiles.getDataSource());
//		String sql="insert into student values(null,'刘园','男',12)";
//		int cnt=queryRunner.update(sql);
//		if(cnt>0) {
//			System.out.println("成功");
//		}else {
//			System.out.println("失败");
//		}
//		Listlist=queryRunner.query("select * from student",new BeanListHandler(Student.class));
//		if(list==null) {
//			System.out.println("空");
//		}
//		for( Student stu:list) {
//			System.out.println(stu.getSid()+" "+stu.getSname()+" "+stu.getSex()+" "+stu.getAge());
//		}
//	}
}

JDBCutiles.java

package com.item.utiles;

import javax.sql.DataSource;

import com.mchange.v2.c3p0.ComboPooledDataSource;

public class JDBCutiles {
 public static final ComboPooledDataSource datasource=new ComboPooledDataSource();
 
 public static DataSource getDataSource() {
	 return datasource;
 }
}

c3p0-config.xml


<c3p0-config>
	<default-config>
		<property name="driverClass">com.mysql.jdbc.Driverproperty>
		<property name="jdbcUrl">jdbc:mysql://localhost:3308/web_test2_studentproperty>
		<property name="user">rootproperty>
		<property name="password">3308property>
		
		<property name="initialPoolSize">5property>
		<property name="minPoolSize">5property>
		<property name="maxPoolSize">20property>
	default-config> 
c3p0-config>

list.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>




Insert title here


	

学生信息页面

学生编号 学生姓名 学生性别 学生年龄
${student.sid} ${student.sname} ${student.sex} ${student.age}

你可能感兴趣的:(java)