你的位置:首页 > Java教程

[Java教程]jsp登录页面 雏形


 1 <%@ page language="java" contentType="text/html; charset=UTF-8" 2   pageEncoding="UTF-8"%> 3 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 4 <html> 5 <head> 6 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 7 <title>欢迎页面</title> 8 </head> 9 <body>10 <%11 session.invalidate();//销毁session12 13 %>14 15 <a href="denglu.jsp" >亲,请登录</a>16 <a href="zhuce.jsp">注册</a>17 </body>18 </html>

欢迎界面:

尚未注册,直接登录的时候:

 1 <%@ page language="java" contentType="text/html; charset=UTF-8" 2   pageEncoding="UTF-8"%> 3 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 4 <html> 5 <head> 6 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 7 <title>登录</title> 8 </head> 9 <body>10 <form action="test_denglu.jsp" method="post"> 11 <table width="300" height="300" border="0" align="center">12  <tr height="80" align="center">13    <td colspan="2"><font size="24">OO登录</font></td></tr>14  <tr height="30">15    <td width="80" align="right">用户名:</td>             16    <td><input type="text" name="username"></td></tr>17  <tr height="30">18    <td width="80" align="right"> 密&nbsp;&nbsp;码:</td>19    <td> <input type="password" name="password"></td></tr>20    <tr><td height="40" align="center" colspan="2">21    <input type="submit" value="登录">22    </td></tr>23 </table>24 </form>25 </body>26 </html>

进入注册界面:

 1 <%@ page language="java" contentType="text/html; charset=UTF-8" 2   pageEncoding="UTF-8"%> 3 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 4 <html> 5 <head> 6 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 7 <title>注册</title> 8 </head> 9 <body>10 <form action="test_zhuce.jsp" method="post"> 11 <table width="300" height="300" border="0" align="center">12  <tr height="80" align="center">13    <td colspan="2"><font size="24">OO注册</font></td></tr>14  <tr height="30">15    <td width="80" align="right">用户名:</td>             16    <td><input type="text" name="username"></td></tr>17  <tr height="30">18    <td width="80" align="right"> 密&nbsp;&nbsp;码:</td>19    <td> <input type="password" name="password"></td></tr>20    <tr><td height="40" align="center" colspan="2">21    <input type="submit" value="提交">22    </td></tr>23 </table>24 </form>25 </body>26 </html>

注册成功,跳转登录界面:

 1 <%@ page language="java" contentType="text/html; charset=UTF-8" 2   pageEncoding="UTF-8" import="java.sql.*"%> 3 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 4 <html> 5 <head> 6 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 7 <title>检测跳转中</title> 8 </head> 9 <body>10 <%11 String name = new String(request.getParameter("username").getBytes("ISO-8859-1"),"UTF-8");12 String pw = request.getParameter("password"); 13 14 String str = "select * from USERS WHERE username = '" + name + "'";15 String str1 = "insert into users values(xuhao.nextval,'" + name + "','" + pw +"')";16 17 try{18   19   //连接数据库20    Connection conn = null;21    Class.forName("oracle.jdbc.driver.OracleDriver");22   String strURL = "jdbc:oracle:thin:@localhost:1521:SP";23    conn = DriverManager.getConnection(strURL, "test", "123");24    System.out.println("数据库连接成功");25    Statement st = conn.createStatement();26    ResultSet rs = st.executeQuery(str);27   if(rs.next()){28     if(name.equals(rs.getString(2))){29        out.println("对不起,用户名已使用。");30        out.println("<br>建议使用用户名:" + name +"99"); 31        response.setHeader("refresh", "5;URL=zhuce.jsp");32      }33    }else{34     int a = st.executeUpdate(str1);35     if(a == 1){36        out.println("恭喜你,注册成功");37        session.setAttribute("username", name);38        response.setHeader("refresh", "3;URL=denglu.jsp");39      }40    }41   42    rs.close();43    st.close();44    conn.close();45 }catch(Exception e){46   e.printStackTrace();47 }48 %>49 <br><br>50 52 </body>53 </html>

登录时,密码出错:5秒后重新登录

                                 

账号密码输入正确,进入主页面:

 1 <%@ page language="java" contentType="text/html; charset=UTF-8" 2   pageEncoding="UTF-8"%> 3 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 4 <html> 5 <head> 6 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 7 <title>主页面</title> 8 </head> 9 <body>10 主页面11 <%12 Object obj = session.getAttribute("username");13 if(obj != null){14   String str = obj.toString();15   out.println(str + "登陆成功");16 }else{17   out.println("登录超时,请重新登录"); 18   response.setHeader("refresh", "5;URL=denglu.jsp");19 }20 %>21 <br>22 23 <a href="yemian.jsp">退出账号</a>24 25 </body>26 </html>