登录练习:
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文件夹中

最新文章

  1. java多线程--几个多线程面试题小结
  2. java线程池ThreadPoolExecutor使用简介
  3. ShellShock 攻击实验
  4. Add sharing to your app via UIActivityViewController
  5. MySQL: Starting MySQL….. ERROR! The server quit without updating PID file解决办法
  6. Libsvm自定义核函数【转】
  7. sublime删除安装的插件
  8. codeforces #310 div1 A
  9. &lt;!DOCTYPE html PUBLIC &quot;-//W3C//DTD XHTML 1.0 Frameset//EN&quot; &quot;http://www.w3.org/TR/xhtml1/DTD/xhtml1-frameset.dtd&quot;&gt;的含义
  10. 分享一个C#自定义事件的实际应用
  11. ASP.NET MVC 使用MSBuild生成的几个注意事项
  12. 新学的js精集
  13. java 协调同步的线程
  14. myql update from 语句
  15. JSON字符串转C#实体Class类
  16. Django实战(17):ajax !
  17. Objective—C中的排序及Compare陷阱
  18. 【JavaScript】__proto__和prototype的区别和联系【整理】
  19. Ubuntu Tweak (linux下的优化大师)
  20. CCNA2.0笔记_IP连接排错

热门文章

  1. 如何通过Java代码判断当前的环境是否支持JRE 9
  2. vue render {} 对象 说明文档
  3. ADB相关指令实例详解
  4. 公共dao的抽取
  5. OpenCV2:第六章 图像几何变换
  6. MySQL中外键删除、更新
  7. Java ArrayList中去掉相同的元素并保留相同元素中的最后一个
  8. 使用 Pytorch 实现 skip-gram 的 word2vec
  9. poj3710 Christmas Game
  10. mysql单实例多库与多实例单库