java web多組件協作實現使用者登入驗證

來源:互聯網
上載者:User

標籤:prot   html4   writer   IV   doc   null   eth   tran   html 4.01   

實現步驟:

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多組件協作實現使用者登入驗證

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.