控制层:
package com.product.dbutil.login.action; import java.io.IOException; import java.io.PrintWriter; import java.util.ArrayList; 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.product.dbutil.login.dao.LoginDao; import com.product.dbutil.login.service.LoginService; /** * MVC模式 一个控制层action 一个数据访问层dao,一个接口service * 接口定义一个登录功能规范, dao层实现对数据库的访问 * action层获得客户端提交的表单信息,调用dao层访问数据库,若有返回真在做界面跳转。 * @author yqq * */ public class LoginAction extends HttpServlet { private LoginService service;//接口 /** * Constructor of the object. */ public LoginAction() { 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 { String path = request.getContextPath(); //设置编码方式和客户端表单的编码方式相同 response.setContentType("text/html;charset=utf-8"); PrintWriter out = response.getWriter(); //获取表单的值 String username = request.getParameter("username"); String pswd = request.getParameter("pswd"); List<Object> params = new ArrayList<Object>(); params.add(username); params.add(pswd); //查询数据库的内容若有则返回true boolean flag = service.login(params); //////////////// if (flag) { //登录界面(获得Session并设置属性在登录成功后才可以) request.getSession().setAttribute("username", username); response.sendRedirect(path + "/main.jsp"); } out.flush(); out.close(); } /** * Initialization of the servlet. <br> * * @throws ServletException * if an error occurs */ public void init() throws ServletException { // Put your code here service = new LoginDao(); } }
服务层:
接口定义中,方法默认是公有抽象的,不指明也可以。
package com.product.dbutil.login.service; import java.util.List; public interface LoginService { public boolean login(List<Object> params); }
dao层:
package com.product.dbutil.login.dao; import java.util.List; import java.util.Map; import com.product.dbutil.jdbc.JdbcUtils; import com.product.dbutil.login.service.LoginService; public class LoginDao implements LoginService { private JdbcUtils jdbcUtils = null; public LoginDao() { // TODO Auto-generated constructor stub jdbcUtils = new JdbcUtils(); } public boolean login(List<Object> params) { // TODO Auto-generated method stub boolean flag = false; String sql = "select * from userinfo where username=? and pswd=?"; try { jdbcUtils.getConnection(); Map<String, Object> map = jdbcUtils.findSimpleResult(sql, params); flag = !map.isEmpty() ? true : false; } catch (Exception e) { // TODO: handle exception e.printStackTrace(); } finally { jdbcUtils.releaseConn(); } return flag; } }
过滤器:
package com.product.dbutil.filter; 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; /** * web.xml配置中url-pattern配置为/*全过滤,配置为谁过滤谁 * @author yqq_coder * */ public class MyFilter implements Filter { public MyFilter() { // TODO Auto-generated constructor stub } public void destroy() { // TODO Auto-generated method stub } public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { // TODO Auto-generated method stub // 过滤用户的请求,判断是否登录 //先做类型转换 HttpServletRequest httpServletRequest = (HttpServletRequest) request; HttpServletResponse httpServletResponse = (HttpServletResponse) response; //获得从定向路径 String path = httpServletRequest.getContextPath(); //设置请求和响应的编码 httpServletRequest.setCharacterEncoding("utf-8"); httpServletResponse.setCharacterEncoding("utf-8"); //从请求中获得Session值,为空跳转 String username = (String)httpServletRequest.getSession().getAttribute("username"); if (username==null) { httpServletResponse.sendRedirect(path + "/index.jsp"); } /////// chain.doFilter(httpServletRequest, httpServletResponse); } public void init(FilterConfig arg0) throws ServletException { // TODO Auto-generated method stub } }
web.xml配置
<?xml version="1.0" encoding="UTF-8"?> <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> <servlet> <description>This is the description of my J2EE component</description> <display-name>This is the display name of my J2EE component</display-name> <servlet-name>RegisterAction</servlet-name> <servlet-class>com.product.dbutil.register.action.RegisterAction</servlet-class> </servlet> <servlet> <description>This is the description of my J2EE component</description> <display-name>This is the display name of my J2EE component</display-name> <servlet-name>LoginAction</servlet-name> <servlet-class>com.product.dbutil.login.action.LoginAction</servlet-class> </servlet> <servlet> <description>This is the description of my J2EE component</description> <display-name>This is the display name of my J2EE component</display-name> <servlet-name>ProductAction</servlet-name> <servlet-class>com.product.dbutil.product.action.ProductAction</servlet-class> </servlet> <servlet-mapping> <servlet-name>RegisterAction</servlet-name> <url-pattern>/servlet/RegisterAction</url-pattern> </servlet-mapping> <servlet-mapping> <servlet-name>LoginAction</servlet-name> <url-pattern>/servlet/LoginAction</url-pattern> </servlet-mapping> <servlet-mapping> <servlet-name>ProductAction</servlet-name> <url-pattern>/servlet/ProductAction</url-pattern> </servlet-mapping> <filter> <filter-name>MyFilter</filter-name> <filter-class>com.product.dbutil.filter.MyFilter</filter-class> </filter> <filter-mapping> <filter-name>MyFilter</filter-name> <url-pattern>/main.jsp</url-pattern> </filter-mapping> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> </web-app>
时间: 2024-10-26 07:34:42