评论

收藏

[JSP] jsp登录页面的简单实例 雏形

开发技术 开发技术 发布于:2021-10-21 10:05 | 阅读数:562 | 评论:0

jsp登录页面的简单实例 雏形
<%@ page language="java" contentType="text/html; charset=UTF-8"
  pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "https://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>欢迎页面</title>
</head>
<body>
<%
session.invalidate();//销毁session
%>
<a href="denglu.jsp" >亲,请登录</a>
<a href="zhuce.jsp">注册</a>
</body>
</html>
欢迎界面:
DSC0000.jpg

尚未注册,直接登录的时候:
<%@ page language="java" contentType="text/html; charset=UTF-8"
  pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "https://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>登录</title>
</head>
<body>
<form action="test_denglu.jsp" method="post"> 
<table width="300" height="300" border="0" align="center">
  <tr height="80" align="center">
  <td colspan="2"><font size="24">OO登录</font></td></tr>
  <tr height="30">
  <td width="80" align="right">用户名:</td>       
  <td><input type="text" name="username"></td></tr>
  <tr height="30">
  <td width="80" align="right"> 密  码:</td>
  <td> <input type="password" name="password"></td></tr>
  <tr><td height="40" align="center" colspan="2">
  <input type="submit" value="登录">
  </td></tr>
 </table>
</form>
</body>
</html>
DSC0001.jpg

进入注册界面:
<%@ page language="java" contentType="text/html; charset=UTF-8"
  pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "https://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>注册</title>
</head>
<body>
<form action="test_zhuce.jsp" method="post"> 
<table width="300" height="300" border="0" align="center">
  <tr height="80" align="center">
  <td colspan="2"><font size="24">OO注册</font></td></tr>
  <tr height="30">
  <td width="80" align="right">用户名:</td>       
  <td><input type="text" name="username"></td></tr>
  <tr height="30">
  <td width="80" align="right"> 密  码:</td>
  <td> <input type="password" name="password"></td></tr>
  <tr><td height="40" align="center" colspan="2">
  <input type="submit" value="提交">
  </td></tr>
 </table>
</form>
</body>
</html>
DSC0002.jpg

注册成功,跳转登录界面:
<%@ page language="java" contentType="text/html; charset=UTF-8"
  pageEncoding="UTF-8" import="java.sql.*"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "https://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>检测跳转中</title>
</head>
<body>
<%
String name = new String(request.getParameter("username").getBytes("ISO-8859-1"),"UTF-8");
String pw = request.getParameter("password"); 
String str = "select * from USERS WHERE username = '" + name + "'";
String str1 = "insert into users values(xuhao.nextval,'" + name + "','" + pw +"')";
try{
  
   //连接数据库
   Connection conn = null;
   Class.forName("oracle.jdbc.driver.OracleDriver");
   String strURL = "jdbc:oracle:thin:@localhost:1521:SP";
   conn = DriverManager.getConnection(strURL, "test", "123");
   System.out.println("数据库连接成功");
   Statement st = conn.createStatement();
   ResultSet rs = st.executeQuery(str);
   if(rs.next()){
   if(name.equals(rs.getString(2))){
     out.println("对不起,用户名已使用。");
     out.println("<br>建议使用用户名:" + name +"99"); 
     response.setHeader("refresh", "5;URL=zhuce.jsp");
   }
   }else{
   int a = st.executeUpdate(str1);
   if(a == 1){
     out.println("恭喜你,注册成功");
     session.setAttribute("username", name);
     response.setHeader("refresh", "3;URL=denglu.jsp");
   }
   }
   
   rs.close();
   st.close();
   conn.close();
}catch(Exception e){
  e.printStackTrace();
}
%>
<br><br>
</body>
</html>
DSC0003.jpg

登录时,密码出错:5秒后重新登录
DSC0004.jpg                                   DSC0005.jpg

账号密码输入正确,进入主页面:
<%@ page language="java" contentType="text/html; charset=UTF-8"
  pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "https://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>主页面</title>
</head>
<body>
主页面
<%
Object obj = session.getAttribute("username");
if(obj != null){
  String str = obj.toString();
  out.println(str + "登陆成功");
}else{
  out.println("登录超时,请重新登录"); 
  response.setHeader("refresh", "5;URL=denglu.jsp");
}
%>
<br>
<a href="yemian.jsp">退出账号</a>
</body>
</html>
DSC0006.jpg

以上就是小编为大家带来的jsp登录页面的简单实例 雏形全部内容了,希望大家多多支持CodeAE代码之家~

关注下面的标签,发现更多相似文章