jsp 登录与注册

login.jsp

<%@ page language="java" contentType="text/html; charset=utf-8"
	pageEncoding="utf-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>登录</title>
<style type="text/css">
form {
     
	width: 440px;
	background-color:aqua;
	text-align: center;
	
}
input {
     
	border: solid 1px #93FEFF;
}
</style>
</head>
<body>
	<form action="index1.jsp" method="post">
		<span><h1>用户登录<h1></span>
		<span>用户名:</span><input type="text" name="username" placeholder="请输入用户名" /><br/> 
		<span style="position: relative;left: 3.3%"><span>密码:</span><input type="password"name="password" placeholder="请输入密码" /></span><br/>
		<span style="position: relative;left: -10%"><input type="submit"value="登录" /></span>
		<span style="position: relative;left: 3.3%"><a href="login2.jsp"><input type="button" value="注册" /></a></span>
	</form>
</body>
</html>

login2.jsp

<%@ page language="java" contentType="text/html; charset=utf-8"
	pageEncoding="utf-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>注册</title>
<style type="text/css">
form {
     
	width: 440px;
	background-color:aqua;
	text-align: center;
	
}
input {
     
	border: solid 1px #93FEFF;
}
</style>
</head>
<body>
	<form action="index2.jsp" method="post">
		<span><h1>用户注册<h1></span>
		<span>用户名:</span><input type="text" name="username" placeholder="请输入用户名" /><br/> 
		<span style="position: relative;left: 3.3%"><span>密码:</span><input type="password"name="password" placeholder="请输入密码" /></span><br/>
		<input type="submit" value="注册" />
	</form>
</body>
</html>

index1.jsp

<%@ page language="java" import="java.sql.*"
	contentType="text/html; charset=utf-8" pageEncoding="utf-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
</head>
<body>
	<%
	String url = "jdbc:oracle:thin:localhost:1521:orcl";
	String user = "scott";
	String password = "tiger";
	Connection conn = null;
	PreparedStatement psmt = null;
	ResultSet rs = null;
	try {
     
		String sql = "select USERNAME,PASSWORD from T_USRE where USERNAME=? and PASSWORD=?";
		//加载驱动
		Class.forName("oracle.jdbc.driver.OracleDriver");
		//创建数据库连接
		conn = DriverManager.getConnection(url, user, password);
		//创建PreparedStatement对象
		psmt = conn.prepareStatement(sql);
		//字符编码
		request.setCharacterEncoding("utf-8");
		//用户名
		String name=request.getParameter("username");
		//用户密码
		String password1=request.getParameter("password");
		psmt.setString(1,name);
		psmt.setString(2,password1);
		rs = psmt.executeQuery();
		if(rs.next()){
     //有值
			out.print("

"+"欢迎"+name+"

"
); out.print("

"+"登录成功!"+"

"
); }else{ out.print("

"+"登录失败!"+"

"
);%> <a href="login2.jsp">去注册</a> <%} } catch (ClassNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } finally { try { if (rs != null) { rs.close(); rs = null; } if (psmt != null) { psmt.close(); psmt = null; } if (conn != null) { conn.close(); conn = null; } } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } %> </body> </html>

index2.jsp

<%@ page language="java" import="java.sql.*"
	contentType="text/html; charset=utf-8" pageEncoding="utf-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title></title>
</head>
<body>
	<%
	String url = "jdbc:oracle:thin:localhost:1521:orcl";//地址
	String user = "scott";//oracle 用户
	String password = "tiger";//oracle 密码
	Connection conn = null;
	PreparedStatement psmt = null;
	ResultSet rs = null;
	try {
     
		String sql = "insert into t_usre values(?,?)";
		//加载驱动
		Class.forName("oracle.jdbc.driver.OracleDriver");
		//创建数据库的连接
		conn = DriverManager.getConnection(url, user, password);
		//创建PreparedStatement对象
		psmt = conn.prepareStatement(sql);
		//设置字符编码
		request.setCharacterEncoding("utf-8");
		//获得用户名
		String name=request.getParameter("username");
		//获得密码
		String password1=request.getParameter("password");
		//传参
		psmt.setString(1,name);
		psmt.setString(2,password1);
		//填充结果集
		rs = psmt.executeQuery();
		if(rs.next()){
     
			out.print("

"+"注册成功!"+"

"
); } } catch (ClassNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } finally { try { if (rs != null) { rs.close(); rs = null; } if (psmt != null) { psmt.close(); psmt = null; } if (conn != null) { conn.close(); conn = null; } } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } %> </body> </html>

效果图:
jsp 登录与注册_第1张图片
jsp 登录与注册_第2张图片

你可能感兴趣的:(作品,java,jsp)