老师帮忙看看怎么把标题和内容显示出来呢
<%@ page language="java" import="java.util.*,imooc.jsptest.*" 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>公告列表</title>
</head>
<body>
<% final Map<String, Users> map = new HashMap<String, Users>();
Users users=map.get(request.getParameter("accout"));
%>
<form action="list.jsp">
<table border=1 width=50px>
<tr>
<td><input type="text" value="公告编号:"></td>
<td><input type="text" value="<%=request.getParameter("accout")%>" /></td>
<td><input type="submit" value="Back"></td>
</tr>
</table>
</form>
<form >
<table border=1 width=50px>
<tr>
<td><input type="text" value="编号"></td>
<td><input type="text" value="名称"></td>
<td><input type="text" value="内容"></td>
<td><input type="text" value="删除"></td>
<td><input type="text" value="修改"></td>
</tr>
<tr>
<td><input type="text" value="<%=request.getParameter("accout")%>"/></td>
<td><input type="text" value="<%=users.getName()%>"/></td>
<td><input type="text" value="<%=users.getContent()%>"></td>
<td><input type="text" value="<a href=delete.jsp?accout=<%=request.getParameter("accout")%>>删除</a>"></td>
<td><input type="text" value="<a href=modify.jsp?accout=<%=request.getParameter("accout")%>&name=<%=request.getParameter("name")%>&content=<%=request.getParameter("content")%>>修改</a>"></td>
</tr>
</table>
</form>
</body>
</html>
上面代码是我写的,有些代码肯定存在逻辑错误,请老师帮忙给看看。
正在回答
测试了同学的代码,有一个list.jsp是正确的,有一个是错误的,那个错误的是你的代码吗?如果是,根据错误提示来看是空指针异常,造成空指针异常的原因是获取的map集合为null,不应该新建一个map集合,而是应该从DBUtil类中获取map集合,修订之后的代码如下图:
修订之后的效果如下图,祝学习愉快~
<%@ page language="java" import="java.util.*" 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>修改公告信息</title> </head> <body> <form > <table border=1 width=50px> <tr> <td><input type="text" name="编号" value=<%=request.getParameter("accout") %>></td> </tr> <tr> <td><input type="text" name="标题" value=<%=request.getParameter("name") %>>"></td> </tr> <tr> <td><input type="text" name="内容" value=<%=request.getParameter("content") %>></td> </tr> </table> </form> </body> </html>
<%@ page language="java" import="java.util.*,imooc.jsptest.*,imooc.jsptest1.*" 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>删除页面</title> </head> <body> 删除公告编号为:<%=request.getParameter("accout")%> </body> </html>
<%@ page language="java" import="java.util.*,imooc.jsptest.*" 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>公告列表</title> </head> <body> <% final Map<String, Users> map = new HashMap<String, Users>(); Users users=map.get(request.getParameter("accout")); %> <form action="list.jsp"> <table border=1 width=50px> <tr> <td><input type="text" value="公告编号:"></td> <td><input type="text" value="<%=request.getParameter("accout")%>" /></td> <td><input type="submit" value="Back"></td> </tr> </table> </form> <form > <table border=1 width=50px> <tr> <td><input type="text" value="编号"></td> <td><input type="text" value="名称"></td> <td><input type="text" value="内容"></td> <td><input type="text" value="删除"></td> <td><input type="text" value="修改"></td> </tr> <tr> <td><input type="text" value="<%=request.getParameter("accout")%>"/></td> <td><input type="text" value="<%=users.getName()%>"/></td> <td><input type="text" value="<%=users.getContent()%>"></td> <td><input type="text" value="<a href=delete.jsp?accout=<%=request.getParameter("accout")%>>删除</a>"></td> <td><input type="text" value="<a href=modify.jsp?accout=<%=request.getParameter("accout")%>&name=<%=request.getParameter("name")%>&content=<%=request.getParameter("content")%>>修改</a>"></td> </tr> </table> </form> </body> </html>
<%@ page language="java" import="java.util.*,imooc.jsptest.*,java.lang.*,imooc.jsptest1.*" pageEncoding="utf-8"%> <% String path = request.getContextPath(); String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path + "/"; %> <title>公告列表</title> </head> <body> <% String s = request.getParameter("accout"); Users users = new Users(s, null, null); boolean flag = DBUtil.select(users); Map<String, Users> map = DBUtil.map; if (flag == true) { %> <h3>公告列表为:</h3> <hr> <form action="details.jsp"> <table border=1 width=50px> <tr> <td><input type="text" value="公告编号:"></td> <td><input type="text" name="accout"></td> <td><input type="submit" value="Select"></td> </tr> </table> </form> <form action="modify.jsp"> <table border=1 width=50px> <tr> <td><input type="text" value="编号"></td> <td><input type="text" value="名称"></td> <td><input type="text" value="内容"></td> <td><input type="text" value="删除"></td> <td><input type="text" value="修改"></td> </tr> <% for (String key : map.keySet()) { Users us = map.get(key); %> <tr> <td><%=us.getAccout()%></td> <td><%=us.getName()%></td> <td><%=us.getContent()%></td> <td><a href="delete.jsp?accout=<%=us.getAccout()%>">删除</a></td> <td><a href="">修改</a></td> </tr> <% } %> </table> </form> <% } %> </body> </html>
<%@ page language="java" import="java.util.*" 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>登录界面</title> </head> <body> <h3 align="center">登录界面</h3> <hr> <form action="list.jsp"> <table align="center"> <tr> <td>用户名:</td> <td><input type="text" name="accout"></td> </tr> <tr> <td>密码:</td> <td><input type="password" password="password"></td> </tr> <tr> <td><input type="submit" value="登录"></td> </tr> </table> </form> </body> </html>
package imooc.jsptest; public class Users { private String accout; private String name; private String content; /** * @param accout * @param name * @param content */ public Users(String accout, String name, String content) { super(); this.accout = accout; this.name = name; this.content = content; } public String getAccout() { return accout; } public void setAccout(String accout) { this.accout = accout; } 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 imooc.jsptest1;
import imooc.jsptest.*;
import java.util.*;
public class DBUtil {
public static java.util.Map<String, Users> map = new HashMap<String, Users>();
static {
map.put("101", new Users("101", "开学", "请同学们于9月1日前来报到!"));
map.put("102", new Users("102", "选课", "开始选课啦~"));
map.put("103", new Users("103", "竞选班委", "将于近期竞选班干部~"));
map.put("104", new Users("104", "评选奖学金", "评选奖学金啦~"));
}
public static boolean select(Users us) {
boolean flag = false;
for (String s : map.keySet()) {
Users users = map.get(s);
if (us.getAccout().equals(users.getAccout())) {
flag = true;
break;
}
}
return flag;
}
}
要将代码粘贴到回答中,粘贴到回复中是有字数限制的,并且会失去代码格式的,你可以按下图进行操作,然后将粘贴上来的代码设置一下代码格式,祝学习愉快~
你好同学,下图是你的实现效果吗?如果不是,建议你把login.jsp页面也粘贴上来,还有你对应的User类和DBUtil类,方便老师进行调试并具有针对性的解答,注意要将代码粘贴到回答中哦,否则会失去代码格式,祝学习愉快~
- 参与学习 人
- 提交作业 1088 份
- 解答问题 10205 个
如果你有Java语言基础,又想以后从事Java Web开发,那么本路径是你的不二选择!本路径从网页搭建开始入手,通过大量案例来学习Java Web基础。定能助你完成Java Web小白的蜕变!
了解课程
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星