登录和修改(JSP)
通过案例学习jsp的常用知识点:
1.创建一个Map集合,用于存放学生信息,将学生信息存入Map中
2.通过page将需要的包导入
3.用request.getParameter通过name得到输入框的内容
4.session生命周期在整个会话期间,整个会话中都可以得到session中存放的信息,可以用于存放登录学生的学号,使每个页面都可以得到学号
5.application生命周期在整个web容器的生命期间,可以用于记录此项目的浏览人数,可以得到application中存放的数据
6. 点击修改,将所点击的学生信息传给另一个页面
package entity; public class Student { private String sno;//学号 private String password;//密码 private String name;//姓名 //构造函数 public Student(String sno, String password, String name) { this.sno = sno; this.password = password; this.name = name; } public String getSno() { return sno; } public void setSno(String sno) { this.sno = sno; } 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; } }
package util; import entity.Student; import java.util.HashMap; public class StudentUtil { //创建一个Map集合,用于存放学生信息 public static HashMap<String, Student> map=new HashMap<String, Student>(); static{ //向集合中传入信息 map.put("101",new Student("101","123","lili")); map.put("102",new Student("102","123","lisa")); map.put("103",new Student("103","123","coco")); } /** * 用于检测登录时用户输入的账户和密码 * @param stu * @return 返回布尔类型的数据 */ public static boolean login(Student stu){ boolean b=false; for(String s:map.keySet()){ if(map.get(s).getSno().equals(stu.getSno())&&map.get(s).getPassword().equals(stu.getPassword())){ b=true; break; } } return b; } }
index.jsp
<%-- Created by IntelliJ IDEA. User: Administrator Date: 2019/7/22 0022 Time: 上午 9:04 To change this template use File | Settings | File Templates. --%> <%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>Title</title> <style> .log{ align-content: center; text-align: center; } </style> </head> <body> <!--登录页面--> <form action="loginController.jsp" method="post"> <div class="log"> <h3>登录</h3> 学号:<input type="text" name="sno"/></br> 密码:<input type="password" name="password"/></br> <input type="submit" value="确定"/></div> </form> </body> </html>
loginController.jsp
<%@ page import="util.StudentUtil" %><%-- Created by IntelliJ IDEA. User: Administrator Date: 2019/7/22 0022 Time: 上午 9:04 To change this template use File | Settings | File Templates. --%> <%@ page contentType="text/html;charset=UTF-8" language="java" errorPage="error.jsp" %> <!--通过page将需要的包导入 --> <%@page import="entity.Student" %> <%@page import="util.StudentUtil" %> <%@ page import="javafx.application.Application" %> <html> <head> <title>Title</title> <style> table{ align-content: center; text-align: center; width: 500px; height: 200px; border: 2px solid #8ea4ff; } tr,td{ border: 2px solid #8ea4ff; } </style> </head> <body> <% String sno=request.getParameter("sno");//用request.getParameter通过name得到输入框的内容 String password=request.getParameter("password"); Student stu=new Student(sno,password,null);//创建一个Student对象 boolean b=StudentUtil.login(stu);//调用StudentUtil的login方法检测学号和密码 if(b){ //session生命周期在整个会话期间,整个会话中都可以得到session中存放的信息 //此处用于存放登录学生的学号,使每个页面都可以得到学号 session.setAttribute("count",sno); //application生命周期在整个web容器的生命期间 //此处用于记录此项目的浏览人数 //得到application中存放的数据 Object o= application.getAttribute("num"); if(o==null){//如果application中没有数据,则向application中存入1 application.setAttribute("num",1); }else{ //如果application中已经存在数据,则存放的数据加1 int num=Integer.parseInt(o.toString()); application.setAttribute("num",num+1); } %> <h2>学生信息</h2> <h3 style="text-align: right">登录账号:<%=session.getAttribute("count")%></h3> <h3 style="text-align: right">浏览量:<%=application.getAttribute("num")%></h3> <hr> <table> <tr> <td>学号</td> <td>密码</td> <td>姓名</td> <td></td> </tr> <% for(Student st:StudentUtil.map.values()){ %> <tr> <td><%=st.getSno()%></td> <td><%=st.getPassword()%></td> <td><%=st.getName()%></td> <!--点击修改,将所点击的学生信息传给另一个页面--> <td><a href="update.jsp?sno=<%=st.getSno()%>&&password=<%=st.getPassword()%>&&name=<%=st.getName()%>">修改</a></td> </tr> <%}%> </table>
<%}else{
//抛出异常
throw new Exception("账户或密码错误");
}%>
</body> </html>
<%-- Created by IntelliJ IDEA. User: Administrator Date: 2019/7/22 0022 Time: 下午 5:10 To change this template use File | Settings | File Templates. --%> <%@ page contentType="text/html;charset=UTF-8" language="java" isErrorPage="true" %> <html> <head> <title>Title</title> </head> <body> <%=exception.getMessage()%> </body> </html>
update.jsp
<%-- Created by IntelliJ IDEA. User: Administrator Date: 2019/7/22 0022 Time: 上午 10:10 To change this template use File | Settings | File Templates. --%> <%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head> <title>Title</title> <style> table{ width: 400px; height: 150px; border:2px solid #8ea4ff; text-align: center; } tr,td{ border:2px solid #8ea4ff; } </style> </head> <body> <h3 style="text-align: right">登录账号:<%=session.getAttribute("count")%></h3> <form action="updateController.jsp" method="post"> <h2>修改信息</h2> <hr> <table> <tr> <td style=" 30%"> 学号 </td> <td><input type="text" name="sno" value="<%=request.getParameter("sno")%>"></td> </tr> <tr> <td> 密码 </td> <td><input type="text" name="password" value="<%=request.getParameter("password")%>"></td> </tr> <tr> <td> 姓名 </td> <td><input type="text" name="name" value="<%=request.getParameter("name")%>"></td> </tr> </table> <input type="submit" value="确定"> </form> </body> </html>
updateController.jsp
<%-- Created by IntelliJ IDEA. User: Administrator Date: 2019/7/22 0022 Time: 上午 10:18 To change this template use File | Settings | File Templates. --%> <%@ page contentType="text/html;charset=UTF-8" language="java" %> <%@page import="entity.Student" %> <%@page import="util.StudentUtil" %> <html> <head> <title>Title</title> <style> table{ align-content: center; text-align: center; width: 500px; height: 200px; border: 2px solid #8ea4ff; } tr,td{ border: 2px solid #8ea4ff; } </style> </head> <body> <% String sno=request.getParameter("sno"); String password=request.getParameter("password"); String name=request.getParameter("name"); for(String s:StudentUtil.map.keySet()){//遍历map中所有的键 if(StudentUtil.map.get(s).getSno().equals(sno)){ StudentUtil.map.get(s).setSno(sno); StudentUtil.map.get(s).setPassword(password); StudentUtil.map.get(s).setName(name); } } %> 修改成功! <h2>学生信息</h2> <h3 style="text-align: right">登录账号:<%=session.getAttribute("count")%></h3> <hr> <table> <tr> <td>学号</td> <td>密码</td> <td>姓名</td> <td></td> </tr> <% for(Student st:StudentUtil.map.values()){ %> <tr> <td><%=st.getSno()%></td> <td><%=st.getPassword()%></td> <td><%=st.getName()%></td> <td><a href="update.jsp?sno=<%=st.getSno()%>&&password=<%=st.getPassword()%>&&name=<%=st.getName()%>">修改</a></td> </tr> <%}%> </table> </body> </html>