好记性不如烂笔头

惠普周训(第一天)

Posted on By ZealerDrm

如需转载本网站内容,请标明转载来源,且保持作品完整性

 

package Explorer

loginDemo
   src 
     com.hpe.service
        DBUti.java
     com.hpe.servlet  
        loginServlet.java
   JRE System Library  
   ..........
   WebContent
     WEB-INF
        lib
     error.html
     login.html
     success.html

DBUtil.java

package com.hpe.service;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import com.sun.crypto.provider.RSACipher;

import sun.security.util.Password;

/**
 * 此类为数据库操作类
 * @author ZealerDrm
 *
 */
public class DBUtil {
	private String url = "jdbc:mysql://localhost:3306/login";
	private String user = "root";
	private String password = "root";

	public Connection getConnection() throws Exception{
		//jdbc 加载链接
		Class.forName("com.mysql.jdbc.Driver");
		//获取链接对象
		Connection conn = DriverManager.getConnection(url,user,password);
		System.out.println("连接对象:"+ conn);
		//获取执行sql语句的对象,statement
		Statement sta = conn.createStatement();
		//执行sql,获取结果集
		ResultSet rs = sta.executeQuery("select name,age from t_user");
		//遍历结果集
		while (rs.next()) {
			String name = rs.getString("name");
			int age = rs.getInt("age");
			System.out.println("name=" + name +"; age=" + age);
		}
		//关闭链接
		rs.close();
		sta.close();
		conn.close();
		return null;
	}
	/**
	 *  从数据库获取用户名集合
	 * @return list
	 * @param null
	 */
	public   java.util.List getName() throws Exception{
		//等待后续数据填充
		List<String> nameList = new ArrayList<String>();
		//jdbc 加载链接
		Class.forName("com.mysql.jdbc.Driver");
		//获取链接对象
		Connection conn = DriverManager.getConnection(url,user,password);
		System.out.println("连接对象:"+ conn);
		//获取执行sql语句的对象,statement
		Statement sta = conn.createStatement();
		//执行sql,获取结果集
		ResultSet rs = sta.executeQuery("select name,age from t_user");
		//遍历结果集
		while (rs.next()) {
			String name = rs.getString("name");
			int age = rs.getInt("age");
			System.out.println("name=" + name +"; age=" + age);
			nameList.add(name);// 把结果集数据addlist
		}
		//关闭链接
		rs.close();
		sta.close();
		conn.close();
		return nameList;
	
	}
	public static void main(String[] args) throws Exception{
		DBUtil dbutil = new DBUtil();
		
		dbutil.getConnection();
		List nameList = dbutil.getName();
		for (Iterator iterator = nameList.iterator(); iterator.hasNext();) {
			String name = (String) iterator.next();
			System.out.println("集合对象:name=" + name);
			
		}
		}
	
}	

LoginServlet.java

package com.hpe.servlet;

import java.io.IOException;
import java.util.List;

import javax.security.auth.login.FailedLoginException;
import javax.servlet.Servlet;
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.hpe.service.DBUtil;

import sun.security.util.PropertyExpander.ExpandException;

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

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.getWriter().append("Served at: ").append(request.getContextPath());
		//设计一个页面(name值) 
		request.setCharacterEncoding("utf-8");//解决乱码问题
		String username = request.getParameter("username");
		String sex = request.getParameter("sex");
		
		//连接数据库.
		//如果有登陆用户,跳转到登陆页面	
		DBUtil service = new DBUtil();
		boolean falg = false;
		try{
			List list = service.getName();
			falg = list.contains(username);
		}catch(Exception e){
			e.printStackTrace();
			response.sendRedirect("/loginDemo/error.html");
			return;
		}
		//判断数据库是否有登陆信息
		if(falg){
			response.sendRedirect("/loginDemo/success.html");
		}else{
			response.sendRedirect("/loginDemo/error.html");
		}
		
	//	if (username.equals("admin")) {
	//		response.sendRedirect("/loginDemo/success.html");
	//	}else{
	//		response.sendRedirect("/loginDemo/error.html");
	//	}
		//否则跳转到失败页面
		
		// TODO Auto-generated method stub
		
	}

	/**
	 * @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);
	}

}

login.html

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
	<!-- 表单,实现获取用户提交数据  -->
	<form action="LoginServlet" method="post"> 
		用户名:<input name="username" value="admin" />  <br/>
		性别: <input type="radio" name="sex" value="1"/><input type="radio" name="sex" value="0"/><br/>
			 <input type="submit" value="提交" />
	</form>
</body>
</html>