纯Jsp式的登陆注册

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
String info = request.getParameter("info");
if(info == null){
	System.out.println("yao hehe");
}else if(info.equals("error")){
	out.println("<center><h1>用户名或密码错误</h1></center>");
}
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>My JSP 'login.jsp' starting page</title>
    
	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0">    
	<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
	<meta http-equiv="description" content="This is my page">
	<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->

  </head>
  
  <body topmargin="150">
  
  <center>
  
    <form name="form1" action="loginCheck.jsp" method="post">
    
    <table width="250" border="1">
    <tr>
    	<td colspan="2" align="center">登陆界面</td>
    </tr>
    
    <tr>
    	<td>用户名</td>
    	<td><input type="text" name="username" size="17"></td>
    </tr>
    
     <tr>
 		<td>密码</td>
 		<td><input type="password" name="password" size="18"></td>
    </tr>
    
    <tr>
 		<td colspan="2"><input type="submit" name="submit" value="登录"> <a href="register.jsp">注册新用户</a></td>
    </tr>
    </table>
    </form>
  </center>
  
 </body>
</html>


loginCheck.jsp

<%@ page language="java" import="java.util.*,java.sql.*;" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
String username = request.getParameter("username");
String password = request.getParameter("password");
//这就是在服务器端完成的,javascript也是可以完成这个功能的,不过就是在客户端完成的了,减轻了服务器的压力
if(username == null||password ==null){
	response.sendRedirect("login.jsp");
}
boolean flag = false;
Connection conn = null;
Statement st = null;
ResultSet rs = null;
String sql ="select * from users where username = '"+username+"' and password = '"+password+"' ";
try{
	Class.forName("com.mysql.jdbc.Driver");
	conn = DriverManager.getConnection("jdbc:mysql://localhost/login?user=root&password=admin");
	if(conn!=null){
		System.out.println("链接数据库成功");
	}
	st = conn.createStatement();
	
	rs = st.executeQuery(sql);
	
	if(rs.next()){
		flag = true;
	}
}catch(Exception e){
	e.printStackTrace();
}finally{
	if(rs!=null){
		rs.close();
	}
	
	if(st!=null){
		st.close();
	}
	
	if(conn !=null){
	   conn.close();
	}
}

if(flag){
	response.sendRedirect("welcome.jsp?user="+username);
}else{
	response.sendRedirect("login.jsp?info=error");
}
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>My JSP 'loginCheck.jsp' starting page</title>
    
	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0">    
	<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
	<meta http-equiv="description" content="This is my page">
	<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->

  </head>
  
  <body>
  
  </body>
</html>


welcome.jsp

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
String username = request.getParameter("user");
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>My JSP 'welcome.jsp' starting page</title>
    
	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0">    
	<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
	<meta http-equiv="description" content="This is my page">
	<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->

  </head>
  
  <body>
    欢迎登陆本网页!<%=username%><br>
    
  </body>
</html>


 

register.jsp

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>My JSP 'register.jsp' starting page</title>
    
	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0">    
	<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
	<meta http-equiv="description" content="This is my page">
	<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->

  </head>
  
  <body>
  <center>
    <form name="form1" action="registerCheck.jsp" method="post">
<table width="300" border="1">
	<tr>
		<td colspan="2" align="center">注册窗口</td>
	<tr>
		<td>用户名</td>
		<td><input type="text" name="username" size="25"></td>
	</tr>
	<tr>
		<td>密码</td>
		<td><input type="password" name="password1" size="26"></td>
	</tr>
	<tr>
		<td>确认密码</td>
		<td><input type="password" name="password2" size="26"></td>
	</tr>
	<tr>
		<td>Email</td>
		<td><input type="text" name="email" size="25"></td>
	</tr>
	<tr>
		<td colspan="2"><input type="submit" name="submit" value="注册"> <a
			href="login.jsp">返回</a></td>
	</tr>
</table>
</form>
</center>
  </body>
</html>


registerCheck.jsp

<%@ page language="java" import="java.util.*,java.sql.*;" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
String email = request.getParameter("email");
String username = request.getParameter("username");
String password1 = request.getParameter("password1");
String password2 = request.getParameter("password2");

if(username == null||password1 == null|| password2 == null||!password1.equals(password2)){
	response.sendRedirect("register.jsp");
}

boolean flag = false;
String sql = "select * from users where username ='"+username+"'";
Connection conn = null;
Statement st = null;
ResultSet rs = null;

try{
	Class.forName("com.mysql.jdbc.Driver");
	conn = DriverManager.getConnection("jdbc:mysql://localhost/login?user=root&password=admin");
	if(conn!=null){
		System.out.println("链接数据库成功");
	}
	st = conn.createStatement();
	
	rs = st.executeQuery(sql);
	
	if(!rs.next()){
		sql = "insert into users(username,password) values('"+username+"','"+password1+"')";
		//st.execute(sql);
		int res = st.executeUpdate(sql);
		if(1 == res){
			flag = true;
		}
		
	}
}catch(Exception e){
	e.printStackTrace();
}finally{
	if(rs!=null){
		rs.close();
	}
	
	if(st!=null){
		st.close();
	}
	
	if(conn !=null){
	   conn.close();
	}
}

if(flag){
	System.out.println("flag=true");
	response.sendRedirect("login.jsp");
}else{
	System.out.println("flag=false");
	response.sendRedirect("register.jsp");
}
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
    
    <title>My JSP 'registerCheck.jsp' starting page</title>
    
	<meta http-equiv="pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0">    
	<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
	<meta http-equiv="description" content="This is my page">
	<!--
	<link rel="stylesheet" type="text/css" href="styles.css">
	-->

  </head>
  
  <body>
    This is my JSP page. <br>
  </body>
</html>


 

 

  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
为了实现JSP验证登录注册页面,可以按照以下步骤进行操作: 1. 首先,将系统用户登录和注册页面改为JSP页面。根据引用中的要求,设计登录JSP页面和注册页面,并确保它们与之前的系统用户登录和注册页面相对应。 2. 在登录页面的表单中,可以使用request对象获取用户输入的信息。可以通过设置表单的name属性来命名表单字段,然后在JSP页面中使用request.getParameter()方法获取相应字段的值。 3. 建立新的JSP页面来完成登录验证。在该页面中,可以使用Java代码来验证用户输入的用户名和密码是否正确。可以使用引用提供的主要基于Servlet JSP JavaBean开发模实现JavaWeb用户登录注册功能的示例代码来参考实现。 4. 对注册页面也可以进行类似的处理,设计一个JSP页面来实现用户注册功能。同样,可以使用request对象获取用户输入的注册信息,并通过Java代码将用户信息存储到数据库或其他持久化方中。 通过以上步骤,你可以实现JSP验证登录注册页面,并根据你的具体需求进行相应的功能实现。<span class="em">1</span><span class="em">2</span> #### 引用[.reference_title] - *1* [使用JSP实现简单的用户登录注册页面示例代码解析](https://download.csdn.net/download/weixin_38608025/14043553)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] - *2* [JavaWeb实现用户登录注册功能实例代码(基于Servlet+JSP+JavaBean模)](https://download.csdn.net/download/weixin_38521831/13005631)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值