JSP实现简单用户登录

来源:互联网 发布:淘宝卖家最迟发货时间 编辑:程序博客网 时间:2024/05/30 23:23

使用初级的JSP代码实现用户登录。使用TXT文件存储用户数据。

初学JSP与大家分享一些自己的代码。


index.jsp

<%@ page language="java" import="java.util.*" contentType="text/html; charset=utf-8"%><%String path = request.getContextPath();String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html>  <head>    <base href="<%=basePath%>">        <title>My JSP 'index.jsp' starting page</title><meta http-equiv="pragma" content="no-cache"><meta http-equiv="cache-control" content="no-cache"><meta http-equiv="expires" content="0">    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"><meta http-equiv="description" content="This is my page"><!--<link rel="stylesheet" type="text/css" href="styles.css">-->  </head>    <body>  <form action="deal.jsp" method="post" >                  <h1>用户登录</h1>          <hr>        <table>          <tr><td align="center" >用户名</td>          <td><input type="text" name="name" /></td></tr>          <tr><td align="center" >密码</td>          <td><input type="password" name="pwd" /></td></tr>          <tr><td></td>          <td><input type="submit" name="submit" value="登录" /></td>          </tr>        </table>  </form>  </body> </html>
deal.jsp

<%@page import="java.io.*"%><%@ page language="java" contentType="text/html; charset=utf-8" import="java.util.*"pageEncoding="utf-8"%><%boolean flag = false;boolean flag1 = false;String name = request.getParameter("name");String pwd = request.getParameter("pwd");response.setCharacterEncoding("utf-8");String path = "E:\\";File file = new File(path, "ee.txt");FileReader fr = new FileReader(file);BufferedReader br = new BufferedReader(fr);while (br.ready()) {String str = br.readLine();String[] userList = str.split(" ");if (userList[0].equals(name)) {if (userList[1].equals(pwd)) {flag = true;break;}}}if (flag) {session.setAttribute("username", name);response.sendRedirect("main.jsp");} else {response.sendRedirect("errorly.jsp");}br.close();%>

main.jsp

<%@ page language="java"  contentType="text/html; charset=utf-8" import="java.util.*" pageEncoding="utf-8"%><%String path = request.getContextPath();String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html>  <head>    <base href="<%=basePath%>">        <title>My JSP 'main.jsp' starting page</title>    <meta http-equiv="pragma" content="no-cache"><meta http-equiv="cache-control" content="no-cache"><meta http-equiv="expires" content="0">    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"><meta http-equiv="description" content="This is my page"><!--<link rel="stylesheet" type="text/css" href="styles.css">-->  </head>    <%  String username = (String)session.getAttribute("username");   %>       <head>        <meta content="text/html;charset=GBK" http-equiv="ContentType">        <title>系统主页</title>     </head>  <body>  您好![<%=username %>]欢迎访问!<br>  <a href="exit.jsp">[退出]</a>  </body></html>
errorly.jsp

<%@ page language="java" import="java.util.*" contentType="text/html; charset=utf-8"%><%String path = request.getContextPath();String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html>  <head>    <base href="<%=basePath%>">        <title>My JSP 'Error.jsp' starting page</title>    <meta http-equiv="pragma" content="no-cache"><meta http-equiv="cache-control" content="no-cache"><meta http-equiv="expires" content="0">    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"><meta http-equiv="description" content="This is my page"><!--<link rel="stylesheet" type="text/css" href="styles.css">-->  </head>    <body>  <h1>出错页</h1>  <hr>  用户名或密码错误!<br>  <a href="index.jsp">返回主页</a>  </body></html>
ee.txt

admin1 111
admin2 111
admin3 111
admin4 111

0 0
原创粉丝点击