普通管理员也可以添加用户,按照问答里相似的问题修改之后还是不行
package com.imooc.servlet; import com.imooc.db.CourseDaoImpl; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; import java.io.PrintWriter; import java.util.List; import java.util.Map; /** * 验证码的Servlet */ public class LoginServlet extends HttpServlet { @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { this.doPost(req, resp); } @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { //验证码部分 //页面用户输入的验证码 String inputCode=req.getParameter("code"); //服务器端生成的验证码 String code=(String)req.getSession().getAttribute("Code"); if(!code.equalsIgnoreCase(inputCode)){ resp.setContentType("text/html;charset=gb2312"); PrintWriter out = resp.getWriter(); out.print("<script language='javascript'>alert('验证码错误');window.location.href='index.jsp';</script>"); return; } //用户名与密码 String username=req.getParameter("username"); String password=req.getParameter("password"); if((username!=null&&!username.equals(""))&&(password!=null&&!password.equals(""))){ if(username.equals("imooc")&&password.equals("imooc")){ //将用户名存到session中 req.getSession().setAttribute("username",username); req.getRequestDispatcher("server.jsp?flag=1").forward(req,resp); return; } } //获取用户表 CourseDaoImpl courseDaoImpl=new CourseDaoImpl(); List<Map<String,Object>> userList=courseDaoImpl.getUsersTable(); for(Map<String,Object> map:userList){ //判断用户名是否正确 if(username.equals(map.get("username")));{ if(password.equals(map.get("password"))){ resp.sendRedirect("server.jsp"); }else{ //用户名或密码错误 req.setAttribute("msg","用户名或密码错误"); req.getRequestDispatcher("index.jsp").forward(req,resp); return; } } } } }
<%@ page contentType="text/html;charset=UTF-8" language="java" %> <% String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + "/" + request.getServletContext().getContextPath(); %> <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%> <!DOCTYPE html> <html> <head> <base href="<%=basePath%>"> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>课程后台管理系统</title> </head> <frameset rows="20%,*"> <frame src="./top.jsp"></frame> <frameset cols="15%,*"> <frame src="./left.jsp"></frame> <frame name="main"></frame> </frameset> </frameset> </html>
<%@ page contentType="text/html;charset=UTF-8" language="java" %> <% String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + "/" + request.getServletContext().getContextPath(); %> <!DOCTYPE html> <html> <head> <base href="<%=basePath%>"> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>登录页面</title> <style type="text/css"> .c1{ width:300px; cursor:pointer; } .c2{ width:300px; padding-left: 30px; } </style> </head> <body> <c:choose> <c:when test="${flag==1 }"> <div class="c1"> <h3 onclick="show1()">用户管理</h3> <div class="c2" id="menu1" style="display:none;"> <p><a href="<%=basePath%>/addUser.jsp" target="main">添加管理员</a></p> <p><a href="<%=basePath%>/SelectUserServlet" target="main">查询管理员</a></p> </div> </div> </c:when> <c:otherwise> <h3>用户管理</h3> </c:otherwise> </c:choose> <div class="c1"> <h3 onclick="show2()">课程管理</h3> <div class="c2" id="menu2" style="display:none;"> <p><a href="<%=basePath%>/addCourse.jsp" target="main">课程添加</a></p> <p><a href="<%=basePath%>/courseImport.jsp" target="main">课程批量导入(Excel)</a></p> <p><a href="<%=basePath%>/ExportCourseServlet" target="main">课程导出</a></p> <p><a href="<%=basePath%>/GetCourseServlet" target="main">课程查询</a></p> </div> </div> <script type="text/javascript"> function show1(){ var menu = document.getElementById("menu1"); var displayStyle = menu.style.display; if(displayStyle=="none"){ menu.style.display="block"; }else{ menu.style.display="none"; } } function show2(){ var menu = document.getElementById("menu2"); var displayStyle = menu.style.display; if(displayStyle=="none"){ menu.style.display="block"; }else{ menu.style.display="none"; } } </script> </body> </html>
0
收起
正在回答
2回答
你好同学,修改之后就证明EL表达式生效了,你可以将LoginServlet中的值存储到session域中试试,祝学习愉快~
从网页搭建入门Java Web2018版
- 参与学习 人
- 提交作业 1088 份
- 解答问题 10205 个
如果你有Java语言基础,又想以后从事Java Web开发,那么本路径是你的不二选择!本路径从网页搭建开始入手,通过大量案例来学习Java Web基础。定能助你完成Java Web小白的蜕变!
了解课程
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星