javaweb重定向——登录页面跳转到首页
生活随笔
收集整理的這篇文章主要介紹了
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)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: AppStore算法再次升级,运营如何在
- 下一篇: 练习4闭合导线平差计算