jsp实现登录注册(与数据库对接)

最近做了一些图像处理的内容,闲暇时间搞了下jsp,终于把至少两个月之前的代码的bug找出来了...

具体内容我在之前一篇博文有介绍,主要是增加了数据库的部分。其实一样处理,获得输入的用户名,密码,然后判断是否需要在当前页面用javascipt处理下(比如注册肯定是需要的,起码两次密码输的要一样),然后跳转逻辑页面,对接数据库,进行增删查改,最后跳转相应的页面.

有几点需要注意:1,myeclipse的导入jar的包与eclipse稍有不同.

                             2,javascript处理和跳转逻辑页面的处理的区别

之后有空再用javabean重写一遍.

login.jsp

<%@ page language="java" import="java.util.*" contentType="text/html;charset=utf-8"%>



用户登录



用户登录
用户名:
密码:
如果您还没有注册,请单击这里注册!

register.jsp

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



  
    
    
    注册页面
    
	
	
	    
	
	
	
  

 
  
  

用户注册

用户名:
输入密码:
再次确认密码:

check.jsp(判断用户登录)

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


  
    
    My JSP 'check.jsp' starting page
    
	
	    
	
	
	
 
  
<%
   request.setCharacterEncoding("utf-8");
   String users=request.getParameter("username");
   String pass=request.getParameter("pwd");
   boolean flag=false;
   PreparedStatement sql=null;  
   ResultSet rs=null;
   Connection conn=null;
%>

<% 
    String driver = "com.mysql.jdbc.Driver";  
    String url = "jdbc:mysql://127.0.0.1:3307/login";  
    String use = "root";   
    String password = "960404";  
    Class.forName(driver);  
    conn= DriverManager.getConnection(url,use,password);  
    sql =conn.prepareStatement("select * from student where username=? and password=?");
    sql.setString(1,users);
    sql.setString(2,pass);
    rs=sql.executeQuery();
    if (rs.next()) {  
    flag=true;
     }
   rs.close();
   sql.close();
   conn.close();
  %>

<% if (flag==true){ %>

<%} 
else
if (flag==false){
%>

<%} %>


check2.jsp(判断用户注册)

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



  
    
    
    检验注册页面
    
	
	    
	
	
	

  
  
  
    
<% request.setCharacterEncoding("utf-8"); String users=request.getParameter("username"); String pass=request.getParameter("pwd"); %> <% String driver = "com.mysql.jdbc.Driver"; String url = "jdbc:mysql://127.0.0.1:3307/login"; String use = "root"; String password = "960404"; Class.forName(driver); Connection conn= DriverManager.getConnection(url,use,password); PreparedStatement sql =conn.prepareStatement("insert into student(username,password)values(?,?)"); sql.setString(1,users); sql.setString(2,pass); int rtn=sql.executeUpdate(); sql.close(); conn.close(); %>
show.jsp(登录成功)

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



  
    
    登录成功
    
	
	    
	
	
	

  
  
  
   登录成功. 


login_fail.jsp(注意还是要跳转回原来的登录页面的)

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



  
    
    
    登录失败
    
	
	
	    
	
	
	

  
  
  
  <% out.println("登录失败");%> 
  <% response.setHeader("refresh","5;url=login.jsp");%>
  



你可能感兴趣的:(Java,数据库)