一个登陆

<%--
  Created by IntelliJ IDEA.
  User: Administrator
  Date: 2018/7/10
  Time: 15:26
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>$Title$</title>
</head>
<body>
<form action="doLogin.jsp" name="loginForm" method="get">  
    <table>    
        <tr>      
            <td>用户名</td>      
            <td><input type="text" name="username"/></td>    
        </tr>    
        <tr>      
            <td>密码</td>      
            <td><input type="password" name="password"/></td>    
        </tr>    
        <tr>      
            <td colspan="2"><input type="submit" value="登录"/></td>    
        </tr>  
    </table>
</form>
</body>
</html>

doLogin.jsp中主要做业务逻辑的处理,所以不需要那些html标签

<%--
  Created by IntelliJ IDEA.
  User: Administrator
  Date: 2018/7/10
  Time: 15:51
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%
    String username="";
    String password="";

    username=request.getParameter("username");
    password=request.getParameter("password");
    if("admin".equals(username)&&"admin".equals(password)){
        request.getRequestDispatcher("loginSuccess.jsp").forward(request,response);
    }else {
        response.sendRedirect("loginFail.jsp");
    }
%>

这个逻辑就这样,然后需要写两个jsp。一个是登陆成功的一个是失败的页面即可。

在下一个界面中获取登陆的用户名

保存用户名,放到session中,到下一个页面中取出来。下一个界面中不直接写session.getAttribute()方法是为了防止空指针异常。

猜你喜欢

转载自www.cnblogs.com/volvane/p/9289455.html