业务逻辑:
1、登陆login.jsp
2、判断登陆是否成功check.jsp
3、登陆成功页面newsDetail.jsp
4、登陆失败转发到login.jsp
代码如下:
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>用户登陆页</title> </head> <body> <form action="check.jsp" method="post"> <table> <tr> <td>用户名:</td> <td><input type="text" name="username"/></td> </tr> <tr> <td>密码:</td> <td><input type="text" name="pwd"/></td> </tr> <tr> <td>邮箱:</td> <td><input type="text" name="email" /></td> </tr> <tr> <td align="right"><input type="checkbox" name="title" value="10001" /></td> <td>第一个选项</td> </tr> <tr> <td align="right"><input type="checkbox" name="title" value="10002" /></td> <td>第二个选项</td> </tr> <tr> <td align="right"><input type="checkbox" name="title" value="10004" /></td> <td>第三个选项</td> </tr> <tr> <td align="right"><input type="submit" value="提交" /></td> </tr> </table> </form> <% Object obj = request.getAttribute("error"); if(obj!=null){ out.println("info=>>>>>>"+obj.toString()); } %> </body> </html>
check.jsp
<%@ page language="java" import="java.util.*" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <base href="<%=basePath%>"> <title>My JSP 'index.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> <% //获取表单提写数据 request.setCharacterEncoding("utf-8"); response.setCharacterEncoding("utf-8"); String username = request.getParameter("username"); String password = request.getParameter("pwd"); String email = request.getParameter("email"); /* 获取checkbox的值*/ String[] titles = request.getParameterValues("title"); String alltitle=""; if(titles!=null && titles.length!=0){ for(int i=0;i<titles.length;i++){ alltitle += titles[i]+" "; } } //判断登陆是否成功 if(!username.equals("admin")){ request.setAttribute("error", "登陆失败!"); request.getRequestDispatcher("login.jsp").forward(request, response); }else{ //登陆成功,创建session会话,保存登陆状态 session.setAttribute("username", username); session.setAttribute("password", password); //session设置过期时间 session.setMaxInactiveInterval(5); response.sendRedirect("newsDetail.jsp"); } %> </body> </html>
newsDetail.jsp
<!--页面的头部--> <div id="header"> <!--页面顶部--> <div class="main-top"> <div class="logo"><a href=""><span>新闻大视野</span></a></div> <div class="login-box"> <% Object obj=session.getAttribute("username"); Object pwd=session.getAttribute("password"); String username=""; String password=""; if(obj!=null &&pwd!=null){ username = obj.toString(); password = pwd.toString();%> <label><%="欢迎你,"+username %></label> <a href="logout.jsp">注销</a> <% }else{%> <label>用户名</label><input type="text" name="username"/><label>密码</label><input type="text" name="password"/><button>登录</button> <%}%> </div>