登录练习:
1创建登录页面
创建servlet进行登录页面请求
2点击登录完成操作
浏览器发送请求到服务器(用户信息+其他数据 )
3服务器调用对应的servlet进行处理
设置响应编码格式
获取请求信息
处理请求信息
响应处理结果
4在servlet中完成登录校验
需要连接数据库(需要创建用户)
使用mvc思想
1、创建登录页面
package com.bjsxt.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.sun.mail.handlers.text_html;
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("<html>");
resp.getWriter().write("<head>");
resp.getWriter().write("</head>");
resp.getWriter().write("<body>");
resp.getWriter().write("<form action='login' method='get'>");
resp.getWriter().write("用户名:<input type='text' name='uname' value=''><br/>" );
resp.getWriter().write("密码:<input type='password' name='pwd' value=''><br/>" );
resp.getWriter().write("<input type='submit' value='提交'><br/>" );
resp.getWriter().write("</form>");
resp.getWriter().write("</body>");
resp.getWriter().write("</html>");
//处理请求
//响应请求结果
}
}
登录服务器
loginservlet
package com.bjsxt.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.bjsxt.pojo.User;
import com.bjsxt.service.LoginService;
import com.bjsxt.service.impl.LoginServicelmpl;
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");
String pwd=req.getParameter("pwd");
System.out.println(uname+":"+pwd);
//处理请求信息
//获取业务层信息
LoginService ls=new LoginServicelmpl();
User u=ls.checkLoginService(uname, pwd);
System.out.println(u);
//响应处理结果
if(u!=null){
resp.getWriter().write("登录成功");
}else{
resp.getWriter().write("登录失败");
}
}
}
service下面写接口
package com.bjsxt.service;
import com.bjsxt.pojo.User;
public interface LoginService {
User checkLoginService(String uname,String pwd);
}
建立实体类专门用来存用户数据
package com.bjsxt.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();
}
public User(int uid,String uname,String pwd){
super();
this.uid=uid;
this.uname=uname;
this.pwd=pwd;
}
}
实现类
过度翻转
package com.bjsxt.service.impl;
import com.bjsxt.dao.LoginDao;
import com.bjsxt.dao.impl.LoginDaImpl;
import com.bjsxt.pojo.User;
import com.bjsxt.service.LoginService;
public class LoginServicelmpl implements LoginService{
//创建DAO层对象
LoginDao Id=new LoginDaImpl();
@Override
public User checkLoginService(String uname, String pwd) {
// TODO Auto-generated method stub
return Id.checkLoginDao(uname, pwd);
}
}
直接浏览数据到操作层
package com.bjsxt.dao;
import com.bjsxt.pojo.User;
public interface LoginDao {
User checkLoginDao(String uname,String pwd);
}
数据库层的接口
package com.bjsxt.dao.impl;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import javax.crypto.spec.PSource;
import com.bjsxt.dao.LoginDao;
import com.bjsxt.pojo.User;
import com.mysql.jdbc.Connection;
public class LoginDaImpl implements LoginDao{
@Override
public User checkLoginDao(String uname, String pwd) {
//声明jdbc对象
java.sql.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", "root");
//创建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();
// TODO: handle exception
}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;
}
}
记得要拿jar包放入到lib文件夹中