案例:用户登录(html--servlet--mysql)

案例:用户登录(html–servlet–mysql)

需要用到的内容:HTML+CSS,Tomcat,Servlet,DBUtils,c3p0,MySQL

1、创建web项目student_management

1)导入jar包,放入WEB-INF下的lib文件夹中—c3p0,dbutils,mysql,
2)在src 下创建包
com.offcn.dao----放操作数据库的文件,即增删查改
com.offcn.javabean—对应数据库的实体类
com.offcn.servlet
3)c3p0的配置文件放在src下,注意其中的一些内容要根据具体数据库来修改

2、设计登录页面,WebContent下创建login.html文件


<html>
<head>
<meta charset="UTF-8">
<title>学生管理系统登录title>
<style type="text/css">
	form {
		width: 300px;
		heigth:200px;
		border:1px,solid,black;
		margin:auto;
	}
style>
head>
<body>
	<form action="admin" method="post">
	<h2>学生管理系统h2>
	账号:<input type="text" name="username" /><br/>
	密码:<input type="password" name="password"/><br/>
	<input type="submit" value="登录">    
	<a href="">去注册a>
	form>
body>
html>

3、创建数据库及数据表

CREATE DATABASE school;
CREATE TABLE admin (
	aid INT(10) PRIMARY KEY AUTO_INCREMENT,
	aname VARCHAR(20) NOT NULL,
	apwd VARCHAR(20) NOT NULL
);
INSERT INTO admin VALUES(1,'JiXiang','123456');

4、在javabean包中创建Admin.java文件

package com.offcn.javabean;

public class Admin {
	private int aid;
	private String aname;
	private String apwd;
	public Admin() {

	}
	public Admin(int aid, String aname, String apwd) {
		super();
		this.aid = aid;
		this.aname = aname;
		this.apwd = apwd;
	}
	public int getAid() {
		return aid;
	}
	public void setAid(int aid) {
		this.aid = aid;
	}
	public String getAname() {
		return aname;
	}
	public void setAname(String aname) {
		this.aname = aname;
	}
	public String getApwd() {
		return apwd;
	}
	public void setApwd(String apwd) {
		this.apwd = apwd;
	}
	
}

5、在dao包中创建AdminDao.java文件

package com.offcn.dao;

import java.sql.SQLException;

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

import com.mchange.v2.c3p0.ComboPooledDataSource;
import com.offcn.javabean.Admin;

public class AdminDao {
	public Admin getAdminByNameAndPwd(String aname,String apwd) {
		Admin admin = null;
		ComboPooledDataSource ds = new ComboPooledDataSource();
		QueryRunner qr = new QueryRunner(ds);
		String sql = "select * from admin where aname = ? and apwd = ?";
		try {
			admin = qr.query(sql,new BeanHandler<>(Admin.class),aname,apwd);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return admin;
	} 
}

6、在servlet包中创建AdminServlet.java文件

package com.offcn.servlet;

import java.io.IOException;
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.offcn.dao.AdminDao;
import com.offcn.javabean.Admin;

public class AdminServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
    public AdminServlet() {

    }

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String aname = request.getParameter("username");
		String apwd = request.getParameter("password");
		AdminDao ad = new AdminDao();
		Admin admin = ad.getAdminByNameAndPwd(aname, apwd);
		if(admin==null) {
			response.sendRedirect("login.html");
		}else {
			response.sendRedirect("index.html");
		}
	}
}

7、WebContent下创建index.html文件(此为非必要,为了第6步增设的,实际项目中另说)


<html>
<head>
<meta charset="UTF-8">
<title>Insert title heretitle>
head>
<body>
	恭喜你,登录成功!
body>
html>

8、在web.xml文件中对AdminServlet.java进行配置


<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd" id="WebApp_ID" version="4.0">
  <display-name>student_managementdisplay-name>
  <welcome-file-list>
    <welcome-file>index.htmlwelcome-file>
    <welcome-file>index.htmwelcome-file>
    <welcome-file>index.jspwelcome-file>
    <welcome-file>default.htmlwelcome-file>
    <welcome-file>default.htmwelcome-file>
    <welcome-file>default.jspwelcome-file>
  welcome-file-list>
  <servlet>
  	<servlet-name>AdminServletservlet-name>
  	<servlet-class>com.offcn.servlet.AdminServletservlet-class>
  servlet>
  <servlet-mapping>
  <servlet-name>AdminServletservlet-name>
  <url-pattern>/adminurl-pattern>
  servlet-mapping>
web-app>

9、启动Srevers并运行login.html

你可能感兴趣的:(数据库MySQL,mysql,servlet,tomcat)