jsp注册页面代码
网上抄下来的,想用在自己的项目里面,改动了不少,越来越感觉程序写的复杂,视图和控制,还有数据库混在一起,错误处理代码写的很烂.....
用户信息的bean: UserBean.java
package chen;
public class UserBean
{
private String userid;
private String password;
public void setUserId(String userid)
{
this.userid=userid;
}
public void setPassword(String password)
{
this.password=password;
}
public String getUserId()
{
return this.userid;
}
public String getPassword()
{
return this.password;
}
}
提交数据库的bean:UserRegister.java
package chen;
import com.mysql.jdbc.Driver;
import java.sql.*;
public class UserRegister
{
private UserBean userBean;
private Connection con;
//获得数据库连接。
public UserRegister()
{
String url="jdbc:mysql://localhost/"+"chao"+"?user="+"root"+"&password="+"850629";
try
{
Class.forName("com.mysql.jdbc.Driver").newInstance();
con = DriverManager.getConnection(url);
}
catch(Exception e)
{
e.printStackTrace();
}
}
//设置待注册的用户信息。
public void setUserBean(UserBean userBean)
{
this.userBean=userBean;
}
//进行注册
public void regist() throws Exception
{
String reg="insert into userinfo(userid,password) values(?,?)";
try
{
PreparedStatement pstmt=con.prepareStatement(reg);
pstmt.setString(1,userBean.getUserId());
pstmt.setString(2,userBean.getPassword());
pstmt.executeUpdate();
}
catch(Exception e)
{
e.printStackTrace();
throw e;
}
}
}
提交注册数据进入数据库:adduser.jsp
<%@ page contentType="text/html;charset=gb2312" pageEncoding="gb2312"
import="chen.*" %>
<jsp:useBean id="userBean" class="chen.UserBean" scope="request">
<jsp:setProperty name="userBean" property="*"/>
</jsp:useBean>
<jsp:useBean id="regist" class="chen.UserRegister" scope="request"/>
<html>
<head>
<title> 用户信息注册页面</title>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
</head>
<body>
<%
String userid =request.getParameter("userid");
String password = request.getParameter("password");
userBean.setUserId(userid);
userBean.setPassword(password);
System.out.println(userid+password);
%>
<% try{
regist.setUserBean(userBean);
out.println(userid);
regist.regist();
out.println("注册成功");}
catch(Exception e){
out.println(e.getMessage());
}
%>
<br>
<a href="login.jsp">返回</a>
</body>
</html>
登陆验证页面:checklogin.jsp
<%@page import="java.sql.*" contentType="text/html;charset=GB2312" %>
<%@page import="java.util.*"%>
<%
String userid1=new String(request.getParameter("userid"));
String password1=new String(request.getParameter("password"));
Class.forName("com.mysql.jdbc.Driver");
Connection con=DriverManager.getConnection("jdbc:mysql://localhost:3306/chao","root","850629");
Statement stmt=con.createStatement();
String sql="select * from userinfo where userid='"+userid1+"';";
ResultSet rs=stmt.executeQuery(sql);
if(rs.next())
{String password=new String(rs.getString("password"));
if(password.equals(password1))
{session.setAttribute("userid1",userid1);
response.sendRedirect("sucess.jsp");
}
else
{response.sendRedirect("login.jsp");
}
}
else
{response.sendRedirect("login.jsp");
}
%>
登陆页面:login.jsp
<%@ page contentType="text/html; charset=gb2312" %>
<html>
<body>
<form method="get" action="checklogin.jsp">
<table>
<tr><td> 输入用户名:</td>
<td><input type=text name=userid ></td>
</tr>
<tr><td>输入密码:</td>
<td><input type=password name=password></td>
</tr>
<tr><td><input type=submit value=确认>
</td></tr>
</table>
</form>
<form action="register.jsp">
<input type=submit value=注册>
</form>
</body>
</html>
注册页面:register.jsp
<%@page contentType="text/html; charset=gb2312" language="java" import="java.util.*,java.io.*"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
</head>
<body>
<center>
<h1>注册新用户</h1>
<form action="adduser.jsp" method=post>
<table border="1" bgcolor="#0099CC">
<tr>
<td> 用户名:
<input type="text" name="userid">
</td>
</tr>
<tr valign="middle">
<td> 密码:
<input type="password" name="password" readonly>
</td>
</tr>
<tr>
<td>
<input type=submit value=提交>
</td>
</tr>
</table>
</form>
</center>
</body>
</html>
登陆成功页面:sucess.jsp
<%@page import="java.util.*" contentType="text/html; charset=gb2312" %>
<%@include file="trans.jsp"%>
<html>
<head>
<title>
sucess
</title>
</head>
<body bgcolor="#ffffff">
<h1>
登录成功,欢迎您!
</h1><%=trans(session.getAttribute("userid1"))%>
</body>
</html>
分享到:
相关推荐
是一个用JSP开发的网页人事管理系统,数据库使用的是SQL,内有SQL文件,导入即可,代码只需在Conn.java处改写你的数据库密码即可运行,源代码导入时,会有错误文件,当你打开数据库,又改动了密码,就不必理会,直接...
/periodcal.jsp 上传文件后台处理页面 / prototype.js prototype框架类库文件 第21章(/ch21) 程序描述:本章将实现通过滑块来显示分页数据的功能。传统的分页方式,多数是通过单击按钮的方式来显示...
This article introduces Struts, a Model-View-Controller implementation that uses servlets and JavaServer Pages (JSP) technology. Struts can help you control change in your Web project and promote ...
本人在使用分页的时候嫌其麻烦,就自己改写了后为一个框架,暂时只支持对单表分页,该分页通过对表的相关反射,使用该分页只需要保证javaBean中的属性和表中的属性对应一致(不区分大小写)然后在页面上传上相关参数就...
在程序设计领域,人们把能够在无需改写有关代码的前提下,让开发出来的应用程序能够支持多种语言和数据格式的技术称为国际化技术。在计算机文档里,国际化(internationalization)通常简写为i18n,这是因为这个单词...
解题思路1 : 重载Connection类 由于extjs和server端交互全都是 json格式的数据交互,server端不会控制页面的跳转,... 因此重载Connection类,并改写其中的 handleResponse 方法,判断返回的结果是否是 403.jsp,logi
这是一个经过改写的struts1.2 使用方便,可以在某种程度上提高开发效率 具体使用方法 1.建普通项目,并加入相应的STRUTS包 稍微改下build属性 加入struts1.2框架,用默认的包就可以了 2.加入caiban-struts.jar,这个...
1.Jv语言的前景 诞生于1995年的Jv语言以20世纪末和21世纪初XX络科技和XX络经济所特有的令人瞠目结舌的速度迅速进展,有人预言,未来90%的程序代码将用Jv语言书写或改写,Jv的跨平台特性、安全机制、可靠性、健壮性...