${error}
Servlet 做逻辑判断是否密码用户正确
1.将数据存在对象中
2.调用对象方法判断
3.成功则重定向到新的页面
4.失败返回此页面
package com.softeem.servlet;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.softeem.dao.imp.BlogDAOImp;
import com.softeem.dao.imp.LoginInfoDAOImp;
import com.softeem.dto.Blog;
import com.softeem.dto.LoginInfo;
import com.softeem.utils.TypeTools;
@WebServlet("/login")
public class LoginAdminServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public LoginAdminServlet() {
super();
// TODO Auto-generated constructor stub
}
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doPost(request, response);
}
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
request.setCharacterEncoding("utf-8");
response.setCharacterEncoding("utf-8");
response.setContentType("text/html;charset=utf-8");
String flag = request.getParameter("flag");
switch (flag) {
case "loginpass":
CommentLoginpass(request, response);
break;
case "updatepassword":
CommentUpdatePassword(request, response);
break;
}
}
private void CommentUpdatePassword(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String userid = request.getParameter("userid");
int blogid = TypeTools.getInt(userid);
String oldpassword = request.getParameter("old_password");
String newpassword = request.getParameter("new_password");
String nextpassword = request.getParameter("newtwo_password");
Blog bg = new Blog();
bg.setBlogid(blogid);
bg.setPassword(nextpassword);
BlogDAOImp bgp = new BlogDAOImp();
if (newpassword.equals(nextpassword)) {
if (newpassword.equals(oldpassword)) {
if (bgp.update(bg)) {
request.setAttribute("success", "修改成功,重新登陆");
response.sendRedirect("login.jsp");
} else {
request.setAttribute("error", "网络问题");
request.getRequestDispatcher("index.jsp").forward(request, response);
}
} else {
request.setAttribute("error", "与源密码一样了!");
request.getRequestDispatcher("index.jsp").forward(request, response);
}
} else {
request.setAttribute("error", "两次密码不一致");
request.getRequestDispatcher("index.jsp").forward(request, response);
}
}
private void CommentLoginpass(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
System.out.println("登陆!");
String username = request.getParameter("username");
String password = request.getParameter("userpwd");
Blog bg = new Blog();
bg.setUsername(username);
bg.setPassword(password);
BlogDAOImp bgp = new BlogDAOImp();
bg = bgp.queryone(bg);
String lgip = "";
if (request.getHeader("x-forwarded-for") == null) {
lgip = request.getRemoteAddr();
} else {
lgip = request.getHeader("x-forwarded-for");
}
String flag = "";
if (bg.getPassword().equals(password)) {
flag = "成功";
} else {
flag = "失败";
}
LoginInfo lg = new LoginInfo();
lg.setLogininfoip(lgip);
lg.setBlog(bg);
lg.setLogininfoflag(flag);
LoginInfoDAOImp lgp = new LoginInfoDAOImp();
if (lg != null) {
if (lgp.save(lg)) {
request.setAttribute("success", "数据存在成功");
request.getSession().setAttribute("logininfo", lg);
// System.out.println(bg.getUsername()+"-----"+bg.getPassword());
// response.sendRedirect("index.jsp");
} else {
// 密码错误
request.setAttribute("error", "数据错误");
// request.getRequestDispatcher("login.jsp").forward(request,
// response);
}
// MD5 md5=new MD5();
// password=md5.getMD5ofStr(password);
if (bg != null) {
if (bg.getPassword().equals(password)) {
request.setAttribute("success", "密码错误");
request.getSession().setAttribute("blog", bg);
// System.out.println(bg.getUsername()+"-----"+bg.getPassword());
response.sendRedirect("index.jsp");
} else {
// 密码错误
request.setAttribute("error", "密码错误");
request.getRequestDispatcher("login.jsp").forward(request, response);
}
} else {
request.setAttribute("error", "账户错误");
request.getRequestDispatcher("login.jsp").forward(request, response);
}
}
}
}
对象方法
package com.softeem.dto;
import java.util.Date;
public class Blog {
private int blogid; // 用户编号
private String username; // 用户名
private String password; // 用户密码
private String nickname; // 博客昵称
private String description; // 博客描述
private String email; // 电子邮件
private Date registerTime; // 注册日期
private int artileNum; // 发表的日志数
public Blog() {
}
public Blog(int blogid, String username, String password, String nickname,
String description, String email, Date registerTime, int artileNum) {
super();
this.blogid = blogid;
this.username = username;
this.password = password;
this.nickname = nickname;
this.description = description;
this.email = email;
this.registerTime = registerTime;
this.artileNum = artileNum;
}
public int getBlogid() {
return blogid;
}
public void setBlogid(int blogid) {
this.blogid = blogid;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getNickname() {
return nickname;
}
public void setNickname(String nickname) {
this.nickname = nickname;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public Date getRegisterTime() {
return registerTime;
}
public void setRegisterTime(Date registerTime) {
this.registerTime = registerTime;
}
public int getArtileNum() {
return artileNum;
}
public void setArtileNum(int artileNum) {
this.artileNum = artileNum;
}
}
连接数据库
package com.softeem.db;
import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.List;
import java.util.Properties;
import org.apache.tomcat.dbcp.dbcp2.BasicDataSource;
/**
* DBCP数据库连接工具类
* 依赖
* 1.mysql驱动
* 2.dbcp相关插件
*
*
*/
public class DBUtils {
//连接数据库基本属性
private static String driverClass;
private static String url;
private static String username;
private static String password;
//连接池属性
private static int initSize=10;
private static int maxSize=50;
private static int maxIdle=30;
private static long maxWait=10000;
//数据源
private static BasicDataSource bds;
//初始化数据源配置
static{
init();
}
public static void init(){
try {
//创建数据源对象
bds = new BasicDataSource();
//加载属性文件,获取属性信息
Properties props = new Properties();
props.load(DBUtils.class.getResourceAsStream("jdbc.properties"));
driverClass = props.getProperty("driver");
url = props.getProperty("url");
username = props.getProperty("user");
password = props.getProperty("password");
initSize = Integer.parseInt(props.getProperty("initSize"));
maxSize = Integer.parseInt(props.getProperty("maxSize"));
maxIdle = Integer.parseInt(props.getProperty("maxIdle"));
maxWait = Long.parseLong(props.getProperty("maxWait"));
//设置驱动类路径
bds.setDriverClassName(driverClass);
//设置url
bds.setUrl(url);
//设置用户名
bds.setUsername(username);
//设置密码
bds.setPassword(password);
//设置初始连接数
bds.setInitialSize(initSize);
//设置最大连接
bds.setMaxTotal(maxSize);
//设置最大闲置连接数
bds.setMaxIdle(maxIdle);
//等待获取连接的最大时间(MS)
bds.setMaxWaitMillis(maxWait);
} catch (Exception e) {
e.printStackTrace();
}
}
//获取连接
public static Connection getConn(){
try {
if(bds == null || bds.isClosed()){
init();
}
return bds.getConnection();
} catch (SQLException e) {
e.printStackTrace();
}
return null;
}
//封装资源回收的方法
public static void close(ResultSet rs,Statement stat,Connection conn){
try {
if(rs != null) rs.close();
if(stat != null) stat.close();
if(conn != null)conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
/**
* 通用增删改
* @param conn
* @param sql
* @param objs
* @return
*/
public static boolean execUpdate(Connection conn,String sql,Object ...objs){
try {
PreparedStatement ps = conn.prepareStatement(sql);
for (int i = 0; i < objs.length; i++) {
ps.setObject(i+1, objs[i]);
}
int i = ps.executeUpdate();
return i>0 ? true:false;
} catch (SQLException e) {
e.printStackTrace();
}
return false;
}
/**
* C++钩子函数 回调函数
* 集合查询
* @param sql
* @param call
* @param params
* @return
*/
public static List queryList(String sql,CallBack call,Object...params){
Connection conn = DBUtils.getConn();
PreparedStatement ps = null;
try {
ps = conn.prepareStatement(sql);
for(int i = 0;i T queryOne(String sql,CallBack call,Object...params)
{
Connection conn = DBUtils.getConn();
PreparedStatement ps = null;
try {
ps = conn.prepareStatement(sql);
for(int i = 0;i{
// default List getDatas(ResultSet rs){
// return null;
// }
// default T getData(ResultSet rs){
// return null;
// }
// }
//jdk1.8以下使用抽象类
public static abstract class CallBack{
public List getDatas(ResultSet rs){
return null;
}
public T getData(ResultSet rs){
return null;
}
}
}
数据库定义文件
####mysql connection info####
driver = com.mysql.jdbc.Driver
url = jdbc:mysql://127.0.0.1:3306/myselfblog
user = root
password = 1234567890
####MSSQLServer connection info####
#driver = com.microsoft.sqlserver.jdbc.SQLServerDriver
#url = jdbc:sqlserver://127.0.0.1:1433;databaseName=test
#user = sa
#password = 123456
####Oracle connection info####
#driver = com.oracle.driver.OracleDriver
#url = jdbc:oracle:thin:@127.0.0.1:1521:orcl
#user = scott
#password = 123456
##pool config###
initSize = 10
maxSize = 50
maxIdle = 30
maxWait = 10000
类的封装
package com.softeem.dto;
import java.util.Date;
public class Blog {
private int blogid; // 用户编号
private String username; // 用户名
private String password; // 用户密码
private String nickname; // 博客昵称
private String description; // 博客描述
private String email; // 电子邮件
private Date registerTime; // 注册日期
private int artileNum; // 发表的日志数
public Blog() {
}
public Blog(int blogid, String username, String password, String nickname,
String description, String email, Date registerTime, int artileNum) {
super();
this.blogid = blogid;
this.username = username;
this.password = password;
this.nickname = nickname;
this.description = description;
this.email = email;
this.registerTime = registerTime;
this.artileNum = artileNum;
}
public int getBlogid() {
return blogid;
}
public void setBlogid(int blogid) {
this.blogid = blogid;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getNickname() {
return nickname;
}
public void setNickname(String nickname) {
this.nickname = nickname;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public Date getRegisterTime() {
return registerTime;
}
public void setRegisterTime(Date registerTime) {
this.registerTime = registerTime;
}
public int getArtileNum() {
return artileNum;
}
public void setArtileNum(int artileNum) {
this.artileNum = artileNum;
}
}