jsp注册登录代码

login.jsp(a.jsp)
<form action="b.jsp" method="post">
    username:<input type="text" name="username"/>
    </br>
    </br>
    password:<input type="text" name="password" />
    </br>
    </br>
    <input type="submit" value="submit"/>
  </form>

dologin.jsp(b.jsp)
<%@page import="java.sql.*"%>  
<% 
String yname=request.getParameter("username"); 
String ypass=request.getParameter("password");
String sname="";
%>
<%
String RL = "jdbc:mysql://192.168.139.215:3306/zhuce?useUnicode=true&characterEncoding=utf-8&user=root&password=zouhuiying";
try {
	Class.forName("com.mysql.jdbc.Driver");
	ResultSet rs = null;
	String sqlpass="";
	String sqlname="";
	Connection con = DriverManager.getConnection(RL);
	Statement st = con.createStatement();
	String sql1="select * from users where name='"+yname+"'";
	rs = st.executeQuery(sql1);
    while (rs.next()) {
    	sqlname=(rs.getString("name"));//getInt
        sqlpass=(rs.getString("pass"));
   	}
   	if (sqlname.equals("")&&yname!=null){
    	out.println(yname);
    	String sqlStr="insert into users(name,pass) values('"+yname+"','"+ypass+"')";
    	PreparedStatement   pre = con.prepareStatement(sqlStr); 
    	pre.executeUpdate();
		}else if(sqlpass.equals(ypass)){
		   request.getSession().setAttribute("sname",sqlname);
		
		  
		  
			response.sendRedirect("friends.jsp");
		}else{
			response.sendRedirect("a.jsp");
		}
		st.close();
		rs.close();
		
	} catch (Exception e2) {
		out.println(e2);
		}
%>

friends.jsp
<%@page import="java.sql.*"%>  
<% String myname=""; %>
<% myname=(String)request.getSession().getAttribute("sname"); %>

<%	
String RL = "jdbc:mysql://192.168.139.215:3306/zhuce?useUnicode=true&characterEncoding=utf-8&user=root&password=zouhuiying";
		
Connection con=null;
		PreparedStatement st=null;
		
	
		ResultSet rs = null;
		String sql="select * from lianxi where lianxi.mname='"+myname+"'";
		String sqlpass="";
		String sqlname="";
		try {
			Class.forName("com.mysql.jdbc.Driver");
			con = DriverManager.getConnection(RL);
			st = con.prepareStatement(sql);
			
			rs = st.executeQuery();
		    while (rs.next()) {
		    	sqlname=(rs.getString("yname"));
		    	out.println(sqlname);//getInt
		    	out.println("</br>");
		        
			}
		   
			} catch (Exception e2) {
				out.println(e2);
			}finally {
				try {
					rs.close();
					con.close();
				    st.close();
				} catch (Exception e) {
					// TODO: handle exception
					e.printStackTrace();
				}

			}
%>
</br>
<input type="text" name="my" id="my"/>
<button onclick="location.href='c.jsp?sname='+document.getElementById('my').value">The query</button>


querry.jsp(c.jsp)
<%@page import="java.sql.*"%>  
<%@page import="java.util.*"%>


<!--% myname=(String)request.getSession().getAttribute("sname"); %-->
<%String myname=request.getParameter("sname");%>





<%	
String RL = "jdbc:mysql://192.168.139.215:3306/zhuce?useUnicode=true&characterEncoding=utf-8&user=root&password=zouhuiying";
		String newname="";
    	Connection con=null;
    	PreparedStatement st=null;	
    	ResultSet rs = null;
    	String sql="select name from users where name like '"+myname+"%'";
    	String sqlmname="";
    	String sqlyname="";
    	
 
    			try {
    				Class.forName("com.mysql.jdbc.Driver");
    				con = DriverManager.getConnection(RL);
    				st = con.prepareStatement(sql);
    				
    				rs = st.executeQuery();
    			    while (rs.next()) {
    			    	sqlmname=(rs.getString("name"));
    			 
    			    	out.println(sqlmname);
    			    	out.println("<button onclick="+"location.href='add.jsp?newname="+sqlmname+"'>add</button>");
    			    	out.println("</br>");
    			  
    		
    			    	
    				}
    			   
    				} catch (Exception e2) {
    					System.out.println(e2);
    				}finally {
    					try {
    						rs.close();
    						con.close();
    					    st.close();
    					} catch (Exception e) {
    						// TODO: handle exception
    						e.printStackTrace();
    					}

    				}
%>


add.jsp

<%@page import="java.sql.*"%>  
<%String newname=request.getParameter("newname");%>
<% String myname=(String)request.getSession().getAttribute("sname"); %>
<%out.println(newname);%>
<%out.println(myname);%>

<% 
String yname=request.getParameter("username"); 
String ypass=request.getParameter("password");
String sname="";
Connection con=null;
Statement st = null;
PreparedStatement pre=null;
%>
<%
String RL = "jdbc:mysql://192.168.139.215:3306/zhuce?useUnicode=true&characterEncoding=utf-8&user=root&password=zouhuiying";
try {
	Class.forName("com.mysql.jdbc.Driver");
	//ResultSet rs = null;
	String sqlpass="";
	String sqlname="";
	con = DriverManager.getConnection(RL);
	st = con.createStatement();
	//String sql1="select * from users where name='"+yname+"'";
	String sql="insert into lianxi(mname,yname) values('"+myname+"','"+newname+"')";
    pre = con.prepareStatement(sql); 
    pre.executeUpdate();
    response.sendRedirect("friends.jsp");
	

  
	} catch (Exception e2) {
				out.println(e2);
			}finally {
				try {
					
					con.close();
				    st.close();
				    pre.close();
				} catch (Exception e) {
					// TODO: handle exception
					e.printStackTrace();
				}

			}
%>


猜你喜欢

转载自zouhuiying.iteye.com/blog/2277625