JSP Learning Notes (iii)-----using JSP to process user registration and Login

Source: Internet
Author: User
Tags insert odbc sql string
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



Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.