一、实验要求
使用JSP内置对象实现简单登录页面
包含login1.jsp(登录页)、login2.jsp(返回信息页)、test.tag(验证页)
注意:本博客只包含最简单的功能页面,如需页面设计与debug,可私戳博主~
二、实验环境
JDK:13 Tomcat:9.0
三、实验过程
1. 建立文件目录结构
2. 编写 test.tag 文件
该页面主要功能为验证用户名、密码的正确性,从而返回相关结果给信息页面
<%@ attribute name="name" required="true" %> <%@ attribute name="shiyan" required="true" %> <%@ variable name-given="message" scope="AT_END" %> <% try{ int password=Integer.parseInt(shiyan); String string="none"; String adminer="user1"; if(name.equals(adminer)&&password==1234) { string="Success"; //或者其他信息 } else{ string="Fail"; } jspContext.setAttribute("message",string); } catch(Exception e){ jspContext.setAttribute("message","error"); //异常处理 } %>
3. 编写login1.jsp页面
该页面为最简单的登录功能页面,只需要简单的表单功能将其与 tag 文件进行数据传送即可
由于本人在进行页面设计时运用了大量 js 网页设计,所以在此将页面编码格式设置为ANSI,方便显示相关文字信息与效果展示
<%@ page contentType="text/html;charset=GB2312" %> <html> <head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0"/> <title>login</title> </head> <% session.invalidate(); %> <body> <form action="login2.jsp" method="post" name=form> <center> <table> <center>用户登录</center> <tr> <td> 请输入用户名:<p><Input type="text" name="name" size=20 placeholder="初始用户名user1"> </td></tr><br> <tr> <td> 请输入密码:<p><Input type="password" name="shiyan" size=20 placeholder="初始密码1234"> </td><br></tr> <tr> <td> <center> <button>登录</button> </center> </td></tr><br> </table> </form> </center> </body> </html>
由于在此想要展示更简单的登录页,所以删掉了大量的 js 修饰代码,只用<table>标签来显示效果
4. 编写login2.jsp页面
该页面为信息页,主要功能为将 tag 文件的判断结果反馈到该页面中
<%@ page contentType="text/html;charset=GB2312" %> <%@ taglib tagdir="/WEB-INF/tags" prefix="towards"%> <html> <head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0"/> <title>login</title> </head> <body> <center> <% String a=request.getParameter("name"); session.setAttribute("admin",a); String b=request.getParameter("shiyan"); session.setAttribute("password",b); if (a==null||b==null){ a=""; b=""; } if(a.length()>0&&b.length()>0){ %> <% if(a==null){ %> <jsp:forward page="error.jsp"/> //异常处理 <% } else{} %> <towards:test name="<%=a%>" shiyan="<%=b%>"/> <%=message%> <br> <% String T="Success"; if(message.equals(T)){ %> <%} else{ %> <A href="login1.jsp">登录失败,请返回登录界面</A> <%} } %> </font> </body> </html>
四、实验效果图(未加网页设计版)
写在最后
由于时间问题,没有把较为复杂的网页设计结构与代码编写博客~
偷懒只写了功能代码,希望能够帮助到刚开始接触 JSP 的小白,在实现功能的基础上可以继续优化页面,实现更多交互效果,也可以私信本人寻找相关网页设计资源噢~