MyBlog部分模块---基于Servlet+JSP+JavaBean实现登陆注册验证功能

 代码:

 1.前端部分

 top.jsp(网页头部,内含弹出式登陆窗口,注册窗口)

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
    <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
 



个人博客网站--用户登录

content.jsp(博客发布的内容栏)

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>




成功页面



  
专注

想要做成一件事,除了勤奋,还需要专注与热爱,三者缺一不可。这样才有可能会成功! 2016-08-07

专注

你愿与我一路同行,我将为你背负行囊!---加油 2015~ 2015-02-19

专注

发现一路走来,不经意间已经慢慢的失去了一些东西。 2014-10-25

专注

在这世界上,不管你做得在怎么好,总会有那么50%的人,不会认同你的做法。只有无视他们的存在,才会走得更远。 2014-07-18

专注

有时候缺的就是破釜沉舟的勇气,把自己逼一逼,才知道自己有多大潜力! 2014-05-27

专注

是男人就得对自己狠点,为事业而奋斗。30而立,给自己十年时间。成功与否十年后见!!!加油 2014-05-09


bottom.jsp(网页底部)

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
 
  • 烟台大学 计算机与控制工程学院 海哥 制作 电话:1786281023x 电子邮箱邮箱:[email protected]
  • Copyright © http://blog.csdn.net/linhaiyun_ytdx All Rights Reserved!

Main.jsp(首页)

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>




博客首页



  
<%@ include file="top.jsp" %> <%@ include file="content.jsp" %> <%@ include file="bottom.jsp" %>


LoSuss.jsp(登陆成功页面)

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
    <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
 



<%@ include file="content.jsp" %> <%@ include file="bottom.jsp" %>

LoSuss.jsp(登陆失败页面)

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>




登陆失败



   
<%@ include file="top.jsp" %>

登陆失败,请仔细检查登陆信息!

<%@ include file="bottom.jsp" %>

Register.jsp(注册页面)

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>		
	
	



  

ReSuss.jsp(注册成功页面)

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8" import="com.bean.User,com.dao.CustomerDao,com.dao.CustomerDaoer"%>
<%
 String username = request.getParameter("user");
 CustomerDao dao=new CustomerDaoer();
 User u=new User();
 u.setUsername(username);
%>




注册成功页面



   
<%@ include file="top.jsp" %>
恭喜<%=u.getUsername() %>注册成功!
<%@ include file="bottom.jsp" %>

Refail.jsp(注册失败页面)

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>




注册失败



   
<%@ include file="top.jsp" %>

注册失败,请仔细检查注册信息是否完整!

<%@ include file="bottom.jsp" %>

Style.css

body{
	margin:0px; 			
	padding:0px; 		
	font-size: 9pt; 		
}
#box{
	margin:0 auto auto auto;		
	width:800px;					
	clear:both;					
	background-color: #FFFFFF;	
}

a:hover {
	font-size: 9pt;
	color: #FF4400;
}
a {
	font-size: 9pt;
	text-decoration: none;	
	color: #3C404D;
}

wghFunction.js

//判定用户名
function checkeUser(user){
	var str=user;
	 //在JavaScript中,正则表达式只能使用首字母开头
	var Expression=/^[A-Z][a-z]{3,15}$/; 
	var objExp=new RegExp(Expression);		//创建正则表达式对象
	if(objExp.test(str)==true){				//通过正则表达式验证
		return true;
	}else{
		return false;
	}
}
//判断密码
function checkePwd(user){
	var str=user;
	var Expression=/^[A-Za-z]{1}([A-Za-z0-9]|[._]){8,30}$/; 
	var objExp=new RegExp(Expression);		//创建正则表达式对象
	if(objExp.test(str)==true){				//通过正则表达式验证
		return true;
	}else{
		return false;
	}
}

//验证E-mail地址
function checkemail(email){
	var str=email;
	var Expression=/\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*/; 
	var objExp=new RegExp(Expression);		//创建正则表达式对象
	if(objExp.test(str)==true){				//通过正则表达式进行验证
		return true;
	}else{
		return false;
	}
}

2.后端部分

User.java

package com.bean;
public class User {
	private int id = 0;// 用户ID
	private String username = "";// 用户名
	private String pwd = "";// 密码
	private String email = "";// E-mail地址
	private String question = "";// 密码提示问题
	private String answer = "";// 密码提示问题答案
	private String city = "";// 所在地

	public String getEmail() {
		return email;
	}

	public void setEmail(String email) {
		this.email = email;
	}

	public String getQuestion() {
		return question;
	}

	public void setQuestion(String question) {
		this.question = question;
	}

	public String getAnswer() {
		return answer;
	}

	public void setAnswer(String answer) {
		this.answer = answer;
	}

	public String getCity() {
		return city;
	}

	public void setCity(String city) {
		this.city = city;
	}

	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public String getPwd() {
		return pwd;
	}

	public void setPwd(String pwd) {
		this.pwd = pwd;
	}

}

CustomDao.java(接口)

package com.dao;
import com.bean.*;
import com.connection.*;
public interface CustomerDao {
  public void add(User u);//添加用户
}

CustomDaoer.java

package com.dao;

import java.sql.SQLException;

import com.bean.User;
import com.connection.DButil;
import com.mysql.jdbc.Connection;
import com.mysql.jdbc.PreparedStatement;

public class CustomerDaoer implements CustomerDao {

	@Override
	public void add(User u) {
		// TODO Auto-generated method stub
		 Connection conn=DButil.open();  
        	 String sql="insert into user(username,pwd,email,city,question,answer) values(?,?,?,?,?,?)"; 
             try {
    			PreparedStatement pst=(PreparedStatement) conn.prepareStatement(sql);
    			pst.setString(1, u.getUsername());
    			pst.setString(2, u.getPwd());
    			pst.setString(3, u.getEmail());
    			pst.setString(4, u.getCity());
    			pst.setString(5, u.getQuestion());
    			pst.setString(6, u.getAnswer());
    			pst.executeUpdate(); 
         } catch (SQLException e) {  
             // TODO Auto-generated catch block  
             e.printStackTrace();  
         }      
         finally{  
             DButil.close(conn);  
         }  
	}

}

ConnectLogin.java

package com.dao;

import java.sql.ResultSet;
import java.sql.SQLException;

import com.connection.DButil;
import com.mysql.jdbc.Connection;
import com.mysql.jdbc.PreparedStatement;

public class ConnectLogin {
	public int login(String username,String pwd)  
    {  
        int ID=0;  
        Connection conn=DButil.open();  
        PreparedStatement pstmt=null;  
        ResultSet rs=null;  
          
        String sql="select id from user where username=? and pwd=?";  
        try {  
            pstmt=(PreparedStatement) conn.prepareStatement(sql);  
            pstmt.setString(1,username);  
            pstmt.setString(2,pwd);  
            rs=pstmt.executeQuery();  
            if(rs.next())  
            {  
                ID=rs.getInt("id");  
            }  
        } catch (SQLException e) {  
            // TODO Auto-generated catch block  
            e.printStackTrace();  
        }  
          
        return ID;  
    }  
}

Register.java

package com.servlet;

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

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.bean.User;
import com.dao.CustomerDao;
import com.dao.CustomerDaoer;
public class Register extends HttpServlet {
	private static final long serialVersionUID = 1L;
    public Register() {
        super();
        // TODO Auto-generated constructor stub
    }
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		  doPost(request, response); 
	}
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		request.setCharacterEncoding("utf-8");    
        response.setCharacterEncoding("utf-8");    
        response.setHeader("Content-Type","text/html; charset=utf-8");    
		String username = request.getParameter("user"); // 获取用户名
		String pwd = request.getParameter("pwd"); // 获取密码
		String email = request.getParameter("email"); // 获取E-mail地址
		String city = request.getParameter("city"); // 获取地址
		String question = request.getParameter("question"); // 获取密码提示问题
		String answer = request.getParameter("answer"); // 获取密码提示问题答案
		PrintWriter out = response.getWriter();
		if(username!=""&&pwd!=""&&email!=""&&city!=""&&question!=""&&answer!="")
		{
		 CustomerDao dao=new CustomerDaoer();
		 User u=new User();
		 u.setUsername(username);
		 u.setPwd(pwd);
		 u.setEmail(email);
		 u.setCity(city);
		 u.setQuestion(question); 
		 u.setAnswer(answer);
		 dao.add(u);
		 RequestDispatcher view=request.getRequestDispatcher("ReSuss.jsp");
		 view.forward(request, response);
		}
		else{
			RequestDispatcher view=request.getRequestDispatcher("ReFail.jsp");
			 view.forward(request, response);
		}
	}

}

login.java

package com.servlet;

import com.dao.*;
import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class login extends HttpServlet {
	private static final long serialVersionUID = 1L;
    public login() {
        super();
        // TODO Auto-generated constructor stub
    }
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		 doPost(request, response);  
	}
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		  request.setCharacterEncoding("utf-8");    
	        response.setCharacterEncoding("utf-8");    
	        response.setHeader("Content-Type","text/html; charset=utf-8");     
	          
	        String username=request.getParameter("username");    
	        String password=request.getParameter("pwd");      
	        PrintWriter out=response.getWriter();  
	        ConnectLogin dao=new ConnectLogin();
	        int ID=dao.login(username, password);  
	        if(ID!=0)  
	        {  
	        	RequestDispatcher view=request.getRequestDispatcher("LoSuss.jsp");
				view.forward(request, response); 
	        }  
	        else  
	        {  
	        	RequestDispatcher view=request.getRequestDispatcher("LoFail.jsp");
			    view.forward(request, response);
	        }  
	}

}

测试截图:

MyBlog部分模块---基于Servlet+JSP+JavaBean实现登陆注册验证功能_第1张图片




MyBlog部分模块---基于Servlet+JSP+JavaBean实现登陆注册验证功能_第2张图片


MyBlog部分模块---基于Servlet+JSP+JavaBean实现登陆注册验证功能_第3张图片


MyBlog部分模块---基于Servlet+JSP+JavaBean实现登陆注册验证功能_第4张图片


MyBlog部分模块---基于Servlet+JSP+JavaBean实现登陆注册验证功能_第5张图片


MyBlog部分模块---基于Servlet+JSP+JavaBean实现登陆注册验证功能_第6张图片


MyBlog部分模块---基于Servlet+JSP+JavaBean实现登陆注册验证功能_第7张图片


MyBlog部分模块---基于Servlet+JSP+JavaBean实现登陆注册验证功能_第8张图片


MyBlog部分模块---基于Servlet+JSP+JavaBean实现登陆注册验证功能_第9张图片


MyBlog部分模块---基于Servlet+JSP+JavaBean实现登陆注册验证功能_第10张图片


MyBlog部分模块---基于Servlet+JSP+JavaBean实现登陆注册验证功能_第11张图片


你可能感兴趣的:(JSP,学习笔记,servlet,javabean,jsp,数据库,前端)