标签:
首先仍然是javaScript的处理
function login(){ var th=document.form1; if(th.username.value==""){ alert("用户名不能为空!"); th.username.focus(); return; } if(th.pswd.value==""){ alert("密码不能为空!"); th.pswd.focus(); return; } th.action="<%=path%>/servlet/LoginAction"; th.submit(); }
新建一个接口 来处理用户登陆
package com.servlet.login.service; import java.util.List; import java.util.Map; public interface LoginService { public boolean userLogin(List<Object> params); }
新建一个类 继承接口 完成对数据的访问
package com.servlet.login.dao; import java.sql.SQLException; import java.util.List; import java.util.Map; import com.jdbc.util.MySqlConnect; import com.servlet.login.service.LoginService; public class LoginDao implements LoginService { private MySqlConnect conn = null; public LoginDao() { conn = new MySqlConnect(); } public boolean userLogin(List<Object> params) { //连接数据库 conn.getConnect(); boolean flag = false; String sql = "select * from userinfo where username= ? and password=? "; try { Map<String, Object> map = conn.queryBySimpleResult(sql, params); flag = !map.isEmpty() ? true : false; } catch (SQLException e) { e.printStackTrace(); }finally{ conn.releaseConnect(); } return flag; } }
再新建一个类 完成对业务逻辑的处理
package com.servlet.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.servlet.login.dao.LoginDao; import com.servlet.login.service.LoginService; public class LoginAction extends HttpServlet { private static final long serialVersionUID = 1L; private LoginService service; public LoginAction() { service=new LoginDao(); } public void destroy() { super.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 path=request.getContextPath(); response.setContentType("text/html;charset=utf-8"); String username=request.getParameter("username"); String password=request.getParameter("pswd"); List<Object>params=new ArrayList<Object>(); params.add(username); params.add(password); boolean flag=service.userLogin(params); if(flag){ request.getSession().setAttribute("username",username); response.sendRedirect(path+"/main.jsp"); } } public void init() throws ServletException { } }
为了防止直接从登陆页面跳转页面直接登陆 还需要添加一个过滤器
package com.servlet.login.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; public class MyFilter implements Filter { public MyFilter() { } public void destroy() { } public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { HttpServletRequest httpServletRequest = (HttpServletRequest) request; HttpServletResponse httpServletResponse = (HttpServletResponse) response; String path = httpServletRequest.getContextPath(); httpServletRequest.setCharacterEncoding("utf-8"); httpServletResponse.setCharacterEncoding("utf-8"); 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 { } }
<filter> <filter-name>MyFilter</filter-name> <filter-class>com.servlet.login.filter.MyFilter</filter-class> </filter> <filter-mapping> <filter-name>MyFilter</filter-name> <url-pattern>/main.jsp</url-pattern> </filter-mapping>
标签:
原文地址:http://www.cnblogs.com/mydomainlistentome/p/4803338.html