1.项目目录结构:
访问注册地址:http://localhost:8080/UserLogin/WebRoot/login.jsp
页面跳转到reg.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
Insert title here
注释:action="http://localhost:8080/UserLogin/RegServer":访问名字叫RegServer的servlet
web.xml
RegServer
com.lyq.service.RegServer
RegServer
/RegServer
跳转到servlet 的实现类的实现类里面:
package com.lyq.service;
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.lyq.model.User;
import com.lyq.model.dao.UserDao;
/**
* 用户注册
*/
public class RegServer extends HttpServlet {
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// 获取用户名
String username = request.getParameter("username");
System.out.println(username);
String password = request.getParameter("password");
String sex = request.getParameter("sex");
String photo = request.getParameter("photo");
String tel = request.getParameter("tel");
String email = request.getParameter("email");
UserDao userDao = new UserDao();
if (username != null && !username.isEmpty()) {
if (userDao.userIsExist(username)) {
User user = new User();
user.setUsername(username);
user.setPassword(password);
user.setSex(sex);
user.setPhoto(photo);
user.setTel(tel);
user.setEmail(email);
userDao.saveUser(user);
request.setAttribute("info", "恭喜,注册成功!
");
} else {
request.setAttribute("info", "错误,用户已存在!
");
}
}
// 转发到message.jsp
request.getRequestDispatcher("WebRoot/message.jsp").forward(request,
response);
}
}
注释:转发到message.jsp时:由于我是在WebContent下面新建的文件夹WebRoot所以我的路径是:WebRoot/message.jsp。有一点要注意 servlet直接访问WebContent文件夹下的jsp的
UserDao:数据库操作类
package com.lyq.model.dao;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import com.lyq.util.DataBaseUtil;
import com.lyq.model.User;
public class UserDao {
public boolean userIsExist(String username) {
// 获取数据库连接Conntion
Connection conn = DataBaseUtil.getConnection();
// 根据指定用户名查询用户信息
String sql = "select * from tb_user where username=?";
try {
// 获取preparedStatement对象
PreparedStatement ps = conn.prepareStatement(sql);
// 对用户对象属性赋值
ps.setString(1, username);
// 执行查询获取结果集
ResultSet rs = ps.executeQuery();
// 判定结果集是否有效
if (!rs.next()) {
// 如果无效则证明此用户名可用
return true;
}
// 释放ResultSet对象的数据库和JDBC资源
rs.close();
// 释放此PreparedStatement对象的数据库和JDBC资源
ps.close();
} catch (Exception e) {
// 关闭数据库连接
DataBaseUtil.closeConnection(conn);
}
return false;
}
public void saveUser(User user) {
// 获取数据库连接 Connection对象
Connection conn = DataBaseUtil.getConnection();
// 插入用户注册信息的SQL语句
String sql = "insert into tb_user(username,password,sex,tel,photo,email) values(?,?,?,?,?,?)";
try {
// 获取PreparedStatement对象
PreparedStatement ps = conn.prepareStatement(sql);
// 对SQL语句的占位符参数进行动态赋值
ps.setString(1, user.getUsername());
ps.setString(2, user.getPassword());
ps.setString(3, user.getSex());
ps.setString(4, user.getTel());
ps.setString(5, user.getPhoto());
ps.setString(6, user.getEmail());
// 执行更新
ps.executeUpdate();
// 释放此PreparedStatement对象的数据库和JDBC资源
ps.close();
} catch (Exception e) {
e.printStackTrace();
} finally {
// 关闭数据库了连接
DataBaseUtil.closeConnection(conn);
}
}
public User login(String username, String password) {
User user = null;
// 获取数据库连接 Connection对象
Connection conn = DataBaseUtil.getConnection();
// 根据用户名及密码查询用户信息
String sql = "select * from tb_user where username = ? and password = ?";
try {
// 获取PreparedStatement对象
PreparedStatement ps = conn.prepareStatement(sql);
// 对SQL语句的占位符参数进行动态赋值
ps.setString(1,username);
ps.setString(2,password);
System.out.println(sql);
// 执行查询获取结果集
ResultSet rs = ps.executeQuery();
// 判断结果集是否有效
if (rs.next()) {
System.out.println(rs.getString("username"));
user = new User();
user.setId(rs.getInt("id"));
user.setUsername(rs.getString("username"));
user.setPassword(rs.getString("password"));
user.setSex(rs.getString("sex"));
user.setTel(rs.getString("tel"));
user.setPhoto(rs.getString("photo"));
user.setEmail(rs.getString("email"));
}
// 释放ResultSet对象的数据库和JDBC资源
rs.close();
// 释放此PreparedStatement对象的数据库和JDBC资源
ps.close();
} catch (Exception e) {
// 关闭数据库了连接
DataBaseUtil.closeConnection(conn);
}
return user;
}
}
注释:select * from tb_user where username = ? and password = ?中问号前面有空格不然会报错:必须声明标量变量"@P0and"
数据库连接类DataBaseUtil不说了,上篇文章说了的。
用户就注册成功了会跳转到http://localhost:8080/UserLogin/RegServer页面如图:
登陆访问地址:http://localhost:8080/UserLogin/WebRoot/login.jsp
登陆jsp:
登陆servlet:
package com.lyq.service;
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.lyq.model.User;
import com.lyq.model.dao.UserDao;
/*
* 用户登陆
*/
public class LoginServer extends HttpServlet {
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// 获取用户名
String username = request.getParameter("username");
System.out.println(username);
String password = request.getParameter("password");
UserDao userDao = new UserDao();
// 根据密码查询用户
User user = userDao.login(username, password);
if (user != null) {
// 将用户放入session中
request.getSession().setAttribute("user", user);
// 转发到result页面
request.getRequestDispatcher("WebRoot/message.jsp").forward(
request, response);
} else {
request.setAttribute("info", "密码用户名有误!");
request.getRequestDispatcher("WebRoot/message.jsp").forward(
request, response);
}
}
}
退出servlet:
package com.lyq.service;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import com.lyq.model.User;
/**
* 用户退出
*
* @author Administrator
*
*/
public class UserExitServlet extends HttpServlet {
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// 获取session
HttpSession session = request.getSession();
// 获取用户对象
User user = (User) session.getAttribute("user");
if (user != null) {
// 将用户对象逐出session
session.removeAttribute("user");
// 设置提示信息
request.setAttribute("info", user.getUsername() + "退出");
}
// 转发到message页面
request.getRequestDispatcher("WebRoot/message.jsp").forward(request,
response);
}
}
提示页面:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ page import="com.lyq.model.User"%>
Insert title here
<%
//获取提示信息
String info=(String)request.getAttribute("info");
//如果提示信息不为空则输出提示信息
if(info !=null){
out.print(info);
}
//获取用户登陆的用户信息
User user=(User)session.getAttribute("user");
//判断用户是否登陆
if(user !=null){
%>
<%
}else{
out.print("
对不起,你没登陆");
}
%>
注释:1.<%@ page import="com.lyq.model.User"%> 导入user类
2.<%%>里面写的Java代码
3.http://localhost:8080/UserLogin/UserExitServlet 调用退出的servlet
注意:访问路径的问题,我是弄了好久才弄明白的
源码下载下载