首先在项目src下创建包com.lzy.servlet。在该包下创建servlet,虚拟名称为/ma。
package com.lzy.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.lzy.pojo.User;
import com.lzy.service.UserService;
import com.lzy.service.imp.UserServiceImpl;
public class UserServlet extends HttpServlet {
User u=null;
@Override
protected void service(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
//设置请求编码格式
req.setCharacterEncoding("utf-8");
//设置响应编码格式
resp.setCharacterEncoding("text/html;charset=utf-8");
//获取操作符
String oper=req.getParameter("oper");
if("login".equals(oper)){
//响应登陆操作
checkUserLogin(req,resp);
}else if("reg".equals(oper)){
//响应注册操作
}else{
System.out.println("响应错误"+oper);
}
}
private void checkUserLogin(HttpServletRequest req, HttpServletResponse resp) {
//获取请求信息
String uname=req.getParameter("uname");
String pwd=req.getParameter("pwd");
//处理请求信息
//校验
UserService us=new UserServiceImpl();
u=us.checkUserLoginService(uname, pwd);
System.out.println(uname+":"+pwd);
}
}
21 23行为设置请求/响应编码格式
25-34行是为了处理不同的请求,在主页面上有登录请求也有注册请求,不同的请求有不同的响应方法,所以此时在login.jsp中加入隐藏标签,代码如下:
<form action="user" method="post">
<input type="hidden" name ="oper" value="login"/>
<ul>
<li><input name="uname" type="text" placeholder="用户名" class="loginuser" value="" /></li>
<li><input name="pwd" type="password" placeholder="密码" class="loginpwd" value="" /></li>
<li><input name="" type="submit" class="loginbtn" value="登录" onclick="javascript:window.location='main.html'" /><label><input name="" type="checkbox" value="" checked="checked" />记住密码</label><label><a href="#">忘记密码?</a></label></li>
</ul>
</form>
加入: <input type="hidden" name ="oper" value="login"/>
所以当oper等于login时就调用登录请求,当oper等于reg(暂时还没有写注册请求,reg暂时用不到)时就调用注册请求
36-47为实现登录请求的方法
38 39为获取请求信息,也就是用户输入的账号密码。
40-43校验用户输入的账号密码是否正确(也就是在数据库中进行校验,查看用户的账号密码是否正确)
如何校验用户的账号密码是否正确?要使用MVC思想。
首先把数据库架包导入项目中
然后创建com.lzy.dao包和com.lzy.pojo和com.lzy.service包
servlet调用service进行业务处理
service调用dao完成数据库操作
dao再把数据库操作结果给service,service再继续进行处理,再把结果给servlet,
servlet在根据service的结果决定是请求转发还是重定向显示页面
pojo包为用户实体
首先在pojo包下创建User类
package com.lzy.pojo;
public class User {
private int uid;
private String pwd;
private String uname;
private String sex;
private int age;
private String birth;
public int getUid() {
return uid;
}
public void setUid(int uid) {
this.uid = uid;
}
public String getPwd() {
return pwd;
}
public void setPwd(String pwd) {
this.pwd = pwd;
}
public String getUname() {
return uname;
}
public void setUname(String uname) {
this.uname = uname;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getBirth() {
return birth;
}
public void setBirth(String birth) {
this.birth = birth;
}
@Override
public String toString() {
return "User [uid=" + uid + ", pwd=" + pwd + ", uname=" + uname
+ ", sex=" + sex + ", age=" + age + ", birth=" + birth + "]";
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + age;
result = prime * result + ((birth == null) ? 0 : birth.hashCode());
result = prime * result + ((pwd == null) ? 0 : pwd.hashCode());
result = prime * result + ((sex == null) ? 0 : sex.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 (age != other.age)
return false;
if (birth == null) {
if (other.birth != null)
return false;
} else if (!birth.equals(other.birth))
return false;
if (pwd == null) {
if (other.pwd != null)
return false;
} else if (!pwd.equals(other.pwd))
return false;
if (sex == null) {
if (other.sex != null)
return false;
} else if (!sex.equals(other.sex))
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();
}
public User(int uid, String pwd, String uname, String sex, int age,
String birth) {
super();
this.uid = uid;
this.pwd = pwd;
this.uname = uname;
this.sex = sex;
this.age = age;
this.birth = birth;
}
}
然后在service包下创建接口UserService
package com.lzy.service;
import com.lzy.pojo.User;
public interface UserService {
/**
* 校验用户登录
* @param uname 用户名
* @param pwd 密码
* @return 返回查询到的用户信息
*/
User checkUserLoginService(String uname,String pwd);
}
继续在com.lzy.service下创建包impl
然后在com.lzy.service.impl包下创建类UserServiceImpl来实现UserService接口
package com.lzy.service.imp;
import com.lzy.dao.UserDao;
import com.lzy.dao.impl.UserDaoImpl;
import com.lzy.pojo.User;
import com.lzy.service.UserService;
public class UserServiceImpl implements UserService {
//声明Dao层对象
UserDao ud=new UserDaoImpl();
@Override
public User checkUserLoginService(String uname, String pwd) {
//用户登录
return ud.checkUserLoginDao(uname, pwd);
}
}
再在dao包下创建接口UserDao
package com.lzy.dao;
import com.lzy.pojo.User;
public interface UserDao {
/**
* 根据用户名和密码查询用户信息
* @param uname 用户名
* @param pwd 密码
* @return 返回查询到的用户信息
*/
User checkUserLoginDao(String uname,String pwd);
}
再在com.lzy.dao下建包impl,在com.lzy.dao.impl下创建类UserDaoImpl,用来进行数据库操作
package com.lzy.dao.impl;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import com.lzy.dao.UserDao;
import com.lzy.pojo.User;
public class UserDaoImpl implements UserDao{
@Override
public User checkUserLoginDao(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/test", "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);
//执行sql语句
rs=ps.executeQuery();
//遍历结果
while(rs.next()){
//给变量赋值
u=new User();
u.setUid(rs.getInt("uid"));
u.setPwd(rs.getString("pwd"));
u.setUname(rs.getString("uname"));
u.setSex(rs.getString("sex"));
u.setAge(rs.getInt("age"));
u.setBirth(rs.getString("birth"));
}
} catch (Exception e) {
// TODO Auto-generated catch block
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;
}
}