4-8编程删除和查找的问题
删除功能和查找功能一点击就会出现500,感觉是判断key值的if语句问题,但是不知道具体为什么,请老师看一下。
DBUtil.java
package com.imooc.db; import java.util.HashMap; import java.util.Map; import com.imooc.bean.Emp; import com.imooc.bean.User; public class DBUtil { public static Map<String,Emp> map=new HashMap<String,Emp>(); public static Map<String,User> users=new HashMap<String,User>(); static { users.put("admin", new User("admin","123","管理员")); map.put("101",new Emp("101","开学","请同学们于9月1日前来报道!")); map.put("102",new Emp("102","选课","开始选课啦~")); map.put("103",new Emp("103","竞选班委","将于近期竞选班干部~")); map.put("104",new Emp("104","评选奖学金","评选奖学金啦~")); } public static boolean in(User user) { boolean flag =false; for(String key:users.keySet()) { User temp = users.get(key); if (user.getAccount().equals(temp.getAccount()) && user.getPassword().equals(temp.getPassword())) { flag = true; break; } } return flag; } public static boolean search(String num) { boolean flag=false; for(String key:map.keySet()) { Emp temp=map.get(key); if(num.equals(temp.getNum())){ flag=true; }else { flag=false; } } return flag; } }
User.java
package com.imooc.bean; public class User { private String account; private String password; private String name; public User() { super(); } public User(String account, String password, String name) { super(); this.account = account; this.password = password; this.name = name; } public String getAccount() { return account; } public void setAccount(String account) { this.account = account; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public String getName() { return name; } public void setName(String name) { this.name = name; } @Override public String toString() { return "User [account=" + account + ", password=" + password + ", name=" + name + "]"; } }
Emp.java
package com.imooc.bean; public class Emp { private String num; private String title; private String text; public Emp() { super(); } public Emp(String num, String title, String text) { super(); this.num = num; this.title = title; this.text = text; } public String getNum() { return num; } public void setNum(String num) { this.num = num; } public String getTitle() { return title; } public void setTitle(String title) { this.title = title; } public String getText() { return text; } public void setText(String text) { this.text = text; } @Override public String toString() { return "Emp [num=" + num + ", title=" + title + ", text=" + text + "]"; } }
logon.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>登录系统</title> </head> <body> <h3 align=center>登录页面</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 language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" import="com.imooc.db.*,com.imooc.bean.*,java.util.*"%> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Insert title here</title> </head> <body> <% String account=request.getParameter("account"); String password=request.getParameter("password"); User user=new User(account,password,null); boolean flag=DBUtil.in(user); if(flag){ pageContext.forward("announcement.jsp"); }else{ %> <div align="center"> <h3>登录失败</h3> <input type="button" value="back" onclick="location.href='logon.jsp'"/> </div> <% } %> </body> </html>
announcements.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" import="com.imooc.db.*,com.imooc.bean.*,java.util.*"%> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>公告</title> </head> <body> <h3>公告列表为</h3> <hr> <form action="search.jsp"> <table align="center" border="1"> <tr> <td>公告编号:</td> <td><input type="text" placeholder="请输入要查询的编号" name="num"></td> <td><input type="submit" value="查 询"></td> <tr> </table> </form> <hr> <table align="center" border="1"> <tr> <td>编号</td> <td>名称</td> <td>内容</td> <td>删除</td> <td>修改</td> <tr> <% for (Emp e : DBUtil.map.values()) { %> <tr> <td><%=e.getNum()%></td> <td><%=e.getTitle()%></td> <td><%=e.getText()%></td> <td><a href="delete.jsp?num=<%=e.getNum()%>">删除</a></td> <td><a href="update.jsp?num=<%=e.getNum() %>">修改</a></td> </tr> <% } %> </table> </body> </html>
search.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" import="com.imooc.db.*,com.imooc.bean.*,java.util.*"%> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>查找</title> </head> <body> <% String num=request.getParameter("num"); boolean flag=DBUtil.search(num); if(flag){ Emp e=DBUtil.map.get(num); %> <table align="center" border="1"> <tr> <td>编号</td> <td>名称</td> <td>内容</td> <tr> <tr> <td><%=e.getNum() %></td> <td><%=e.getTitle() %></td> <td><%=e.getText() %></td> </tr> </table> <div align="center"> <input type="button" value="back" onclick="location.href='announcement.jsp'"/> </div> <% }else{ %> <h3 align="center">未找到该公告</h3> <div align="center"> <input type="button" value="back" onclick="location.href='announcement.jsp'"/> </div> <% } %> </body> </html>
点击查询按钮出现的问题,自己认为是DBUtil.java里的search函数出了问题,要么就是search.jsp引用函数时出了问题,但不知道具体出在哪里,请老师看一下
正在回答 回答被采纳积分+1
走了一遍所有的流程,只有在删除时,key的值你写成了字符形式,其他都是没有问题的,需要将"key"改为key。
显示效果如下,并且数据内容也是被删除的。
如上所属,你的代码是没有问题的,同学用的是eclipse的内置浏览器吗?如果是,建议使用火狐浏览器或者chrome浏览器,因为eclipse自带的浏览器有bug,推荐你以后测试的时候都使用chrome或者火狐即可,祝学习愉快~
删除时出现500页面,这是错误提示。 An error occurred at line: [12] in the jsp file: [/delete.jsp] The method search(Emp) in the type DBUtil is not applicable for the arguments (String) 9: <body> 10: <% 11: String key = request.getParameter("num"); 12: if (DBUtil.search(key)) { 13: DBUtil.map.remove(key); 14: %> 15: <h3 align ="center">鍒犻櫎鍏憡缂栧彿涓�:<%="key"%></h3>
查找也是
An error occurred at line: [12] in the jsp file: [/search.jsp] The method search(Emp) in the type DBUtil is not applicable for the arguments (String) 9: <body> 10: <% 11: String num=request.getParameter("num"); 12: boolean flag=DBUtil.search(num); 13: if(flag){ 14: Emp e=DBUtil.map.get(num); 15: %>
你好同学,应该将search方法的中的else语句去掉,因为在for循环的if语句中,如果num的值不匹配,就会执行else语句,此时flag的值就一直为false了。
修订之后的效果如下,如果我的建议解决了你的问题,请采纳,祝学习愉快~
delete.jsp
delete.jsp <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" import="com.imooc.db.*,com.imooc.bean.*,java.util.*"%> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Insert title here</title> </head> <body> <% String key = request.getParameter("num"); if (DBUtil.search(key)) { DBUtil.map.remove(key); %> <h3 align ="center">删除公告编号为:<%="key"%></h3> <% } %> <form action="announcement.jsp"> <div align="center"> <input type="button" value="back" onclick="location.href='announcement.jsp'"/> </div> </form> </body> </html>
update.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" import="com.imooc.db.*,com.imooc.bean.*,java.util.*"%> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Insert title here</title> </head> <body> <% String key = request.getParameter("num"); Emp e =DBUtil.map.get(key); %> <h3 align="center">修改公告信息</h3> <form align="center" action="update_do.jsp"> <table> <tr> <td>编号:</td> <td><input type="text" name="num" value = <%=e.getNum() %>></td> </tr> <tr> <td>标题:</td> <td><input type="text" name="title"value =<%=e.getTitle() %>></td> </tr> <tr> <td>内容:</td> <td><input type="text" name="text" value = <%=e.getText() %>></td> </tr> <tr> <td><input type="submit" value="确认修改" href="announcement.jsp"></td> </tr> </table> </form> </body> </html>
update_do.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" import="com.imooc.db.*,com.imooc.bean.*,java.util.*"%> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Insert title here</title> </head> <body> <% Map<String,Emp> map = DBUtil.map; Emp emp = map.get(request.getParameter("num")); emp.setTitle(request.getParameter("title")); emp.setText(request.getParameter("text")); %> <div align="center"> <input type="button" value="back" onclick="location.href='announcement.jsp'"/> </div> </body> </html>
完整代码这是,请老师指出问题。
- 参与学习 人
- 提交作业 1088 份
- 解答问题 10205 个
如果你有Java语言基础,又想以后从事Java Web开发,那么本路径是你的不二选择!本路径从网页搭建开始入手,通过大量案例来学习Java Web基础。定能助你完成Java Web小白的蜕变!
了解课程
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星