用户可以选择管理员和游客两种登录方式,管理员必须输入正确密码,游客不能输入任何密码

项目结构

index.jsp

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<jsp:useBean id="userInfo" class="beans.UserBean" scope="session"/>
<%request.setCharacterEncoding("utf-8");%>
<jsp:setProperty name="userInfo" property="*"/>
<%String valueoption = (String) request.getAttribute("uid");String option1 = "管理员";String option2 = "游客";String valueoption1 = "admin";String valueoption2 = "guest";if ("guest".equals(valueoption)) {option1 = "游客";option2 = "管理员";valueoption1="guest";valueoption2="admin";}
%>
<!DOCTYPE html>
<html><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><title>系统登录</title></head><body><h1>请输入你的登录信息</h1><div style="color:red"><%=request.getAttribute("errMsg") == null ? "" : request.getAttribute("errMsg")%></div><form  method="POST" action="Checklogin"><p>账号:<select name="uid"  style="width: 100px"><option value= <%=valueoption1%>> <%=option1%></option><option value= <%=valueoption2%>><%=option2%></option></select></p><p>口令:<input   style="width: 100px" type="password" name="pwd" value=<%=request.getAttribute("pwd") == null ?"": request.getAttribute("pwd")%> /></p><div><input type="submit" value="确定" /></div></form></body>
</html>

Checklogin.java

package beans;import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;public class Checklogin extends HttpServlet {protected void processRequest(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {response.setContentType("text/html;charset=UTF-8");request.setCharacterEncoding("utf-8");String uid = request.getParameter("uid");String pwd = request.getParameter("pwd");String errMsg = "";if ("admin".equals(uid)) {if ("1234567".equals(pwd)) {response.sendRedirect("admin.jsp");return;}errMsg = "管理员口令不正确!";request.setAttribute("uid", uid);request.setAttribute("pwd", pwd);request.setAttribute("errMsg",errMsg);request.getRequestDispatcher("index.jsp").forward(request,response);return;} else if ("guest".equals(uid)) {if (pwd == null || pwd.isEmpty()) {//口令不输入,得到的口令是长度为0的字符串//登录成功response.sendRedirect("admin.jsp");return;}errMsg = "游客不能也无需输入口令!";request.setAttribute("errMsg",errMsg);request.setAttribute("uid", uid);request.setAttribute("pwd", pwd);request.getRequestDispatcher("index.jsp").forward(request, response);return;}try (PrintWriter out = response.getWriter()) {/* TODO output your page here. You may use following sample code. */out.println("<!DOCTYPE html>");out.println("<html>");out.println("<head>");out.println("<title>Servlet Checklogin</title>");out.println("</head>");out.println("<body>");out.println("<h1>Servlet Checklogin at " + request.getContextPath() + "</h1>");out.println("</body>");out.println("</html>");}}// <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">/*** Handles the HTTP <code>GET</code> method.** @param request servlet request* @param response servlet response* @throws ServletException if a servlet-specific error occurs* @throws IOException if an I/O error occurs*/@Overrideprotected void doGet(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {processRequest(request, response);}/*** Handles the HTTP <code>POST</code> method.** @param request servlet request* @param response servlet response* @throws ServletException if a servlet-specific error occurs* @throws IOException if an I/O error occurs*/@Overrideprotected void doPost(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {processRequest(request, response);}/*** Returns a short description of the servlet.** @return a String containing servlet description*/@Overridepublic String getServletInfo() {return "Short description";}// </editor-fold>}

UserBean.java

package beans;public class UserBean {private String pwd;private String uid;public UserBean() {}public UserBean(String pwd, String uid) {this.pwd = pwd;this.uid = uid;}/*** @return the pwd*/public String getPwd() {return pwd;}/*** @param pwd the pwd to set*/public void setPwd(String pwd) {this.pwd = pwd;}/*** @return the uid*/public String getUid() {return uid;}/*** @param uid the uid to set*/public void setUid(String uid) {this.uid = uid;}}

admin.jsp

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<jsp:useBean id="userInfo" class="beans.UserBean" scope="session"/>
<!DOCTYPE html>
<html><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><title>系统管理</title></head><body><h1>登录成功!<br>你的登录账号是:<jsp:getProperty name="userInfo" property="uid"/></h1><br><p>程序制作人信息:<br>姓名:以太潮汐<br></p></body>
</html>

运行结果

使用Servlet实现简单的登录验证相关推荐

  1. 使用filter过滤器实现简单用户登录验证(不用配置web.xml文件)

    一.filter过滤器的作用 如果想要获取中文字符,或者是显示提交的中文,就需要添加以下代码,来防止乱码的情况发生. request.setCharacterEncoding("utf-8& ...

  2. jsp java 登陆_jsp+java servlet实现简单用户登录

    jsp+java servlet实现简单用户登录(使用数据库,包括注册页面) 功能介绍 本项目通过使用jsp和servlet实现简单的用户登录.主要逻辑为:如果用户不存在,则首先进行注册(注册信息同步 ...

  3. 简单Flask登录验证

    记录一下学习Flask的过程 下面是一个简单的FLask的登录验证代码 from flask import Flask from flask import requestapp = Flask(__n ...

  4. Ajax实现简单的登录验证与帐号注册

    上一篇博客介绍了Ajax的GET和POST方法以及上传文件的进度条展示,这篇博客将介绍一个简单的登录与注册功能的实现 设计HTML 这个是即将成为我毕业设计的一个网站, 在导航栏我添加了两个注册和登录 ...

  5. JSP+Servlet + JDBC 实现简单的登录验证模块

    数据库设计+编码+运行调试 数据库准备: 二话不说,上图 文件组织如下: 首先写出三个JSP页面文件 login.jsp <%@ page language="java" c ...

  6. Jsp+Servlet+Mysql简单的登录

    图片 JSP: <div id = "main">           <form action = "${pageContext.request.co ...

  7. Flask Web开发入门(一)之简单的登录验证

    Flask is a microframework for Python based on Werkzeug, Jinja 2 and good intentions. 我们的目标是通过Flask实现 ...

  8. mysql jsp javabean servlet 注册登录_jsp+javabean+servlet实现简单的登录

    登录功能 注释:过程中的错误看5.6.7,(DBUtil类中,用户名密码填写自己的用户名和密码). 1.首先进行Mysql数据库的建立 在mysql数据库中建立test数据库,新建表user,表中两个 ...

  9. 简单的登录验证程序代码

    No. 页面名称 描述 1 login.jsp 提供用户的登陆表单,可以输入用户id和密码. 2 check.jsp 登陆检查页,根据表单提交过来的id和密码进行数据库验证,成功跳转到登陆成功页,否则 ...

最新文章

  1. win7下的IP-主机名映射
  2. 科普丨浅谈人工神经网络跌宕起伏七十年
  3. Angular和SAP C4C的事件处理队列
  4. 13.2System类中的常用方法
  5. 信号扫描_科研必备“武器”之扫描电子显微镜
  6. virtualbox下安装archlinux
  7. 短信验证码倒计时代码
  8. oracle学习笔记---oracle10g 卸载方法
  9. 放弃月薪过万的城市工作,返乡创业做农业,面对未知风险,你敢尝试吗?
  10. python必备基础代码-新手上路必学的Python函数基础知识,全在这里了(多段代码举例)...
  11. Collections.sort new Compartor 用法
  12. 前端性能优化(慕课网笔记)-2-渲染优化(浏览器)
  13. 尺规作图将任意角度三等分
  14. Chrome打不开baidu的解决方法
  15. 根据条件对Excel表中数据进行计数
  16. mindmanager2018优化
  17. 荣耀magic5和vivox90参数对比 荣耀magic5和vivox90哪个好
  18. [美国访问学者]J1签证和商务B1签证的区别
  19. Simple java里面的paint(Graphics g)的理解
  20. oracle11g闪退 win7,win7 oracle11g

热门文章

  1. 同济大学软件学院院长谈择业—关于嵌入式方向
  2. ssm+JSP计算机毕业设计中国瑰宝——戏曲赏析网92n88【源码、程序、数据库、部署】
  3. 【Lambda】正序、倒序
  4. 可编程 USB 转串口适配器开发板 SHT3x-DIS 温湿度传感器芯片
  5. 图解电动汽车:电动汽车电池系统
  6. Python的优缺点
  7. 兰亭集势任命Ezbuy联合创始人担任代理CFO
  8. 云计算机的三层模式阅读题,云计算服务的类型信息系统项目管理师教程第3版真题习题与考点...
  9. 今日哪些热门股有望涨停?
  10. Excel函数大全-09逻辑函数