您的位置:首页 > Web前端 > JavaScript

jsp+jdbc+Servlet实现登录系统(.servelet+filter)

2018-01-11 22:11 621 查看
登录过滤器LoginFilter

import java.io.IOException;

import javax.servlet.Filter;

import javax.servlet.FilterChain;

import javax.servlet.FilterConfig;

import javax.servlet.ServletException;

import javax.servlet.ServletRequest;

import javax.servlet.ServletResponse;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;

import javax.servlet.http.HttpSession;

public class LoginFilter implements Filter {

public void init(FilterConfig arg0) throws ServletException {

}

public void doFilter(ServletRequest arg0, ServletResponse arg1,
FilterChain arg2) throws IOException, ServletException {

HttpServletRequest request = (HttpServletRequest) arg0;
HttpServletResponse response = (HttpServletResponse) arg1;

request.setCharacterEncoding("utf-8");
response.setContentType("text/html;charset=utf-8");

String uri = request.getRequestURI();
System.out.println("uri: " + uri);

// String url = request.getRequestURL().toString();

// System.out.println("url: " + url);

// 如果访问登录文件夹下的内容,放过
if (uri.startsWith("/ch006/index.jsp") || uri.startsWith("/ch006/login/") || uri.contains("LoginUserServlet")) {
arg2.doFilter(arg0, arg1);
} else {
HttpSession session = request.getSession();
String username = (String)session.getAttribute("loginUser");
if (username != null) {
arg2.doFilter(arg0, arg1);
} else {
String path = "/ch006/login/login.jsp";
response.sendRedirect(path);
}
}
}

public void destroy() {

}

}

loginuserservlet

import java.io.IOException;

import java.io.PrintWriter;

import javax.servlet.RequestDispatcher;

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.soft.dao.UserDAO;

public class LoginUserServlet extends HttpServlet {

public LoginUserServlet() {
super();
}

public void destroy() {

}

public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
this.doPost(request, response);

}

public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {

String username =1 request.getParameter("username");
String password = request.getParameter("password");

// 登录操作,应该访问数据库,查询数据库表中是否含有用户输入的信息!

// 访问DAO对象,进行数据库表的查询操作,如果数据库表中含有登录信息,则成功,否则失败!

UserDAO userDAO = new UserDAO();

boolean res = userDAO.queryUserByNameAndPwd(username, password);

if (res) {
// 登录成功

HttpSession session = request.getSession();
session.setAttribute("loginUser", username);

String path = "login/loginSucc.jsp";
RequestDispatcher dispatcher = request.getRequestDispatcher(path);
dispatcher.forward(request, response);
} else {
// 登录失败
String path = "login/loginFail.jsp?username="+ username;
response.sendRedirect(path);
}
}

public void init() throws ServletException {

}

}

deleteuserservlet

import java.io.IOException;

import java.util.List;

import javax.servlet.ServletException;

import javax.servlet.http.HttpServlet;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;

import com.soft.bean.User;

import com.soft.dao.UserDAO;

public class DeleteUserServlet extends HttpServlet {

/**
* Constructor of the object.
*/
public DeleteUserServlet() {
super();
}

/**
* Destruction of the servlet. <br>
*/
public void destroy() {
super.destroy(); // Just puts "destroy" string in log
// Put your code here
}

/**
* The doGet method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to get.

* @param request the request send by the client to the server
* @param response the response send by the server to the client
<
4000
/span>* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {

this.doPost(request, response);
}

/**
* The doPost method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to post.

* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {

// 删除步骤:
// 1. 从JSP页面中获取到id值,代表一条记录
int id = Integer.parseInt(request.getParameter("id"));

// 2. 调用DAO,删除对应的记录
UserDAO userDAO = new UserDAO();
boolean res = userDAO.delteUser(id);
System.out.println("res: " + res);

// 3. 删除完成之后,跳转到 3.1 一个新的页面 3.2 显示所有记录的页面
// 如果是3.2,在删除之后,继续查询剩余的所有记录
List<User> list = userDAO.queryAllUser();
request.setAttribute("list", list);

// 页面跳转,请求转发;一定要测试跳转路径的写法!!!
String path = "/user/listUser.jsp";
request.getRequestDispatcher(path).forward(request, response);

}

/**
* Initialization of the servlet. <br>
*
* @throws ServletException if an error occurs
*/
public void init() throws ServletException {
// Put your code here
}

}

QuerUserServlet

import java.io.IOException;

import java.util.List;

import javax.servlet.ServletException;

import javax.servlet.http.HttpServlet;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;

import com.soft.bean.User;

import com.soft.dao.UserDAO;

public class QuerUserServlet extends HttpServlet {

/**
* Constructor of the object.
*/
public QuerUserServlet() {
super();
}

/**
* Destruction of the servlet. <br>
*/
public void destroy() {
super.destroy(); // Just puts "destroy" string in log
// Put your code here
}

/**
* The doGet method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to get.

* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {

this.doPost(request, response);
}

/**
* The doPost method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to post.

* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// 1. 获取到页面输入的查询条件
String username = request.getParameter("username");
int minAge = Integer.parseInt(request.getParameter("minAge"));
int maxAge = Integer.parseInt(request.getParameter("maxAge"));
// 2. 封装
User user = new User();
user.setUsername(username);
user.setMinAge(minAge);
user.setMaxAge(maxAge);
// 3. 调用DAO查询
UserDAO userDAO = new UserDAO();
List<User> list = userDAO.queryUser(user);
for (User user2 : list) {
System.out.println("user name: " + user2.getUsername());
}

// 4. 页面跳转
request.setAttribute("list", list);
String path = "user/listUser.jsp";
request.getRequestDispatcher(path).forward(request, response);

}

/**
* Initialization of the servlet. <br>
*
* @throws ServletException if an error occurs
*/
public void init() throws ServletException {
// Put your code here
}

}

QueryAllUserServlet

import java.io.IOException;

import java.util.List;

import javax.servlet.ServletException;

import javax.servlet.http.HttpServlet;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;

import com.soft.bean.User;

import com.soft.dao.UserDAO;

public class QueryAllUserServlet extends HttpServlet {

/**
* Constructor of the object.
*/
public QueryAllUserServlet() {
super();
}

/**
* Destruction of the servlet. <br>
*/
public void destroy() {
super.destroy(); // Just puts "destroy" string in log
// Put your code here
}

/**
* The doGet method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to get.

* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {

this.doPost(request, response);
}

/**
* The doPost method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to post.

* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// 1. 调用DAO对象,查询到所有数据表中的记录
UserDAO userDAO = new UserDAO();
List<User> list = userDAO.queryAllUser();

// 2. 将所有的记录信息向前面JSP页面传送
// 2.1 怎样把上面得到的list传递到JSP页面
request.setAttribute("list", list);

// 页面跳转:请求转发、请求重定向
// 2.2 请求转发
String path = "user/listUser.jsp";
request.getRequestDispatcher(path).forward(request, response);

}

/**
* Initialization of the servlet. <br>
*
* @throws ServletException if an error occurs
*/
public void init() throws ServletException {
// Put your code here
}

}

RegUserServlet

import java.io.IOException;

import javax.servlet.RequestDispatcher;

import javax.servlet.ServletException;

import javax.servlet.http.HttpServlet;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;

import com.soft.bean.User;

import com.soft.dao.UserDAO;

public class RegUserServlet extends HttpServlet {

public RegUserServlet() {
System.out.println("RegUserServlet RegUserServlet()!!!!!!!!!!!");
}

public void destroy() {
System.out.println("RegUserServlet destroy!!!!!!!!!!!");
}

public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {

this.doPost(request, response);
}

public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// 1. 从JSP页面获取到用户输入的数据
String username = request.getParameter("username");
System.out.println("username: " + username);

String pwd = request.getParameter("password");
int age
c23f
= Integer.valueOf(request.getParameter("age"));

// 获取到用户的注册信息后,应该:将用户输入的信息添加到数据库中!
// 获取到的信息怎么传给DAO对象?

// 2. 将获取到的以上数据进行封装,封装到对应的JavaBean对象中
User user = new User();
user.setUsername(username);
user.setPassword(pwd);
user.setAge(age);

UserDAO userDAO = new UserDAO();
boolean res = userDAO.addUser(user);

// 请求重定向

// String url = "user/regSucc.jsp";

// response.sendRedirect(url);

// 请求转发
String url = "";
if (res) {
url = "user/regSucc.jsp";
} else {
url = "user/regFail.jsp";
}

RequestDispatcher dispatcher = request.getRequestDispatcher(url);
dispatcher.forward(request, response);

}

public void init() throws ServletException {
System.out.println("RegUserServlet init!!!!!!!!!!!");
}

}

ToUpdateUserServlet

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.soft.bean.User;

import com.soft.dao.UserDAO;

public class ToUpdateUserServlet extends HttpServlet {

/**
* Constructor of the object.
*/
public ToUpdateUserServlet() {
super();
}

/**
* Destruction of the servlet. <br>
*/
public void destroy() {
super.destroy(); // Just puts "destroy" string in log
// Put your code here
}

/**
* The doGet method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to get.

* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {

this.doPost(request, response);
}

/**
* The doPost method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to post.

* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// 跳转到修改页面
// 1. 获取到list页面中的id值
int id = Integer.parseInt(request.getParameter("id"));

// 2. 查询该id值对应的数据库表记录
UserDAO userDAO = new UserDAO();
User user = userDAO.queryUserById(id);

// 3. 将该条记录显示到updateUser.jsp页面上
if (user != null) {
request.setAttribute("usr", user);
String path = "/user/updateUser.jsp";
request.getRequestDispatcher(path).forward(request, response);
}

}

/**
* Initialization of the servlet. <br>
*
* @throws ServletException if an error occurs
*/
public void init() throws ServletException {
// Put your code here
}

}

UpdateUserServlet

import java.io.IOException;

import java.io.PrintWriter;

import javax.servlet.ServletException;

import javax.servlet.http.HttpServlet;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;

import com.soft.bean.User;

import com.soft.dao.UserDAO;

public class UpdateUserServlet extends HttpServlet {

/**
* Constructor of the object.
*/
public UpdateUserServlet() {
super();
}

/**
* Destruction of the servlet. <br>
*/
public void destroy() {
super.destroy(); // Just puts "destroy" string in log
// Put your code here
}

/**
* The doGet method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to get.

* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {

this.doPost(request, response);
}

/**
* The doPost method of the servlet. <br>
*
* This method is called when a form has its tag value method equals to post.

* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {

// 更新
// 1. 获取到页面中的最新的值
int id = Integer.parseInt(request.getParameter("id"));
String username = request.getParameter("username");
int age = Integer.parseInt(request.getParameter("age"));

// 2. 调用DAO,进行更新
// 2.1 对DAO传值时,所有字段封装到JavaBean, User
User user = new User();
user.setId(id);
user.setUsername(username);
user.setAge(age);

UserDAO userDAO = new UserDAO();

}

/**
* Initialization of the servlet. <br>
*
* @throws ServletException if an error occurs
*/
public void init() throws ServletException {
// Put your code here
}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: