助教老师们,我的代码有很多问题,我已经做了好久了,希望你们帮我认真地找出错误
package com.imooc.proj; import java.util.*; public class DBUtil { public static Map<Integer, User> map = new HashMap<>(); static { map.put(001, new User("001","123456")); map.put(002, new User("002","123456")); map.put(003, new User("003","123456")); map.put(004, new User("004","123456")); } public static Map<Integer, ListInfo> data = new HashMap<>(); static { data.put(101, new ListInfo("101","开学", "请同学们于9月1日前来报道")); data.put(102, new ListInfo("102","选课", "开始选课啦")); data.put(103, new ListInfo("103","竞选班委", "将于近期竞选班干部~")); data.put(104, new ListInfo("104","评选奖学金", "评选奖学金啦~")); } public static boolean isRightUser(User user) { boolean flag = false; for(int key : map.keySet()) { User u = map.get(key); if(user.getUsername().equals(u.getUsername()) && user.getPassword().equals(u.getPassword())) flag = true; break; } return flag; } }
package com.imooc.proj; public class ListInfo { private String id; private String name; private String content; public ListInfo(String id, String name, String content) { this.id = id; this.name = name; this.content = content; } public String getId() { return id; } public void setId(String id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getContent() { return content; } public void setContent(String content) { this.content = content; } }
package com.imooc.proj; public class User { private String username; private String password; public User(String username, String password) { super(); this.username = username; this.password = password; } public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } }
//login.jsp <%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%> <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>Insert title here</title> </head> <body> <h3>登陆页面</h3> <hr/> <form action="control.jsp"> <table align = center> <tr> <td> 用户名: </td> <td> <input type="text" name="account"> </td> </tr> <tr> <td> 密码: </td> <td> <input type="password" name="password"> </td> </tr> <tr> <td> <input type="submit" value="登录"> </td> </tr> </table> </form> </body> </html>
//control.jsp <%@ page contentType="text/html;charset=UTF-8" language="java" import="com.imooc.proj.*,java.util.*" errorPage="error.jsp" %> <html> <head> <title>Title</title> </head> <body> <% String account = request.getParameter("account"); String password = request.getParameter("password"); Map<Integer,ListInfo> map = DBUtil.data; User user = new User(account, password); boolean flag = DBUtil.isRightUser(user); if(flag){ %> <h3>公告列表为:</h3> <hr/> <form action="select.jsp"> <table align="center" border="1" style="margin-top: 100px"> <tr> <td> 公告编号: </td> <td> <input type="text" value="null" name="id"> </td> <td> <input type="submit" value="Select"> </td> </tr> </table> </form> <table align="center" border="1" style="margin-top: 100px"> <tr> <td> 编号 </td> <td> 名称 </td> <td> 内容 </td> <td> 删除 </td> <td> 修改 </td> </tr> <% for(Integer key : map.keySet()){ ListInfo l = map.get(key); %> <tr> <td> <%=l.getId()%> </td> <td> <%=l.getName()%> </td> <td> <%=l.getContent()%> </td> <td> <a href="delete.jsp?id=<%= l.getId()%>">删除</a> </td> <td> <a href="update.jsp?id=<%= l.getId()%>&name=<%= l.getName()%>&content=<%=l.getContent()%>">修改</a> </td> </tr> <% } %> </table> <% }else{ throw new Exception("账号和密码错误"); } %> </body> </html>
//select.jsp <%@ page contentType="text/html;charset=UTF-8" language="java" import="java.util.*, com.imooc.proj.*" %> <html> <head> <title>Title</title> </head> <body> <% Map<Integer,ListInfo> map = DBUtil.data; ListInfo l = map.get(request.getParameter("id")); %> <form action="control.jsp"> <table align="center" border="1" style="margin-top: 100px"> <tr> <td> 公告编号: </td> <td> <input type="text" value="<%=request.getParameter("id")%>"> </td> <td> <input type="submit" value="Back"> </td> </tr> </table> </form> <table align="center" border="1" style="margin-top: 100px"> <tr> <td> 编号 </td> <td> 名称 </td> <td> 内容 </td> <td> 删除 </td> <td> 修改 </td> </tr> <tr> <td> <%=l.getId()%> </td> <td> <%=l.getName()%> </td> <td> <%=l.getContent()%> </td> <td> <a href="delete.jsp?id=<%= l.getId()%>">Delete</a> </td> <td> <a href="update.jsp?id=<%= l.getId()%>&name=<%= l.getName()%>&content=<%=l.getContent()%>">Update</a> </td> </tr> </table> </body> </html>
//update.jsp <%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>Title</title> </head> <body> <h3>修改公告信息</h3> <form action="update_control.jsp"> <table style="margin-top: 100px"> <tr> <td> 编号:<input type="text" name="id" value="<%=request.getParameter("id")%>"> </td> </tr> <tr> <td> 标题:<input type="text" name="name" value="<%=request.getParameter("name")%>"> </td> </tr> <tr> <td> 内容:<input type="text" name="content" value="<%=request.getParameter("content")%>"> </td> </tr> <tr> <td> <input type="submit" value="修改"> </td> </tr> </table> </form> </body> </html>
//update_control.jsp <%@ page contentType="text/html;charset=UTF-8" language="java" import="java.util.*, com.imooc.proj.*" %> <html> <head> <title>Title</title> </head> <body> <% Map<Integer,ListInfo> map = DBUtil.data; ListInfo l = map.get(request.getParameter("id")); l.setContent(request.getParameter("content")); l.setId(request.getParameter("id")); l.setName(request.getParameter("name")); %> 修改成功 </body> </html>
//error.jsp <%@ page contentType="text/html;charset=UTF-8" language="java" isErrorPage="true" %> <html> <head> <title>Title</title> </head> <body> <%=exception.getMessage()%> </body> </html>
//delete.jsp <%@ page contentType="text/html;charset=UTF-8" language="java" import="com.imooc.proj.*, java.util.*" %> <html> <head> <title>Title</title> </head> <body> <h3>删除公告编号为:<%=request.getParameter("id")%></h3> <% Map<Integer,ListInfo> map = DBUtil.data; map.remove(request.getParameter("id")); %> </body> </html>
0
收起
正在回答
2回答
1、
可在control.jsp页面中把用户名和密码存到域中。
request.setAttribute("account", account);
request.setAttribute("password", password);
2、
然后再back提交的表单中,把之前存到域中的参数取出来,并隐藏提交
例如在back的表单中添加一下代码
<input type="hidden" name="account" value=<%= request.getParameter("account")%> />
<input type="hidden" name="password" value<%= request.getParameter("password") %> />
如果我的回答解决了你的疑惑,请采纳!祝学习愉快!
Java Web基础入门2018版
- 参与学习 716 人
- 提交作业 185 份
- 解答问题 1363 个
会Java?懂前端基础?想学后台开发?那么,赶快来学习《Java Web入门》路径吧。本路径主要介绍Java Web的基础知识,并配有大量案例,定会让你收获多多!
了解课程
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星