eclipse编写简单注册页面(JSP+MySQL)

eclipse编写简单注册页面(JSP+MySQL)

数据库页面
eclipse编写简单注册页面(JSP+MySQL)_第1张图片
项目构成
eclipse编写简单注册页面(JSP+MySQL)_第2张图片

register.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>注册</title>
</head>
<body>
<center>
<h1>注册操作</h1>
<hr>
<form action="register_check.jsp" method="post">
<table border="1">
<tr>
<br>
<td colspan="2" align="center"><font size="4"><b>用户注册</b></font></td>
</tr>
<tr>
<td align="right">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;名:</td>
<td><input type="text" name="name1" size="20"></td>
</tr>
<tr>
<td align="right">&nbsp;&nbsp;&nbsp;&nbsp;名:</td>
<td><input type="text" name="username1" size="20" maxlength="11"></td>
</tr>
<tr>
<td align="right">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;码:</td>
<td><input type="password" name="passwd1" size="21"></td>
</tr>
<tr>
<td align="right">确认密码:</td>
<td><input type="password" name="passwd2" size="21"></td>
</tr>
<tr>
<td colspan="2" align="center">
<input type="reset" value="重置">
<input type="submit" value="注册">
</table>
</form>
</center>
</body>
</html>

注册页面
eclipse编写简单注册页面(JSP+MySQL)_第3张图片

register_check.jsp

<%@page import="java.sql.DriverManager"%>
<%@page import="java.sql.ResultSet"%>
<%@page import="java.sql.PreparedStatement"%>
<%@page import="java.sql.Connection"%>
<%@page import="java.sql.*" %>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ page import="javax.sql.*" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>注册验证</title>
</head>
<body>
<%
request.setCharacterEncoding("utf-8");
%>
<%!
public static final String DBDRIVER="com.mysql.jdbc.Driver";
public static final String DBURL="jdbc:mysql://localhost:3306/mldn";
public static final String DBUSER="root";
public static final String DBPASS="123456";
%>
<%
Connection conn=null;
PreparedStatement pstmt=null;
ResultSet rs=null;
boolean flag=false;
%>
<%
try{
	
	Class.forName(DBDRIVER);
	String username=request.getParameter("username1");
	String passwd=request.getParameter("passwd1");
	String passwd1=request.getParameter("passwd2");
	String name=request.getParameter("name1");
	if(username==null || username=="" || passwd==null || passwd=="" || name==null || name==""){
		flag=false;
	}
	else if(passwd.equals(passwd1)){
		conn=DriverManager.getConnection(DBURL,DBUSER,DBPASS);
		String sql="insert into user01(userid,password,name) values (?,?,?)";
		pstmt=conn.prepareStatement(sql);
		pstmt.setString(1,username);
		pstmt.setString(2,passwd);
		pstmt.setString(3, name);
		int rtn=pstmt.executeUpdate();
		flag=true;
	}
	else{
		flag=false;
	}
}catch(Exception e){
	System.out.println(e);
}
finally{
	try{
		rs.close();
		pstmt.close();
		conn.close();
	}catch(Exception e){}
}
%>
<%
if(flag){
%>
<jsp:forward page="register_success.jsp"/>
<%
}else{
%>
<jsp:forward page="register_failure.jsp"/>
<%
}
%>
</body>
</html>

register_success.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>注册成功</title>
</head>
<body>
<center>
<h1>注册成功</h1>
<h2>3秒后将返回登录页面,你也可以点击<a href="register.jsp">这里</a>跳转</h2>
<%
response.setHeader("refresh", "3;URL=login.jsp");
%>
</center>
</body>
</html>

注册失败页面
eclipse编写简单注册页面(JSP+MySQL)_第4张图片

register_failure.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>注册失败</title>
</head>
<body>
<center>
<h1>注册失败</h1>
<h2>3秒后将返回注册页面,你也可以点击<a href="register.jsp">这里</a>跳转</h2>
<%
response.setHeader("refresh", "3;URL=register.jsp");
%>
</center>
</body>
</html>

注册成功页面
eclipse编写简单注册页面(JSP+MySQL)_第5张图片

你可能感兴趣的:(javaweb,eclipse,java-ee)