jsp基础--登录注册建立

搞了一个周,总算成功了,现分享给大家,请多指教!

所需软件:

myeclipse  mysql数据库,   Navicat for MySQL(sqlyog可视化软件可替换Navicat for MySQL)  Tomact7.x

①:首先创建一个WebService project,我命名是zuoye2

如图:


②:将驱动(我用的是mysql-connector-java-5.0.8-bin)复制到创建的WebService project中WebRoot--WEB-INF-lib目录下,如图:


③:新建包xxxx.model,,,,,,新建类,如user(在user类中写需要的方法,并同时写set与get方法)

代码如下:

<span style="font-size:18px;">package zuoye2.model;

public class user {
	private int username;
	private String password;
	private String code;
	public int getUsername()
	{
		return username;
	}
	public void setUsername(int username)
	{
		this.username = username;
	}
	public String getPassword()
	{
		return password;
	}
	public void setPassword(String password)
	{
		this.password = password;
	}
	public String getCode()
	{
		return code;
	}
	public void setCode()
	{
		this.code = code;
	}
}</span>

④:新建包名 xxxx.util,,,,,新建类 如DBO(用于连接数据库)

代码如下:

package zuoye2.util;

import java.sql.Connection;
import java.sql.DriverManager;

import com.mysql.jdbc.PreparedStatement;
import com.mysql.jdbc.ResultSet;
import com.mysql.jdbc.Statement;

public class DBO {
	protected final static  String driver = "com.mysql.jdbc.Driver";
	protected final static  String url =  "jdbc:mysql://localhost:3306/建立数据库的名称" ;
	static {
		
		try{
			Class.forName(driver);//加载驱动程序
		}catch(Exception e)
		{
			e.printStackTrace();
		}
	}
	public static Connection getConnection ()
	{
		Connection conn = null;
		try{
			String password;
			conn = (Connection)DriverManager.getConnection(url, “数据库用户名”,“数据库密码”);
			
		}catch(Exception e)
		{
			e.printStackTrace();
		}
		return conn;
	}
	public static void close (Statement st,ResultSet rs,Connection conn)
	{
		try
		
		{
			if(st!=null)
			{
				st.close();
			}
			if(rs!=null)
			{
				rs.close();
			}
			if(conn!=null)
			{
				conn.close();
			}
		}catch(Exception  e)
		{
			e.printStackTrace();
		}
	}
	public static void close(PreparedStatement pst,Connection conn)
	{
		try{
			if(pst!=null)
			{
				pst.close();
			}
			if(conn!=null)
			{
				conn.close();
			}
		}catch(Exception  e)
		{
			e.printStackTrace();
		}
	}

}
⑤:新建包名 xxxx.service(用于写操作接口与实现接口的方法),,,,新建接口,如userManager (注意此处建的是接口Interface不是class),右击service包,点开新建,找到Interface,点击建立,命名userManager,代码如下:

package zuoye2.service;

import zuoye2.model.user;


public interface userManger {
	public boolean add(user u);
	public boolean addm(int username,String password);

}
⑥:在 xxxx.service包中新建类,如userManagerImpl(此类用于实现userManager接口中定义的方法)。

代码如下:

package zuoye2.service;

import java.sql.Connection;

import com.mysql.jdbc.PreparedStatement;
import com.mysql.jdbc.ResultSet;
import com.mysql.jdbc.Statement;
import com.sun.tools.xjc.reader.xmlschema.bindinfo.BIConversion.User;

import zuoye2.model.user;
import zuoye2.util.DBO;


public  class userMangerImpl implements userManger{
	public boolean add(user u){
		boolean flag = false;
		Connection conn = null;
		PreparedStatement pst = null;
		try{
			conn = DBO.getConnection();
			String sql = "insert into a (username,password,code) value(1,22,333)";
		     pst=(PreparedStatement )conn.prepareStatement(sql);
		     
		     pst.setInt(1, u.getUsername());
		     pst.setString(2,u.getPassword());
		     pst.setString(3, u.getCode());
		     
		     int row = pst.executeUpdate();
		     if(row>0)
		     {
		    	 flag = true;
		     }
		   }
		     catch(Exception e)
		     {
		    	 e.printStackTrace();
		     }finally{
				DBO.close(pst,conn);
		     }
		return false;
		}
	public boolean addm (int username,String password)
	{
		boolean flag = false;
		Connection conn = null;
		Statement st = null;
		ResultSet rs = null;
		
		try{
			conn = DBO.getConnection();
			String sql = "select * from a where username="+username;
			st = (Statement) conn.createStatement();
			rs = (ResultSet) st.executeQuery(sql);
			
			while(rs.next())
			{
				if(rs.getString("password").equals(password));
				{
					flag = true;
				}
			}
		}catch(Exception e)
		{
			e.printStackTrace();
		}finally{
			DBO.close(st,rs,conn);
		}
		return false;
	}
	
}
	
⑦:建立JSP页面,右点击WebRoot,新建JSP(Advaanced  Templates) (PS :软件在新建工程时默认第一个JSP页面是index.jsp,),注意:在JSP页面中,标红出,确定是utf-8,若不是一定修改过来。

登录页面代码:

<%@ 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 'index.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>
    <from action="addm" method="post" align="center">
    <center><h1>网页登陆</h1></center>
    <center><b>用户名:</b><input type ="password" name="Username" style="width: 193px; "><br>
     <p></p>
     <b>密  码:</b><input type="password" name="Password" style="width: 193px; "><br>
     <br/>

       <font size="+1">
       <p>
       <input name="submit"  type="submit" class="button"  style="width:200px; height:35px " value="登录" />
             </p>
      
      <p> 
       <font  size="-1"  color="#000099"><a href="zhuce.jsp">注册</a> </font> </p>
   </font>
   </center>
    </from>
  </body>
</html>


注册页面代码:


<%@ 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 'zhuce.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>
    <h1 align="center">新用户注册</h1>
    <hr/>
    <from action="add method="post" ><center>
                 <b>用户名:</b><input name="Username" type="text" style="width: 193px"><br/>
	    <p></p>
	        <b>密  码:</b><input name="Password" type="password" style="width: 193px; "/><br/>
	    <p></p>
	         <b>验证码:</b><input name="Code" type="text" style="width:193px;"><br/>
	    <p></p>
  	<input type="submit" style="width:180px; height:35px "value="注册" target="0"/> <br/>
    </form>
    <br/>
    	<a href="index.jsp">返回 </a>
    	</center>
  </body>
</html>

⑧:在JSP页面中,可在<body></body>中写form表单

<form action="xxxx"method="post" >,可参考步骤⑦中代码,

附xxx是对应的后台程序的servlet,名字要相同,否则表单数据无法提交到servlet中。

⑨:新建包xxx.servlet,,,,,新建与操作相对应的servlet如添加add,addm,
注意此处新建的文件名字是 servlet 不是class,servlet用于接收from表单中传来的数据。

建立方法:右点击包xxx.servlet,新建目录下,点击Servlet,可建立两个,一个命名add,一个命名addm,

add代码:

package zuoye2.servlet;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import zuoye2.model.user;
import zuoye2.service.userManger;
import zuoye2.service.userMangerImpl;



public class add extends HttpServlet {


	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		doPost(request, response);
		}


	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		
		request.setCharacterEncoding("utf-8");
		response.setCharacterEncoding("utf-8");

		int username = Integer.parseInt(request.getParameter("Username"));
		String pswd = request.getParameter("Password");
		String code = request.getParameter("Code");
		
		userManger um =new  userMangerImpl();
		user u = new  user();
		
		u.setUsername(username);
		u.setPassword(pswd);
		u.setCode();
		
		boolean flag= false;
		
		try {
			flag = um.add(u);
			if(flag==true){
				response.sendRedirect("index.jsp");
			}
			
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
		
	}
}

addm代码:

package zuoye2.servlet;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import zuoye2.model.user;
import zuoye2.service.userMangerImpl;


public class addm extends HttpServlet {


	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		
		request.setCharacterEncoding("utf-8");
		response.setCharacterEncoding("utf-8");
		
		int un = Integer.parseInt(request.getParameter("Username"));
		String pswd = request.getParameter("Password");
        String checkcode = request.getParameter("Checkcode");
		
		String pickcode = (String) request.getSession().getAttribute("piccode");
		
		userMangerImpl um = new userMangerImpl();
		 user u = new user();
		 boolean flag= false;
		 try {
			 flag = um.addm(un, pswd);
			
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
		 if(flag&&checkcode.equals(pickcode)){
			 response.sendRedirect("logchegngong.jsp");
		 }
		
		
	}

}
⑩:到此所有代码结束,接下来是运行,右点击创建的 WebService project,在此目录下找到Run  As--MyEclipse Server Application,会弹出这个出口


选择Tomcat7.x,点击OK,运行!


另外,数据库连接代码,及测试类代码,博客空间里有,在这里就不多陈述了



最后,给大家分享一点体会,做这个一定要有耐心,也要足够的细心,我在写的时候,好几次导入包的时候,都导错了,造成很大麻烦,还好有人指导,

你可能感兴趣的:(jsp基础--登录注册建立)