Servlet 實現登錄頁面,並在條件下跳轉


Java代碼 

package ServletDemo;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

public class DealServlet extends HttpServlet {
    @Override
    public void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }

    @Override
    public void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        PrintWriter out=resp.getWriter();
        resp.setContentType("text/html;charset =utf-8");//防止亂碼
        String username = req.getParameter("username");//獲取登錄名
        String passWd = req.getParameter("passwd");//獲取密碼
        System.out.println("登錄名" + username);
        System.out.println("密碼"+ passWd);
        if(username.equals("") || passWd.equals("")){//判斷密碼和用戶名是否為null
            String str = "You code is Error!!!";
//            req.setAttribute("Alert",str);
            //跳轉到密碼錯誤頁面
//          req.getRequestDispatcher("passwdError.jsp").forward(req,resp);
            System.out.println("執行到沒輸入密碼");
            out.println(str);
        } else{
            if(username.equals(passWd)){//判斷用戶名是否等於密碼
                String str = "welcome!!!";
//              req.getRequestDispatcher("welcome.jsp").forward(req,resp);
                out.println(str);
                System.out.println("執行到密碼等於用戶名");
            }
            else{
                String str = "code Error,back to welcome";
//              req.setAttribute("Alert",str);
//              req.getRequestDispatcher("index.jsp").forward(req,resp);
                out.println(str);
                System.out.println("執行到密碼錯誤");
            }
        }
    }
}

更新

package ServletDemo;
/**
 * 登錄功能
 * 獲取username和passwd
 * 將username和passwd封裝
 * 調用UserDao的login方法,獲取返回的User對象
 * 判斷username和passwd的正確行,並根據條件跳轉不同的頁面
 */

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

public class DealServlet extends HttpServlet {
    @Override
    public void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }
    @Override
    public void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        PrintWriter out=resp.getWriter();
        //設置服務器端以UTF-8編碼進行輸出
        resp.setCharacterEncoding("GB2312");
        //設置瀏覽器以UTF-8編碼進行接收,解決中文亂碼問題
        resp.setContentType("text/html;charset = GB2312");//防止亂碼
        String userName = req.getParameter("username");//獲取登錄名
        String passWd = req.getParameter("passwd");//獲取密碼
        System.out.println("登錄名" + userName);
        System.out.println("密碼"+ passWd);
        if(userName.equals("") || passWd.equals("")){//判斷密碼和用戶名是否為null
//            String str = "密碼錯誤!!!";
//            req.setAttribute("Alert",str);
            //跳轉到密碼錯誤頁面
//          req.getRequestDispatcher("passwdError.jsp").forward(req,resp);
            resp.sendRedirect("http://localhost:8080/javaWeb_war_exploded/ServletDemo/PassWdError");//跳轉頁面
//            http://localhost:8080/javaWeb_war_exploded/ServletDemo/DateServlet
//            http://localhost:8080/javaWeb_war_exploded/ServletDemo/PassWdError
//            System.out.println("執行到沒輸入密碼");//
//            out.println(str);
        } else{
            if(userName.equals(passWd)){//判斷用戶名是否等於密碼
//                String str = "welcome!!!";
//              req.getRequestDispatcher("welcome.jsp").forward(req,resp);
//                out.println(str);
                //resp.sendRedirect("");
                
                System.out.println("執行到密碼等於用戶名");
            }
            else{
                String str = "code Error,back to welcome";
//              req.setAttribute("Alert",str);
//              req.getRequestDispatcher("index.jsp").forward(req,resp);
//                out.println(str);
                resp.sendRedirect("http://localhost:8080/javaWeb_war_exploded/ServletDemo/PassWdError");//跳轉頁面
                System.out.println("執行到密碼錯誤");
            }
        }
    }
}

 

html文件 


<%@ page contentType="text/html;charset=utf-8" language="java" %>
<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>用戶登錄示例</title>
</head>
<body>
<div style="margin: auto; text-align: center;">
    <form action="ServletDemo/DealServlet" method="post">
        用戶名:<input type="text" name="username" />
        密 碼:<input type="password" name="passwd" />
                <input type="submit" value="登 錄" />
    </form>
</div>
</body>
</html>

 

 

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM