将从数据库得到的信息分别以HTML,XML,Json的形式输出

package donghongyu.servlet;

import java.io.IOException;

public class PrintServlet extends HttpServlet {

	/**
	 * Constructor of the object.
	 */
	public PrintServlet() {
		super();
	}

	/**
	 * Destruction of the servlet. <br>
	 */
	public void destroy() {
		super.destroy(); // Just puts "destroy" string in log
		// Put your code here
	}

	/**
	 * The doGet method of the servlet. <br>
	 * 
	 * This method is called when a form has its tag value method equals to get.
	 * 
	 * @param request
	 *            the request send by the client to the server
	 * @param response
	 *            the response send by the server to the client
	 * @throws ServletException
	 *             if an error occurred
	 * @throws IOException
	 *             if an error occurred
	 */
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		response.setContentType("tex/html,encoding=UTF-8");
		response.setCharacterEncoding("UTF-8");

		PrintWriter out = response.getWriter();

		out.print(request.getParameter("name") + "你好,欢迎登入CSDN-JAVA班开发的web网页!");

		// findUser(request, response);
		// findUserXml(request, response);
		// findUserJson(request, response);
	}

	public void findUser(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		response.setContentType("text/html");

		response.setCharacterEncoding("UTF-8");

		PrintWriter out = response.getWriter();
		out.println("<!DOCTYPE HTML>");
		out.println("<HTML>");
		out.println("  <HEAD><TITLE>数据库信息</TITLE></HEAD>");
		out.println("  <BODY>");
		out.println("数据库中的数据如下:<br/>");
		// 创建userDaoImple的实例
		UserDaoImpl userDaoImpl = new UserDaoImpl();

		// 调用userDaoImple的实例内部的方法得到数据集合对象
		List<Admin> list = userDaoImpl.findAll();
		// 遍历集合

		out.println("<table border='2' style='height:40px;width:400px'>");
		out.println("<tr >");
		out.println("<td>");
		out.println("ID");
		out.println("</td>");
		out.println("<td>");
		out.println("name");
		out.println("</td>");
		out.println("<td>");
		out.println("pass");
		out.println("</td>");
		out.println("<td>");
		out.println("sex");
		out.println("</td>");
		out.println("</tr>");

		for (Admin admin : list) {

			out.println("ID:" + admin.getId() + "\t姓名:" + admin.getName()
					+ "\t密码:" + admin.getPass() + "\t性别:" + admin.getSex()
					+ "<br/>");

			out.println("<br/><br/>");

			out.println("<tr height='40px'>");
			out.println("<td>");
			out.println(admin.getId());
			out.println("</td>");
			out.println("<td>");
			out.println(admin.getName());
			out.println("</td>");
			out.println("<td>");
			out.println(admin.getPass());
			out.println("</td>");
			out.println("<td>");
			out.println(admin.getSex());
			out.println("</td>");
			out.println("</tr>");

		}
		out.println("</table>");
		out.println("  </BODY>");
		out.println("</HTML>");
		out.flush();
		out.close();

	}

	public void findUserXml(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		response.setContentType("application/xml");

		response.setCharacterEncoding("UTF-8");

		PrintWriter out = response.getWriter();
		out.println("<?xml version='1.0' encoding='UTF-8'?>");
		// 创建userDaoImple的实例
		UserDaoImpl userDaoImpl = new UserDaoImpl();

		// 调用userDaoImple的实例内部的方法得到数据集合对象
		List<Admin> list = userDaoImpl.findAll();
		// 遍历集合

		out.println("<数据库>");

		for (Admin admin : list) {

			out.println("<用户>");
			out.println("<id>");
			out.println(admin.getId());
			out.println("</id>");

			out.println("<name>");
			out.println(admin.getName());
			out.println("</name>");

			out.println("<pass>");
			out.println(admin.getPass());
			out.println("</pass>");

			out.println("<sex>");
			out.println(admin.getSex());
			out.println("</sex>");
			out.println("</用户>");
		}

		out.println("</数据库>");
		out.flush();
		out.close();

	}

	public void findUserJson(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		response.setContentType("application/json");

		response.setCharacterEncoding("UTF-8");

		PrintWriter out = response.getWriter();

		// 创建userDaoImple的实例
		UserDaoImpl userDaoImpl = new UserDaoImpl();

		// 调用userDaoImple的实例内部的方法得到数据集合对象
		List<Admin> list = userDaoImpl.findAll();
		// 遍历集合

		out.print("{\"user\":[");
		for (int i = 0; i < list.size(); i++) {
			Admin admin = list.get(i);
			if (i < list.size() - 1) {
				out.print("{\"id\":\"" + admin.getId() + "\",\"name\":\""
						+ admin.getName() + "\",\"pass\":\"" + admin.getPass()
						+ "\",\"sex\":\"" + admin.getSex() + "\"},");
			} else {
				out.print("{\"id\":\"" + admin.getId() + "\",\"name\":\""
						+ admin.getName() + "\",\"pass\":\"" + admin.getPass()
						+ "\",\"sex\":\"" + admin.getSex() + "\"}");
			}

		}
		out.print("]}");
		out.flush();
		out.close();

	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		response.setContentType("text/html");
		PrintWriter out = response.getWriter();
		out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");
		out.println("<HTML>");
		out.println("  <HEAD><TITLE>A Servlet</TITLE></HEAD>");
		out.println("  <BODY>");
		out.print("    This is ");
		out.print(this.getClass());
		out.println(", using the POST method");
		out.println("  </BODY>");
		out.println("</HTML>");
		out.flush();
		out.close();
	}

	/**
	 * Initialization of the servlet. <br>
	 * 
	 * @throws ServletException
	 *             if an error occurs
	 */
	public void init() throws ServletException {
		// Put your code here
	}

}

package donghongyu.domain;

import java.sql.Date;

public class Admin {
	private Integer id;
	private String name;
	private String pass;
	private String sex;
	private Date birth;

	public Admin() {
		super();
		// TODO Auto-generated constructor stub
	}

	public Admin(Integer id, String name, String pass, String sex, Date birth) {
		super();
		this.id = id;
		this.name = name;
		this.pass = pass;
		this.sex = sex;
		this.birth = birth;
	}

	public Integer getId() {
		return id;
	}

	public void setId(Integer id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getPass() {
		return pass;
	}

	public void setPass(String pass) {
		this.pass = pass;
	}

	public String getSex() {
		return sex;
	}

	public void setSex(String sex) {
		this.sex = sex;
	}

	public Date getBirth() {
		return birth;
	}

	public void setBirth(Date birth) {
		this.birth = birth;
	}

	@Override
	public String toString() {
		return "Admin [id=" + id + ", name=" + name + ", pass=" + pass
				+ ", sex=" + sex + ", birth=" + birth + "]";
	}

}
package donghongyu.dao;

import java.util.List;

public interface BaseDao<T,PK,Name> {
	T FindByName(Name na);
	List<T> findAll();
}

package donghongyu.dao;

import donghongyu.domain.Admin;

public interface UserDao extends BaseDao<Admin, Integer,String> {

}


package donghongyu.dao_impl;

import java.sql.Connection;

public class UserDaoImpl implements UserDao {

	// 创建连接对象
	private Connection conn;
	private PreparedStatement pstmt;
	private ResultSet rs;

	@Override
	public List<Admin> findAll() {
		// TODO Auto-generated method stub
		// 创建返回值
		List<Admin> list = new ArrayList<Admin>();
		// 创建中间变量
		Admin admin = null;
		
		
		String sql = "select id,name,pass,sex,birth from users";
		conn = JdbcUtil.getConn();
		
		try {
			pstmt = conn.prepareStatement(sql);

			rs = pstmt.executeQuery();
			SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			Date brit=rs.getDate("birth");
			
			while (rs.next()) {
				admin = new Admin();
				admin.setId(rs.getInt("id"));
				admin.setName(rs.getString("name"));
				admin.setPass(rs.getString("pass"));
				admin.setSex(rs.getString("sex"));
				
				
				admin.setBirth(rs.getDate("birth"));
				list.add(admin);
			}

		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			JdbcUtil.release(pstmt, rs);
		}
		return list;
	}

	@Override
	public Admin FindByName(String na) {
		// TODO Auto-generated method stub
		Admin admin=null;
		
		String sql="select id,name,pass,sex";
		return null;
	}

}

连接数据库时封装好的类
package donghongyu.unit;

import java.io.File;

public class JdbcUtil {
	private static Connection conn;

	private JdbcUtil() {
		super();
	}

	public static synchronized Connection getConn() {
		if (conn == null) {
			try {
				// 加载驱动文件
				Class.forName("com.mysql.jdbc.Driver");
				// 创建属性文件对象
				File file = new File("F:/web/tomcate/webapps/test/WEB-INF/classes/jdbc.properties");
				// 将属性文件对象转化为流文件
				InputStream is = new FileInputStream(file);
				// 创建解读属性文件的属性的对象
				Properties prop = new Properties();
				// 将属性文件对象加载到属性对象的解析中
				prop.load(is);

				// 从属性文件取出对应的内容字段,并创建出相应的连接对象

				conn = DriverManager.getConnection(prop.getProperty("url"),
						prop.getProperty("user"), prop.getProperty("pass"));

			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		// 返回数据库连接对象
		return conn;
	}
	
	//关闭资源,
	public static void release(Statement pstmt,ResultSet rs){
		if(rs!=null){
			try {
				rs.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		if(pstmt!=null){
			try {
				pstmt.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}

}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值