请问不相同的key值,Map添加值的时候还是覆盖了?
web.xml
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0"> <display-name>Libary</display-name> <welcome-file-list> <welcome-file>index.html</welcome-file> <welcome-file>index.htm</welcome-file> <welcome-file>index.jsp</welcome-file> <welcome-file>default.html</welcome-file> <welcome-file>default.htm</welcome-file> <welcome-file>default.jsp</welcome-file> </welcome-file-list> <servlet> <description></description> <display-name>InitServlet</display-name> <servlet-name>InitServlet</servlet-name> <servlet-class>Servlet.InitServlet</servlet-class> <load-on-startup>2</load-on-startup> </servlet> <servlet-mapping> <servlet-name>InitServlet</servlet-name> <url-pattern>/InitServlet</url-pattern> </servlet-mapping> <filter> <filter-name>OneFilter</filter-name> <filter-class>Filter.One</filter-class> <init-param> <param-name>charSet</param-name> <param-value>UTF-8</param-value> </init-param> </filter> <filter-mapping> <filter-name>OneFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> </web-app>
package Servlet; import java.io.IOException; import java.util.ArrayList; import java.util.List; import javax.servlet.ServletConfig; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import User.User; /** * Servlet implementation class InitServlet */ public class InitServlet extends HttpServlet { private static final long serialVersionUID = 1L; @Override public void init() throws ServletException { // 创建一个User集合类用于保存用户集合 List<User> UserList = new ArrayList<User>(); // 将集合保存在ServletContext域中 this.getServletContext().setAttribute("list",UserList); } }
package Servlet; import java.io.IOException; import java.util.List; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import User.User; /** * Servlet implementation class RegistServlet */ @WebServlet("/RegistServlet") public class RegistServlet extends HttpServlet { private static final long serialVersionUID = 1L; protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //获得注册页面的相关信息 String username = request.getParameter("username"); String password = request.getParameter("password"); String checkPWD = request.getParameter("checkPWD"); String phone = request.getParameter("phone"); String email = request.getParameter("email"); //进行后台验证 防止吊毛用户在前端删除验证方式 String usernameRegex = "[a-zA-Z_0-9]{6,12}"; boolean flag1 = username.matches(usernameRegex); String passwordRegex = "[0-9]{6,12}"; boolean flag2 = password.matches(passwordRegex); String phoneRegex = "1[34578][0-9]{9}"; boolean flag3 = phone.matches(phoneRegex); String emailRegex = "[a-zA-Z_0-9]{6,}@([a-zA-Z]+ | \\d+)(\\.[a-zA-Z]+)+"; boolean flag4 = email.matches(emailRegex); System.out.println(username); System.out.println(password); System.out.println(checkPWD); System.out.println(phone); System.out.println(email); if(flag1==false && flag2==false && flag3==false && flag4==false) { //不符合注册条件,进行重定向并且转发数据 response.sendRedirect(request.getContextPath()+"/regist.jsp?flag=1"); }else { //将符合的信息存储在User中 User user = new User(); user.setUsername(username); user.setPassword(password); user.setEmail(email); user.setPhone(phone); // 将User存入集合中 List<User> UserList = (List<User>)this.getServletContext().getAttribute("list"); UserList.add(user); this.getServletContext().setAttribute("UserList", UserList); //然后进行页面跳转 response.sendRedirect(request.getContextPath()+"/index.jsp?flag=1"); } } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } }
package Servlet; import java.io.IOException; import java.util.HashMap; import java.util.List; import java.util.Map; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import User.User; /** * Servlet implementation class LoginServlet */ @WebServlet("/LoginServlet") public class LoginServlet extends HttpServlet { private static final long serialVersionUID = 1L; protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String username = request.getParameter("username"); String password = request.getParameter("password"); //获得集合 List<User> UserList = (List<User>) this.getServletContext().getAttribute("UserList"); //判断 for(User u : UserList) { if(u.getUsername().equals(username) && u.getPassword().equals(password)) { //进行页面跳转 request.getSession().setAttribute("username", username); response.sendRedirect(request.getContextPath()+"/server.jsp"); }else{ //页面跳转 } } } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } }
package Servlet; import java.io.IOException; import java.util.HashMap; import java.util.Map; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; /** * Servlet implementation class CatgoryServlet */ @WebServlet("/CatgoryServlet") public class CatgoryServlet extends HttpServlet { private static final long serialVersionUID = 1L; protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String id = request.getParameter("id"); String catgoryName = request.getParameter("catgoryName"); String description = request.getParameter("description"); //创建Map集合来存储catgoryName key-id value-catgoryName Map<String,String> map = new HashMap<String,String>(); map.put(id, catgoryName); this.getServletContext().setAttribute("map", map); Map<String,String> maps = (Map<String, String>) this.getServletContext().getAttribute("map"); for(String key : maps.keySet()) { System.out.println(key+"==="+maps.get(key)); } } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub doGet(request, response); } }
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; String flag = request.getParameter("flag"); %> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta charset="UTF-8"> <title>图书馆管理系统首页</title> <script type="text/javascript"> var flag = <%=flag%>; if(flag == 1){ alert("注册成功!"); } </script> </head> <body> <center> <h1>登录 | <a href="<%=basePath%>regist.jsp">注册</a></h1> <form action="<%=basePath%>/LoginServlet" method="post"> <table width="350px" cellspacing="0px" cellpadding="0px" border="1px"> <tr> <td>用户名</td> <td><input type="text" name="username" placeholder="用户名为3-12位字母数字或下划线组合" ></td> </tr> <tr> <td>密 码</td> <td><input type="password" name="password" placeholder="长度为6-12位的纯数字" ></td> </tr> <tr> <td colspan="2" style="text-align:center"> <input type="submit" value="登录"> <input type="reset" value="取消"> </td> </tr> </table> </form> </center> </body> </html>
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; String flag = request.getParameter("flag"); %> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta charset="UTF-8"> <title>用户注册</title> </head> <body> <script type="text/javascript"> function mk(){ var id1 = document.getElementById("password").value; var id2 = document.getElementById("password2").value; if(id1==id2){ }else{ alert("两次密码不相同!"); } } var flag = <%=flag%>; if(flag ==1 ){ alert("注册信息不正确!"); } </script> <center> <h1>用户注册</h1> <form action="<%=basePath%>RegistServlet" method="post"> <table width="400px" cellspacing="0px" cellpadding="0px" border="1px"> <tr> <td>用户名</td> <td><input type="text" name="username" placeholder="用户名为3-12位字母数字或下划线组合" pattern="[a-zA-Z_0-9]{3,12}"></td> </tr> <tr> <td>密 码</td> <td><input type="password" name="password" placeholder="密码长度为6-12位的纯数字" id="password" pattern="[0-9]{6,12}"></td> </tr> <tr> <td>确认密码</td> <td><input type="password" name="checkPWD" placeholder="密码长度为6-12位的纯数字" id="password2" pattern="[0-9]{6,12}"></td> </tr> <tr> <td>手机号码</td> <td><input type="text" name="phone" placeholder="请输入正确的手机号码格式" onclick="mk()" pattern="1[34578][0-9]{9}"></td> </tr> <tr> <td>邮箱</td> <td><input type="email" name="email" placeholder="请输入正确邮箱格式" required="required"></td> </tr> <tr> <td colspan="2" style="text-align:center"> <input type="submit" value="注册"> <input type="reset" value="重置"> </td> </tr> </table> </form> </center> </body> </html>
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta charset="UTF-8"> <title>图书后台管理系统</title> </head> <frameset rows="20%,*"> <frame src="./top.jsp"></frame> <frameset cols="10%,*"> <frame src="./left.jsp"></frame> <frame name="main"></frame> </frameset> </frameset> </html>
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <% String name = (String)request.getSession().getAttribute("username"); %> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta charset="UTF-8"> <title>Insert title here</title> </head> <body> <center> <h1> 图书后台管理系统<span style="font-size:12px">您好,<%=name %> </h1> </center> </body> </html>
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Insert title here</title> </head> <body> <p><a href="<%=basePath%>/catgory.jsp" target="main">分类添加</a></p> <p><a href="<%=basePath%>/addbook.jsp" target="main">图书添加</a></p> <p><a href="<%=basePath%>/show.jsp" target="main">图书查询</a></p> </body> </html>
<%@ page language="java" contentType="text/html; charset=UTF-8" 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" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta charset="UTF-8"> <title>图书分类添加</title> </head> <body> <center> <h1>图书分类添加</h1> <form action="<%=basePath%>/CatgoryServlet" method="post"> <table width="400px" cellspacing="0px" cellpadding="0px" border="1px"> <tr> <td>分类ID</td> <td><input type="text" name="id"></td> </tr> <tr> <td>名 字</td> <td><input type="text" name="catgoryName"></td> </tr> <tr> <td>描 述</td> <td><input type="text" name="description"></td> </tr> <tr> <td colspan="2" style="text-align:center"> <input type="submit" value="添加"> <input type="reset" value="重置"> </td> </tr> </table> </form> </center> </body> </html>
<%@page import="java.util.Map"%> <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; Map<String,String> maps =(Map<String,String>) this.getServletContext().getAttribute("map"); %> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta charset="UTF-8"> <title>图书添加</title> </head> <body> <center> <h1>图书添加</h1> <form action="<%=basePath%>/AddBookServlet" method="post"> <table width="400px" cellspacing="0px" cellpadding="0px" border="1px"> <tr> <td>图书ID</td> <td><input type="text" name="id" placeholder="请输入数字" pattern="\d+" required="required"></td> </tr> <tr> <td>图书名</td> <td><input type="text" name="bookName"></td> </tr> <tr> <td>图书分类</td> <td> <select name="catgoryName"> <% for(String key : maps.keySet()){ %> <option value="<%=maps.get(key) %>"><%=maps.get(key)%></option> <% } %> </select> </td> </tr> <tr> <td>价格</td> <td><input type="text" name="price" placeholder="请输入价格" ></td> </tr> <tr> <td>描述</td> <td><input type="text" name="description" placeholder="请输入描述信息"></td> </tr> <tr> <td colspan="2" style="text-align:center"> <input type="submit" value="添加"> <input type="reset" value="重置"> </td> </tr> </table> </form> </center> </body> </html>
其他页面没有抒写业务逻辑就不贴出来了
0
收起
正在回答 回答被采纳积分+1
2回答
好帮手慕阿莹
2018-11-01 18:28:36
首先在map中不同的key值不会被覆盖,
不过,同学可以把域想象成一个Map 。同一个key值只能存一个 value,所以,每次往域中key为“map”中存时,会覆盖上次存的map。例如你的代码:
this.getServletContext().setAttribute("map", map);
如果再来一个map,就会把之前的存的覆盖掉了。
所以,
1、我们可以在全局设置一个 List
public static List<Map<String ,String>> mapList = newArraryList<Map<String ,String>>();
2、
然后我们把id,catgoryName,description 等都存到新创建的map中。
Map<String,String> map = new HashMap<String,String>(); map.put(“id”,id ); map.put(“catgoryName”,catgoryName ); map.put(“description ”,description );
3、把这个创建好的map (一个分类) 放到 mapList 中去。
mapList.add(map);
4、最后我们再把这个mapList集合放到域中,可以起名叫 mapList
this.getServletContext().setAttribute("mapList", mapList);
这样,每次放一个分类在里边,都可以存在这个mapList,中了。
可以遍历mapList,就可以获取所以添加的类了。
5、建议同学把这个过程放到Dao的实现类中,写成一个方法,在Servlet中,直接调用这个方法,把获取的这几个参数传递给这个方法就可以了。实现了代码的分成,比较清晰。
如果我的回答解决了你的疑惑,请采纳!祝学习愉快!
从网页搭建入门Java Web2018版
- 参与学习 人
- 提交作业 1088 份
- 解答问题 10205 个
如果你有Java语言基础,又想以后从事Java Web开发,那么本路径是你的不二选择!本路径从网页搭建开始入手,通过大量案例来学习Java Web基础。定能助你完成Java Web小白的蜕变!
了解课程
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星