JSP学习笔记(三)-----使用JSP处理用户注册和登陆

80酷酷网    80kuku.com

  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


注册成功


察看注册用户


登陆成功



分享到
  • 微信分享
  • 新浪微博
  • QQ好友
  • QQ空间
点击: