JSP learning notes (3) ----- Using JSP to handle user registration and landing

xiaoxiao2021-03-06  87

1. This is a JSP instance, consisting of four JSP pages, handles users' registration and login information

2. The first is login.jsp, the code is as follows:

Username



Password



Username



Password




3. Test.jsp code is as follows: (Mainly handled user registration, insert registration information into the database)

<% @ page language = "java" import = "java.sql. *"%>

Your username is: <% = request.getParameter ("UserName")%>



Your Password Is: <% = Request.getParameter ("pass")%>



<%

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 ( "

insert successful ");

}

Catch (Exception E)

{

Out.println (e);

}

%>

Click Here to View All People

4. Test2.jsp, the code is as follows: (Processing You can view all registered user list after the user is successfully registered)

<% @ 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 ();

%>

<%

While (result.next ())

{

%>

<% = Result.getstring (1)%>

<%

}

%>

<%

}

Catch (Exception E)

{

Out.println (e);

}

%>

5. Test3.jsp code is as follows: (handle user login information)

Your username is: <% = request.getParameter ("UserName")%>



Your Password Is: <% = Request.getParameter ("pass")%>



<% @ 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. Also need a database named login, only two columns Username, Password can

First registration, username 33, password 33

registration success

View registered users

Landed successfully

转载请注明原文地址:https://www.9cbs.com/read-122288.html

New Post(0)
CopyRight © 2020 All Rights Reserved
Processed: 0.044, SQL: 9