知方号

知方号

Java Web项目案例之

登录注册和增删改查(jsp+servlet)

(一)功能介绍

1.用户输入正确的密码进行登录

2.新用户可以进行注册

3.登录后显示学生的信息表

4.可以添加学生

5.可以修改学生已有信息

6.可以删除已有学生

7.可以显示登录的用户学号(每个页面都可以通过调用session显示)

8.可以记录浏览量

(二)页面展示

登录页面

 

注册页面

 

学生信息页面

 

添加页面

 

添加之后的学生信息表

 

修改页面

 

修改之后的学生信息表

 

删除之后的学生信息表

 

(三)项目代码

 

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 { public static HashMap map=new HashMap(); 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")); map.put("104",new Student("104","123","mark")); } public static boolean log(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; } } return b; } public static boolean reg(Student stu){ map.put(stu.getSno(),stu); return true; }}

 

package servlet;import entity.Student;import org.omg.CORBA.Request;import util.StudentUtil;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 java.io.IOException;@WebServlet("/LoginServlet")public class LoginServlet extends HttpServlet { @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { String sno=req.getParameter("sno"); String password=req.getParameter("password"); Student s=new Student(sno,password,null); boolean b=StudentUtil.log(s); if(b==true){ ServletContext a=this.getServletContext(); Object o=a.getAttribute("count"); if(o==null){ a.setAttribute("count",1); }else{ int count=Integer.parseInt(o.toString()); a.setAttribute("count",count+1); } HttpSession se=req.getSession(); se.setAttribute("sno",sno); req.setAttribute("sno",sno); req.getRequestDispatcher("student.jsp").forward(req,resp); }else{ req.getRequestDispatcher("error.jsp").forward(req,resp); } } @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { doPost(req, resp); } }

 

package servlet;import entity.Student;import util.StudentUtil;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 java.io.IOException;@WebServlet("/RegisterServlet")public class RegisterServlet extends HttpServlet { @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { String sno=req.getParameter("sno"); String password=req.getParameter("password"); String name=req.getParameter("name"); StudentUtil.reg(new Student(sno,password,name)); req.getRequestDispatcher("/index.jsp").forward(req,resp); } @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { doPost(req, resp); }}

 

package servlet;import entity.Student;import util.StudentUtil;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 java.io.IOException;@WebServlet("/InsertServlet")public class InsertServlet extends HttpServlet { @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { String sno=req.getParameter("sno"); String password=req.getParameter("password"); String name=req.getParameter("name"); StudentUtil.map.put(sno,new Student(sno,password,name)); req.getRequestDispatcher("student.jsp").forward(req,resp); } @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { doPost(req, resp); }}

 

package servlet;import util.StudentUtil;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 java.io.IOException;@WebServlet("/UpdateServlet")public class UpdateServlet extends HttpServlet { @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { String sno=req.getParameter("sno"); String password=req.getParameter("password"); String name=req.getParameter("name"); StudentUtil.map.get(sno).setName(name); StudentUtil.map.get(sno).setPassword(password); req.getRequestDispatcher("student.jsp").forward(req,resp); } @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { doPost(req, resp); }}package servlet;import util.StudentUtil;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 java.io.IOException;@WebServlet("/DeleteServlet")public class DeleteServlet extends HttpServlet { @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { String sno= req.getParameter("sno"); StudentUtil.map.remove(sno); req.getRequestDispatcher("student.jsp").forward(req,resp); } @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { doPost(req, resp); }}

 

 

index.jsp

Title 登录 学号: 密码: 注册

 

 

register.jsp

Title 学号: 密码: 姓名:  student.jsp Title增加        用户:        浏览量: 学号 密码 姓名 修改 删除

 

 error.jsp Title账户名或密码错误返回登录

 

 

insert.jsp

Title 学号: 密码: 姓名:

 

 

update.jsp

Title 学号 密码 姓名

 

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至lizi9903@foxmail.com举报,一经查实,本站将立刻删除。