java web多组件协作实现用户登录验证
2018-06-18 00:43:45来源:未知 阅读 ()
实现步骤:
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"; } }
标签:
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有
- 国外程序员整理的Java资源大全(全部是干货) 2020-06-12
- 2020年深圳中国平安各部门Java中级面试真题合集(附答案) 2020-06-11
- 2020年java就业前景 2020-06-11
- 04.Java基础语法 2020-06-11
- Java--反射(框架设计的灵魂)案例 2020-06-11
IDC资讯: 主机资讯 注册资讯 托管资讯 vps资讯 网站建设
网站运营: 建站经验 策划盈利 搜索优化 网站推广 免费资源
网络编程: Asp.Net编程 Asp编程 Php编程 Xml编程 Access Mssql Mysql 其它
服务器技术: Web服务器 Ftp服务器 Mail服务器 Dns服务器 安全防护
软件技巧: 其它软件 Word Excel Powerpoint Ghost Vista QQ空间 QQ FlashGet 迅雷
网页制作: FrontPages Dreamweaver Javascript css photoshop fireworks Flash