登录流程
登录练习:
1、创建登录页面
创建Servlet进行登录页面请求处理
2、点击登录完成登录操作
浏览器发送请求到服务器(用户信息+其他数据)
服务器调用对应的servlet进行处理
设置响应编码格式
获取请求信息
处理请求信息
响应处理结果
3、在servlet中完成用户登录校验
需要连接数据库(在mySql中创建用户表)
使用MVC思想完成
创建Servlet进行登录页面请求处理
package com.dym.servlet;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class PageServlet extends HttpServlet {
@Override
protected void service(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
//设置相应编码格式
resp.setContentType("text/html;charset=utf-8");
//获取请求信息
//处理请求
//响应处理结果
resp.getWriter().write("");
resp.getWriter().write("");
resp.getWriter().write("");
resp.getWriter().write("");
resp.getWriter().write("");
resp.getWriter().write("");
resp.getWriter().write("");
}
}
浏览器发送请求到服务器(用户信息+其他数据)
服务器调用对应的servlet进行处理
设置响应编码格式
获取请求信息
处理请求信息
响应处理结果
User.java
package com.dym.pojo;
public class User {
private int uid;
private String uname;
private String pwd;
public int getUid() {
return uid;
}
public void setUid(int uid) {
this.uid = uid;
}
public String getUname() {
return uname;
}
public void setUname(String uname) {
this.uname = uname;
}
public String getPwd() {
return pwd;
}
public void setPwd(String pwd) {
this.pwd = pwd;
}
@Override
public String toString() {
return "User [uid=" + uid + ", uname=" + uname + ", pwd=" + pwd + "]";
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((pwd == null) ? 0 : pwd.hashCode());
result = prime * result + uid;
result = prime * result + ((uname == null) ? 0 : uname.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
User other = (User) obj;
if (pwd == null) {
if (other.pwd != null)
return false;
} else if (!pwd.equals(other.pwd))
return false;
if (uid != other.uid)
return false;
if (uname == null) {
if (other.uname != null)
return false;
} else if (!uname.equals(other.uname))
return false;
return true;
}
public User() {
super();
// TODO Auto-generated constructor stub
}
public User(int uid, String uname, String pwd) {
super();
this.uid = uid;
this.uname = uname;
this.pwd = pwd;
}
}
LoginService.java
package com.dym.service;
import com.dym.pojo.User;
public interface LoginService {
User checkLoginService(String uname,String pwd);
}
LoginServiceImpl.java
package com.dym.service.impl;
import com.dym.dao.LoginDao;
import com.dym.dao.impl.LoginDaoImpl;
import com.dym.pojo.User;
import com.dym.service.LoginService;
public class LoginServiceImpl implements LoginService{
//创建Dao层对象
LoginDao ld=new LoginDaoImpl();
@Override
public User checkLoginService(String uname, String pwd) {
return ld.checkLoginDao(uname, pwd);
}
}
LoginDao.java
package com.dym.dao;
import com.dym.pojo.User;
public interface LoginDao {
//根据用户名和密码获取用户信息
User checkLoginDao(String uname,String pwd);
//根据uid获取用户信息
User checkUidDao(String uid);
}
LoginDaoImpl.java
package com.dym.dao.impl;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import com.dym.dao.LoginDao;
import com.dym.pojo.User;
public class LoginDaoImpl implements LoginDao{
@Override
public User checkLoginDao(String uname, String pwd) {
//声明jdbc对象
Connection conn=null;
PreparedStatement ps=null;
ResultSet rs=null;
//声明数据存储对象
User u=null;
try {
//加载驱动
Class.forName("com.mysql.jdbc.Driver");
//获取连接对象
conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/wollo","root","123456");
//创建sql命令
String sql="select * from t_user where uname=? and pwd=?";
//创建sql命令对象
ps=conn.prepareStatement(sql);
//给占位符赋值
ps.setString(1, uname);
ps.setString(2, pwd);
//执行
rs=ps.executeQuery();
//遍历执行结果
while(rs.next()){
u=new User();
u.setUid(rs.getInt("uid"));
u.setUname(rs.getString("uname"));
u.setPwd(rs.getString("pwd"));
}
} catch (Exception e) {
e.printStackTrace();
}finally{
//关闭资源
try {
rs.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
ps.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
conn.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return u;
}
//根据UID获取用户信息
@Override
public User checkUidDao(String uid) {
//声明Jdbc对象
Connection conn=null;
PreparedStatement ps=null;
ResultSet rs=null;
//声明数据存储对象
User u=null;
try {
//加载驱动
Class.forName("com.mysql.jdbc.Driver");
//获取连接对象
conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/407","root","1234");
//创建Sql命令
String sql="select * from t_user where uid=?";
//创建Sql命令对象
ps=conn.prepareStatement(sql);
//给占位符赋值
ps.setString(1, uid);
//执行
rs=ps.executeQuery();
//遍历执行结果
while(rs.next()){
u=new User();
u.setUid(rs.getInt("uid"));
u.setUname(rs.getString("uname"));
u.setPwd(rs.getString("pwd"));
}
//关闭资源
} catch (Exception e) {
e.printStackTrace();
}finally{
try {
rs.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
ps.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
conn.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
//返回
return u;
}
}
LoginServlet.java
package com.dym.servlet;
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.dym.pojo.User;
import com.dym.service.LoginService;
import com.dym.service.impl.LoginServiceImpl;
public class LoginServlet extends HttpServlet {
@Override
protected void service(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
//设置响应编码格式
resp.setContentType("text/html;charset=utf-8");
//获取请求信息
String uname=req.getParameter("uname");
uname=new String(uname.getBytes("iso8859-1"),"utf-8"); //解决了乱码问题
String pwd=req.getParameter("pwd");
System.out.println(uname+":"+pwd);
//处理请求信息
//获取业务层对象
LoginService ls=new LoginServiceImpl();
User u=ls.checkLoginService(uname, pwd);
System.out.println(u);
//响应处理结果
if(u!=null){
resp.getWriter().write("登录成功");
}else{
resp.getWriter().write("登录失败");
}
}
}