login.jsp部分:
<div class="box box-info">
<div class="box-header with-border">
<h3 class="box-title">登录</h3>
</div>
<form class="form-horizontal" action="login" method="post">
<div class="box-body">
<%
Cookie[] cs = request.getCookies();
Cookie findC = null;
if (cs != null) {
for (Cookie c : cs) {
if ("remname".equals(c.getName())) {
findC = c;
}
}
}
String username = "";
if (findC != null) {
username = URLDecoder.decode(findC.getValue(), "UTF-8");
}
String value = request.getParameter("username");
if (value != null) {
username = value;
}
%>
<div
class='${empty message ? "form-group" : "form-group has-error" }'>
<label for="username" class="col-sm-2 control-label">用户名:</label>
<div class="col-sm-10">
<input type="text" class="form-control" id="username"
placeholder="请输入用户名" name="username" value="<%=username%>">
</div>
<span class="help-block">${message }</span>
</div>
<div class="form-group">
<label for="password" class="col-sm-2 control-label">密码:</label>
<div class="col-sm-10">
<input type="password" class="form-control" id="password"
placeholder="请输入密码" name="password">
</div>
</div>
<div class="form-group">
<div class="col-sm-offset-2 col-sm-10">
<div class="checkbox">
<label> <input type="checkbox" name="remname"
value="true" <%=findC == null ? "" : "checked='checked'"%>>记住用户名
</label>
</div>
</div>
</div>
</div>
<div class="box-footer">
<button type="reset" class="btn btn-default">取消</button>
<button type="submit" class="btn btn-info pull-right">登录</button>
</div>
</form>
</div>
LoginUserServlet部分:
package cn.tedu.web;
import java.io.IOException;
import java.net.URLEncoder;
import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import cn.tedu.dao.UserDao;
public class LoginUserServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
request.setCharacterEncoding("UTF-8");
String username = request.getParameter("username");
String password = request.getParameter("password");
String remname = request.getParameter("remname");
if(username == null || "".equals(username)) {
request.setAttribute("message", "用户名不能为空!");
request.getRequestDispatcher("/login.jsp").forward(request, response);
return;
}
UserDao dao = new UserDao();
boolean f = dao.getUserByUsernameAndPassword(username, password);
if(f) {
HttpSession session = request.getSession();
session.setAttribute("user", username);
if("true".equals(remname)) {
Cookie cookie = new Cookie("remname", URLEncoder.encode(username, "UTF-8"));
cookie.setMaxAge(60*60*24);
cookie.setPath(request.getContextPath());
response.addCookie(cookie);
}else {
Cookie cookie = new Cookie("remname", "");
cookie.setMaxAge(0);
cookie.setPath(request.getContextPath());
response.addCookie(cookie);
}
response.sendRedirect("list");
return;
}else{
request.setAttribute("message", "登录失败!用户名或密码错误!");
request.getRequestDispatcher("/login.jsp").forward(request, response);
return;
}
}
}