助教老师们,我的代码有很多问题,我已经做了好久了,希望你们帮我认真地找出错误

助教老师们,我的代码有很多问题,我已经做了好久了,希望你们帮我认真地找出错误

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>


正在回答

登陆购买课程后可参与讨论,去登陆

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") %> />


如果我的回答解决了你的疑惑,请采纳!祝学习愉快!


  • 松柏i 提问者 #1
    你很专业噢 棒棒哒
    2018-10-23 13:06:25
好帮手慕阿莹 2018-10-22 18:51:10

1、查找的时候报空指针异常。

http://img1.sycdn.imooc.com//climg/5bcda94f0001735f06880698.jpg

修改建议:

select.jsp

http://img1.sycdn.imooc.com//climg/5bcda9750001a7c708280201.jpg

因为你的data的map值里是Integer类型的。

http://img1.sycdn.imooc.com//climg/5bcdaa030001330e09750249.jpg

2、修改页面同理。

 ListInfo l = map.get(request.getParameter("id"));

改为:

 ListInfo l = map.get(Integer.parseInt(request.getParameter("id")));

3、删除页面同理。

如果我的回答解决了你的疑惑,请采纳!祝学习愉快!


  • 提问者 松柏i #1
    是的,可以解决,原来这个方法返回值是String,我运行了一下,大致符合题意,还有一个小问题,我在修改页面点击Back的时候,想返回到公告列表页面,但页面上只出现一个null;这个又该如何修改呢? 粘上该处表单的代码。 <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>
    2018-10-22 22:15:46
问题已解决,确定采纳
还有疑问,暂不采纳

恭喜解决一个难题,获得1积分~

来为老师/同学的回答评分吧

0 星
Java Web基础入门2018版
  • 参与学习       716    人
  • 提交作业       185    份
  • 解答问题       1363    个

会Java?懂前端基础?想学后台开发?那么,赶快来学习《Java Web入门》路径吧。本路径主要介绍Java Web的基础知识,并配有大量案例,定会让你收获多多!

了解课程
请稍等 ...
意见反馈 帮助中心 APP下载
官方微信

在线咨询

领取优惠

免费试听

领取大纲

扫描二维码,添加
你的专属老师