js| Notes | User Registration
1. This is a JSP instance, consists of four JSP pages, processing user registration and login Information 2. The first is login.jsp, the code is as follows:<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 code is as follows: (Main processing user registration, insert registration information into the database)
<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, the code is as follows: (processing when the user registration is successful, you can view all registered users list)
<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. The test3.jsp code is as follows: (processing user login information)
<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. In addition, you need a database called login, which requires only two columns Username,password.
Register first, username 33, password 33
Registration successful
Viewing registered Users
Landing success