实验2.2 Servlet用户登录验证
实验内容:
编写JSP程序,实现用户提交登录表单给Servlet,由Servlet 查询数据库(模拟查询,不用真的连接数据库查询),对用户是否存在进行验证。
实验步骤:
(1)编写一个验证用户的Servlet(LoginServlet. java),再编写登录页面程序login.html、登录成功页面welcome.jsp和登录失败页面loginfail.jsp。
(2)运行程序查看效果。

代码结构:

登陆页面参考实验1
check.jsp
<%--
Created by IntelliJ IDEA.
User: GAVT
Date: 2021/11/11
Time: 上午9:33
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Check</title>
</head>
<body>
<%
String u=new String();
String p=new String();
java.util.Enumeration allPara= request.getParameterNames();
while(allPara.hasMoreElements()){
Object pname=allPara.nextElement(); //取得各个元素的name值
String[] rs; //通过元素的name值取得value
rs = request.getParameterValues((String) pname);
int size=java.lang.reflect.Array.getLength(rs);
if(pname.equals("u"))
{
for (int i=0;i<size;i++) {
u +=rs[i];
}
}
if(pname.equals("p"))
{
for (int i=0;i<size;i++) {
p +=rs[i];
}
break;
}
}
if(u.equals("gavt")&&p.equals("123"))
{
response.sendRedirect("welcome.jsp");
}
else
{
response.sendRedirect("loginfail.jsp");
}
%>
</body>
</html>
其余代码略
5471



被折叠的 条评论
为什么被折叠?



