js|筆記|使用者註冊
1. 這是一個JSP執行個體,由四個JSP頁面組成,處理使用者的註冊和登陸資訊2. 首先是login.jsp,代碼如下:<html>
<center>
<form method=get action="http://127.0.0.1:8000/jsp/test.jsp">
username<input type=text name=username>
<br><br>
password<input type=password name=pass>
<br><br>
<input type=submit value="註冊">
</form>
<form method=get action="http://127.0.0.1:8000/jsp/test3.jsp">
username<input type=text name=username>
<br><br>
password<input type=password name=pass>
<br><br>
<input type=submit value="登陸">
</form>
<br>
</center>
</html>
3. test.jsp代碼如下:(主要處理使用者註冊,將註冊資訊插入資料庫)
<html>
<%@ page language="java" import="java.sql.*"%>
your username is:<%=request.getParameter("username")%>
<br><br>
your password is:<%=request.getParameter("pass")%>
<br><br>
<%
String name=request.getParameter("username");
String pass=request.getParameter("pass");
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection dbcon=DriverManager.getConnection("jdbc:odbc:test","sa","");
PreparedStatement stat=dbcon.prepareStatement(
"insert login values(?,?)");
stat.setString(1,name);
stat.setString(2,pass);
stat.executeUpdate();
out.println("<br><br><B>insert successful</B>");
}
catch(Exception e)
{
out.println(e);
}
%>
<b>Click here to view all people</b>
<form method=get action="http://127.0.0.1:8000/jsp/test2.jsp">
<input type=submit value="--->">
</form>
</html>
4. test2.jsp,代碼如下:(處理當使用者註冊成功之後可以查看所有的註冊使用者名稱單)
<html>
<%@ page language="java" import="java.sql.*"%>
<% try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection dbcon=DriverManager.getConnection("jdbc:odbc:test","sa","");
PreparedStatement stat=dbcon.prepareStatement(
"select * from login");
ResultSet result=stat.executeQuery();
%>
<table border=2>
<%
while(result.next())
{
%>
<tr><td><%=result.getString(1)%><td></tr>
<%
}
%>
</table>
<%
}
catch(Exception e)
{
out.println(e);
}
%>
</html>
5. test3.jsp代碼如下:(處理使用者登入資訊)
<html>
your username is:<%=request.getParameter("username")%>
<br><br>
your password is:<%=request.getParameter("pass")%>
<br><br>
<%@ page language="java" import="java.sql.*"%>
<%
String name=request.getParameter("username");
String pass=request.getParameter("pass");
try
{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection dbcon=DriverManager.getConnection("jdbc:odbc:test","sa","");
PreparedStatement stat=dbcon.prepareStatement(
"select * from login where username=? and password=?");
stat.setString(1,name);
stat.setString(2,pass);
ResultSet result=stat.executeQuery();
if(result.next())
{
out.println("Login Successful");
}
else
{
out.println("Login Error");
}
}
catch(Exception e)
{
out.println(e);
}
%>
6.此外還需要一個名為login的資料庫,只需要兩個列username,password就可以了
先註冊,使用者名稱33,密碼33
註冊成功
察看註冊使用者
登陸成功