request.setCharacterEncoding("utf-8"); response.setCharacterEncoding("utf-8"); User user = null; HttpSession session = request.getSession(); String errorMsg = null; String userName = request.getParameter("username"); String password = request.getParameter("password"); if(userName == null || password == null || (userName.trim().equals("")) ||(password.trim().equals(""))){ errorMsg = "用户名或密码不能为空"; request.setAttribute("errorMsg", errorMsg); request.getRequestDispatcher("login2.jsp").forward(request, response); }else if(userName.equals("admin") && password.equals("123456")){ user = new User(); user.setUsername(userName); user.setPassword(password); session.setAttribute("user", user); request.getRequestDispatcher("main.jsp").forward(request, response); }else{ errorMsg ="用户名或密码错误"; request.setAttribute("errorMsg", errorMsg); request.getRequestDispatcher("login2.jsp").forward(request, response); } }
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; request.setAttribute("ctx", path); %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <base href="<%=basePath%>"> <title>My JSP 'login2.jsp' starting page</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> </head> <body> <div style="color:red">${errorMsg}</div> <form action="${ctx}/LoginServlet" method="post"> <table> <tr> <td>用户名:</td> <td><input type="text" name="username" value="${param.username}"></td> </tr> <tr> <td>密码:</td> <td><input type="password" name="password" value="${param.password}"></td> </tr> <tr> <td colspan="2"><input type="submit" value="登录"> </td> </tr> </table> </form> </body> </html>
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <%@ page isELIgnored="false"%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <base href="<%=basePath%>"> <title>My JSP 'main.jsp' starting page</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> </head> <body> <c:if test="${empty sessionScope.user}" var="isLogin" scope="page"> <%request.getRequestDispatcher("login2.jsp").forward(request, response); %> </c:if> <c:if test="${isLogin}" var="isLogin" scope="page"> 系统主页 </c:if> <c:set var="salary" scope="session" value="${2000*2 }"></c:set> <c:if test="${salary >2000 }"> <p>sfdsfsdf</p> </c:if> </body> </html>