现在所有的项目几乎都离不开登录,所以就用MyEclipse 做了一个简单的验证登录
首先创建一个项目
创建包:
Common:放公共的类
Dao:数据库操作接口
Filter:过滤器
Po:数据库的实体类
Service:放服务层的接口
Servlet:放servlet,即类
Util:放工具类
写一个连接数据库的驱动
创建一个类,命名为LoginServlet,部分代码:
public class LoginServlet extends HttpServlet{
/**
*
*/
private static final long serialVersionUID = 8736434482787994811L;
Logger logger=Logger.getRootLogger();
//加载日志配置
public void init(ServletConfig config)
throws ServletException
{
String path=config.getServletContext().getRealPath("WEB-INF\\classes");
path=path+File.separator+config.getServletContext().getInitParameter("log4j");
PropertyConfigurator.configure(path);
}
public void doGet(HttpServletRequest request,HttpServletResponse response)
throws ServletException,IOException{
logger.info("doGet方法");
doPost(request, response);
}
public void doPost(HttpServletRequest request,HttpServletResponse response)
throws ServletException,IOException{
String fun=request.getParameter("fun");
logger.warn("doPost方法的值是:"+fun);
if("toLogin".equals(fun)){
toLogin(request, response);
}
else if("login".equals(fun)){
login(request, response);
}
else{
response.sendRedirect("../index.jsp");
}
}
public void toLogin(HttpServletRequest request,HttpServletResponse response)
throws ServletException,IOException{
//获取单例
String token=TokenProccessor.getInstance().makeToken();
request.getSession().setAttribute("token", token);
//把请求转发过去
request.getRequestDispatcher("/jsp/login.jsp").forward(request, response);
}
public void login(HttpServletRequest request,HttpServletResponse response)
throws ServletException,IOException{
String name=request.getParameter("name");
String password=request.getParameter("password");
//验证码
String codekey=request.getParameter("codekey");
String token=request.getParameter("token");
//session
String sessionCodekey=(String) request.getSession().getAttribute("randomString");
String sessiontoken=(String) request.getSession().getAttribute("token");
//验证是否重复提交
if(token!=null && token.equals(sessiontoken)){
//放到session中的token给移掉
request.getSession().removeAttribute("token");
//忽略大小写,判断是否等于sessionCodekey
if(codekey !=null && codekey.equalsIgnoreCase(sessionCodekey)){
IUserService userService=new UserServiceImpl();
UserPo dbUser=userService.login(name);
if (dbUser!=null) {
if (dbUser.getPassword().equals(password)) {
request.getSession().setAttribute("gUser", dbUser);
//response.sendRedirect("UserServlet?fun=list");
//重定向
response.sendRedirect("UserServlet?fun=list");
} else {
request.setAttribute("error", "密码错误");
request.getRequestDispatcher("/jsp/error.jsp").forward(request, response);
}
} else {
request.setAttribute("error", "没有此用户");
request.getRequestDispatcher("/jsp/error.jsp").forward(request,response);
}
}else{
System.out.println("验证码错误");
response.sendRedirect("../index.jsp");
}
}else{
System.out.println("重复提交");
response.sendRedirect("../index.jsp");
}
}
XML配置Servlet
XML对整个登录来说是很重要的,如果配置错了,就可能页面都打不开
登录除了控制台,肯定是少不了页面的,写jsp页面
<body>
<form action="${ctx}/servlet/LoginServlet" method="post">
<input type="hidden" name="fun" value="login">
<input type="hidden" name="token" value="${token}">
<table>
<tr>
<td>用户名:</td>
<td><input type="text" name="name" /></td>
</tr>
<tr>
<td>密码:</td>
<td><input type="password" name="password" /></td>
</tr>
<tr>
<td>验证码:</td>
<td><input type="text" name="codekey" /></td>
</tr>
<tr>
<td>
<jsp:useBean id="now" class="java.util.Date" scope="page" />
<img id="codekey" alt="" src="${ctx}/servlet/IdentityServlet?t=${now}" onclick="refcodekey()">
</td>
</tr>
<tr>
<td><input type="submit" name="登陆" /></td>
</tr>
</table>
</form>
<script type="text/javascript">
function refcodekey(){
document.getElementById("codekey").src="${ctx}/sercvlet/IdentityServlet?t="+new Date();
}
</script>
</body>
这里${ctx}是拼接路径,可以少写相对路径
<c:set value="${pageContext.request.contextPath}" scope="page" var="ctx"></c:set>
验证登录要有验证码
用的是别人写已经好的验证码类,直接引用就好了,部分代码:
然后写个错误页面erro.jsp,如果登录失败的话就跳转到那里
要注意验证的重复提交,避免用户二次刷新重复提交表单
最后要写数据库的实体类、数据库操作接口以及服务层的接口
完成后效果如图: