日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

javaweb重定向——登录页面跳转到首页

發(fā)布時間:2023/12/18 编程问答 25 豆豆
生活随笔 收集整理的這篇文章主要介紹了 javaweb重定向——登录页面跳转到首页 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

文章目錄

        • 一、項目結(jié)構(gòu)
        • 二、login.html
        • 三、ServletLogin.java
        • 四、loginError.jsp
        • 五、home.html
        • 六、部署和運行
          • 1、測試用例一(用戶名錯誤)
          • 2、測試用例二(密碼錯誤)
          • 3、測試用例二(賬號信息正確)

一、項目結(jié)構(gòu)

login.html用戶信息錯誤,進行請求轉(zhuǎn)發(fā),跳轉(zhuǎn)到loginError.html。
用戶信息正確,進行重定向,跳轉(zhuǎn)到home.html。

二、login.html

<!DOCTYPE html> <html lang="en"> <head><meta charset="UTF-8"><title>用戶登錄</title> </head> <body><h3>用戶登錄</h3><form action="login" method="get"><p>用戶名<input type="text" name="username"></p><p>密 碼<input type="text" name="password"></p><input type="submit" value="登錄"></form> </body> </html>

三、ServletLogin.java

package net.test.servlet;import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException;@WebServlet(name = "ServletLogin",urlPatterns = "/login") public class ServletLogin extends HttpServlet {protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {}protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {//用戶列表String user="user123";String pass="pass123";//獲取客戶端傳來的參數(shù)String username = request.getParameter("username");String password = request.getParameter("password");if(!user.equals(username)){/*用戶名不存在*///request攜帶數(shù)據(jù)到web資源request.setAttribute("errorMessage","用戶名不存在");//請求轉(zhuǎn)發(fā)request.getRequestDispatcher("/loginError.jsp").forward(request,response);}else if(!pass.equals(password)){/*密碼錯誤*///request攜帶數(shù)據(jù)到web資源request.setAttribute("errorMessage","密碼錯誤");//請求轉(zhuǎn)發(fā)request.getRequestDispatcher("/loginError.jsp").forward(request,response);}else{//必須加項目地址,這里加helloresponse.sendRedirect("/hello/home.html");}} }

四、loginError.jsp

<%--Created by IntelliJ IDEA.User: AdministratorDate: 2019/2/1Time: 1:20To change this template use File | Settings | File Templates. --%> <%@ page contentType="text/html;charset=UTF-8" language="java" %> <html> <head><title>用戶登錄錯誤</title> </head> <body><h3>用戶登錄錯誤界面</h3><%=request.getAttribute("errorMessage")%> </body> </html>

五、home.html

<!DOCTYPE html> <html lang="en"> <head><meta charset="UTF-8"><title>首頁</title> </head> <body><h3>網(wǎng)站首頁</h3> </body> </html>

六、部署和運行

部署項目

訪問login.html

1、測試用例一(用戶名錯誤)


跳轉(zhuǎn)到用戶登錄錯誤頁面,提示“用戶名不存在”

2、測試用例二(密碼錯誤)


跳轉(zhuǎn)到用戶登錄錯誤頁面,提示“密碼錯誤”

3、測試用例二(賬號信息正確)


跳轉(zhuǎn)到網(wǎng)站首頁

總結(jié)

以上是生活随笔為你收集整理的javaweb重定向——登录页面跳转到首页的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。