# servlet小案例 **Repository Path**: wei_dongdong/servlet-small-case ## Basic Information - **Project Name**: servlet小案例 - **Description**: servlet小案例 - **Primary Language**: Java - **License**: GPL-3.0 - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 2 - **Forks**: 0 - **Created**: 2020-08-30 - **Last Updated**: 2022-12-14 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # servletTest #### 介绍 servlet小案例(模拟数据库---学生管理系统) #### 软件架构 软件架构说明 #### 安装教程 1. xxxx 2. xxxx 3. xxxx #### 使用说明 ### Servlet案例之---登录注册和增删改查 (一)功能介绍 1.用户输入正确的密码进行登录 2.新用户可以进行注册 3.登录后显示学生的信息表 4.可以添加学生 5.可以修改学生已有信息 6.可以删除已有学生 7.可以显示登录的用户学号(每个页面都可以通过调用session显示) 8.可以记录浏览量 ```java 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; } } ``` ```java 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; } } ``` ```java 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); } } ``` ```java 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); } } ``` ```java 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); } } ``` ```java 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 ```java <%@ page contentType="text/html;charset=UTF-8" language="java" %> Title

学生登录登录

学号:
密码:
注册
``` register.jsp ```java <%@ page contentType="text/html;charset=UTF-8" language="java" %> Title

学生注册

学号:
密码:
姓名:
``` student.jsp ```java <%@ page import="util.StudentUtil" %> <%@ page contentType="text/html;charset=UTF-8" language="java" %> Title

学生信息展示

增加         用户:<%=session.getAttribute("sno")%>         浏览量:<%=application.getAttribute("count")%>
<% for(String s: StudentUtil.map.keySet()){%> <% } %>
学号 密码 姓名 操作
<%=StudentUtil.map.get(s).getSno()%> <%=StudentUtil.map.get(s).getPassword()%> <%=StudentUtil.map.get(s).getName()%> 修改 删除
``` error.jsp ```java <%@ page contentType="text/html;charset=UTF-8" language="java" %> Title 账户名或密码错误 返回登录 ``` insert.jsp ```java <%@ page contentType="text/html;charset=UTF-8" language="java" %> Title

添加学生

学号:
密码:
姓名:
``` update.jsp ```java <%@ page contentType="text/html;charset=UTF-8" language="java" %> Title
学号 ">
密码 ">
姓名 ">
``` #### 参与贡献 1. Fork 本仓库 2. 新建 Feat_xxx 分支 3. 提交代码 4. 新建 Pull Request #### 码云特技 1. 使用 Readme\_XXX.md 来支持不同的语言,例如 Readme\_en.md, Readme\_zh.md 2. 码云官方博客 [blog.gitee.com](https://blog.gitee.com) 3. 你可以 [https://gitee.com/explore](https://gitee.com/explore) 这个地址来了解码云上的优秀开源项目 4. [GVP](https://gitee.com/gvp) 全称是码云最有价值开源项目,是码云综合评定出的优秀开源项目 5. 码云官方提供的使用手册 [https://gitee.com/help](https://gitee.com/help) 6. 码云封面人物是一档用来展示码云会员风采的栏目 [https://gitee.com/gitee-stars/](https://gitee.com/gitee-stars/)