实现步骤:

1、创建用户登录提交界面

2、创建处理用户登录请求servlet组件Main

3、创建代表登录成功响应的servlet的组件LoginSuccess

4、创建代表登录失败响应的servlet组件LoginFail

【1代码login.html】

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>登录</title>
</head>
<body bgcolor="#FFFFFF">
<center>欢迎登录系统</center>
<form name="login" action="Main" method="post">
<label>用户名:</label>
<input type="text" name="userID" value="">
<label>密码:</label>
<input type="password" name="password" value="">
<input type="submit" name="tj" value="提交"></input>
<input type="reset" name="reset"></input> </form>
</body>
</html>

【2程序Main.java】

package example.servlet;

import java.io.IOException;

import javax.servlet.RequestDispatcher;
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 Main
*/
@WebServlet("/Main")
public class Main extends HttpServlet {
private static final long serialVersionUID = 1L; protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String uerID=request.getParameter("userID");
if (uerID==null) uerID="";
String password=request.getParameter("password");
if(password==null) password="";
if(uerID.equals("guest")&&password.equals("guest")){
RequestDispatcher dispatcher=request.getRequestDispatcher("LoginSuccess");
dispatcher.forward(request, response);
}else {
RequestDispatcher dispatcher=request.getRequestDispatcher("LoginFail");
dispatcher.forward(request, response);
}
} protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
processRequest(request,response);
} protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
processRequest(request,response);
} public String getServletinfo(){
return "short description";
} }

【3程序LoginSuccess.java】

package example.servlet;

import java.io.IOException;
import java.io.PrintWriter; import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; @WebServlet("/LoginSuccess")
public class LoginSuccess extends HttpServlet {
private static final long serialVersionUID = 1L; protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html; charset=UTF-8");
PrintWriter out=response.getWriter();
String name=request.getParameter("userID"); out.println("<html>");
out.println("<head>");
out.println("<title>登录成功</title>");
out.println("</head>");
out.println("<body>");
out.println("<h1>欢迎"+name+"您已成功登录系统</h1>");
out.println("</body>");
out.println("</html>"); } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
processRequest(request,response);
} protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
processRequest(request,response);
} public String getServletInfo(){
return "short description";
} }

【4程序LoginFail.java】

package example.servlet;

import java.io.IOException;
import java.io.PrintWriter; import javax.servlet.RequestDispatcher;
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 LoginFail
*/
@WebServlet("/LoginFail")
public class LoginFail extends HttpServlet {
private static final long serialVersionUID = 1L; protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html; charset=UTF-8");
PrintWriter out=response.getWriter(); out.print("<html>");
out.print("<head>");
out.print("<title>登录失败</title>");
out.print("</head>");
out.print("<body>");
out.print("<h1>登录失败,请重新登录</h1>");
RequestDispatcher dispatcher=request.getRequestDispatcher("login.html");
dispatcher.include(request, response);
out.print("</body>");
out.print("<html>"); } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
processRequest(request,response);
} protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
processRequest(request,response);
} public String getServletInfo(){
return "short description";
} }

java web多组件协作实现用户登录验证的更多相关文章

  1. Java初学者作业——编写Java程序,实现用户登录验证。

    返回本章节 返回作业目录 需求说明: 编写Java程序,实现用户登录验证. 若用户名与密码输入正确,则提示"登录成功,欢迎回来!",若用户名与密码不匹配,则提示"用户名和 ...

  2. android loginDemo +WebService用户登录验证

        android loginDemo +WebService用户登录验证 本文是基于android4.0下的loginActivity Demo和android下的Webservice实现的.l ...

  3. djangorestframework-jwt自带的认证视图进行用户登录验证源代码学习

    Django REST framework JWT djangorestframework-jwt自带的认证视图进行用户登录验证源代码学习 SECRET_KEY = '1)q(f8jrz^edwtr2 ...

  4. Python程序练习1-模拟用户登录验证

    1.功能简介 此程序模拟用户登录验证的过程,实现用户名输入.黑名单检测.用户有效性判别.密码输入及验证等.用户在3次以内输入正确密码登陆成功,连续输错3次密码登陆失败,且该用户名被记录在黑名单,黑名单 ...

  5. cookie实现用户登录验证

    cookie实现用户登录验证 1, INSTALLED_APPS中注册app03 2,在主程序中新建映射关系到app3的url中 from django.conf.urls import url,in ...

  6. python3 用户登录验证的小功能

    用户登录验证,记录一下,还需要修改黑名单不合理 #!/usr/bin/env python3 ''' 需求:编写登录接口 1.输入用户名和密码 2.验证用户密码成功后输出欢迎消息 3.3次没有验证通过 ...

  7. 如何使用Django实现用户登录验证

    最初开始搞用户登录验证的时候感觉没什么难的,不就是增删改查中的查询数据库么,但是还是遇到许多小问题,而且感觉在查询数据库的时候,要把前端的数据一条一条的进行比对,会导致我的代码很丑,而且方式很不智,所 ...

  8. 用javascript实现简单的用户登录验证

    用javascript实现简单的用户登录验证 <!DOCTYPE html> <html lang="en"> <head> <meta ...

  9. django 从零开始 8 用户登录验证 待测

    看文档 djang 自带一个用户登录验证的方法,不过有些看着懵逼,去网上找了一圈,发现很多都是照抄文档说明的,几乎没说啥原理 特别是 from django.contrib.auth import a ...

随机推荐

  1. webgis开发-开始向JS转向

    官方的一个blog Final Release and Support Plan for the ArcGIS APIs / Viewers for Flex and Silverlight 网址: ...

  2. vs2010开发activex(MFC)控件/ie插件(一)

    原文:http://blog.csdn.net/yhhyhhyhhyhh/article/details/50782904  vs2010开发activex(MFC)控件:      第一步:生成ac ...

  3. 程序员装B指南(转载)

    转自:http://www.oschina.net/question/615783_115390 一.准备工作 "工欲善其事必先利其器." 1.电脑不一定要配置高,但是双屏是必须的 ...

  4. 使用SDL2出现 “error LNK2019: 无法解析的外部符号 _SDL_main,该符号在函数 _main 中被引用” 时的可能错误记录

    这几天在使用SDL2,之前一直都没有错,直到上午把项目搬了个地方.结果一直出现 “error LNK2019: 无法解析的外部符号 _SDL_main,该符号在函数 _main 中被引用” . 看了网 ...

  5. salt 之 master and minion

    系统:centos7.2 master:192.168.1.41minion:192.168.1.46 注释: setenforce 0 --关闭selinux systemctl stop fire ...

  6. linux crontab 的使用

    linux crontab 的使用 准备(实验楼需要,实际环境不需要):sudo service rsyslog startsudo cron -f & crontab 使用添加任务:cron ...

  7. 【Leetcode】【Medium】Gas Station

    There are N gas stations along a circular route, where the amount of gas at station i is gas[i]. You ...

  8. 为某个云服务设置RVIP

    获取所有Azure(云)服务Get-AzureService #查看某个云服务的LocationGet-AzureService -ServiceName tests |select location ...

  9. 应用线性代数简介 - 向量,矩阵和最小二乘法 By Stephen Boyd and Lieven Vandenberghe

    Introduction to Applied Linear Algebra – Vectors, Matrices, and Least Squares 应用线性代数简介 - 向量,矩阵和最小二乘法 ...

  10. C/C++ 合法整数与字符

    一.C语言中的合法整型 首先C语言中的整型有三种表示方式:十进制.八进制和十六进制.(C语言中没有表示二进制的整型) 十进制: 如 int a = 63; //一个正常的整型 八进制: 如果想用8进制 ...