实现简单的登录功能(1.0)
项目图
Dao层
package com. example. dao;
import com. example. domain. User;
public interface Dao {
User findUserandPassword ( String name, String password) ;
}
DaoImpl层
package com. example. dao. impl;
import java. sql. Connection;
import java. sql. DriverManager;
import java. sql. PreparedStatement;
import java. sql. ResultSet;
import java. sql. SQLException;
import com. example. dao. Dao;
import com. example. domain. User;
public class DaoImpl implements Dao {
@Override
public User findUserandPassword ( String name, String password) {
Connection conn = null ;
PreparedStatement ps = null ;
ResultSet rs = null ;
try {
Class. forName ( "com.mysql.jdbc.Driver" ) ;
conn = DriverManager. getConnection ( "jdbc:mysql:///test" , "root" , "123456" ) ;
ps = conn. prepareStatement ( "select * from user where username=? and password=?" ) ;
ps. setString ( 1 , name) ;
ps. setString ( 2 , password) ;
rs = ps. executeQuery ( ) ;
while ( rs. next ( ) ) {
User u = new User ( ) ;
u. setUid ( rs. getInt ( "uid" ) ) ;
u. setUname ( rs. getString ( "username" ) ) ;
u. setPassword ( rs. getString ( "password" ) ) ;
return u;
}
} catch ( Exception e) {
e. printStackTrace ( ) ;
return null ;
} finally {
try {
rs. close ( ) ;
} catch ( SQLException e) {
e. printStackTrace ( ) ;
}
try {
conn. close ( ) ;
} catch ( SQLException e) {
e. printStackTrace ( ) ;
}
try {
ps. close ( ) ;
} catch ( SQLException e) {
e. printStackTrace ( ) ;
}
}
return null ;
}
}
User层
package com. example. domain;
public class User {
private Integer uid;
private String username;
private String password;
public Integer getUid ( ) {
return uid;
}
public void setUid ( Integer uid) {
this . uid = uid;
}
public String getUname ( ) {
return username;
}
public void setUname ( String uname) {
this . username = uname;
}
public String getPassword ( ) {
return password;
}
public void setPassword ( String password) {
this . password = password;
}
}
Service层
package com. example. service;
import com. example. domain. User;
public interface Service {
User login ( String name, String password) ;
}
ServiceImpl层
package com. example. service. impl;
import com. example. dao. Dao;
import com. example. dao. impl. DaoImpl;
import com. example. domain. User;
import com. example. service. Service;
public class ServiceImpl implements Service {
private Dao dao = new DaoImpl ( ) ;
@Override
public User login ( String name, String password) {
User u = dao. findUserandPassword ( name, password) ;
if ( u == null )
throw new RuntimeException ( "bucunzai" ) ;
return u;
}
}
LoginService层
package com. example. web;
import java. io. IOException;
import javax. servlet. ServletException;
import javax. servlet. http. HttpServlet;
import javax. servlet. http. HttpServletRequest;
import javax. servlet. http. HttpServletResponse;
import com. example. service. Service;
import com. example. service. impl. ServiceImpl;
public class LoginServlet extends HttpServlet {
private static final long serialVersionUID = 1 L;
protected void doGet ( HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response. getWriter ( ) . append ( "Served at: " ) . append ( request. getContextPath ( ) ) ;
}
protected void doPost ( HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String username = request. getParameter ( "username" ) ;
String password= request. getParameter ( "password" ) ;
Service service = new ServiceImpl ( ) ;
try {
service. login ( username, password) ;
System. out. println ( "登录成功" ) ;
} catch ( Exception e) {
System. out. println ( "用户名或密码错误,登录失败" ) ;
}
}
}
Login.html
<!DOCTYPE html>
< html>
< head>
< meta charset = " UTF-8" >
< title> Insert title here</ title>
</ head>
< body>
<form action="dologin" method = "post">
用户名: <input type = "text" name = "username">
密 码: <input type="password" name = "password">
<input type = "submit" value = "登录">
</ form>
</ body>
</ html>
web.xml
<?xml version="1.0" encoding="UTF-8"?>
< web-app xmlns: xsi= " http://www.w3.org/2001/XMLSchema-instance" xmlns = " http://java.sun.com/xml/ns/javaee" xsi: schemaLocation= " http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id = " WebApp_ID" version = " 2.5" >
< display-name> web02</ display-name>
< welcome-file-list>
< welcome-file> index.html</ welcome-file>
< welcome-file> index.htm</ welcome-file>
< welcome-file> index.jsp</ welcome-file>
< welcome-file> default.html</ welcome-file>
< welcome-file> default.htm</ welcome-file>
< welcome-file> default.jsp</ welcome-file>
</ welcome-file-list>
< servlet>
< description> </ description>
< display-name> LoginServlet</ display-name>
< servlet-name> LoginServlet</ servlet-name>
< servlet-class> com.example.web.LoginServlet</ servlet-class>
</ servlet>
< servlet-mapping>
< servlet-name> LoginServlet</ servlet-name>
< url-pattern> /dologin</ url-pattern>
</ servlet-mapping>
</ web-app>
数据库
谢谢大家的欣赏和关注!!!