`
x125521853
  • 浏览: 72678 次
  • 性别: Icon_minigender_1
  • 来自: 武汉
社区版块
存档分类
最新评论

登录和注册(jsp+servlet+JavaBean)

阅读更多

一:示例

   //model层(Student.java)  

package com.model;

public class Student {
	private int id;
	private String userName;
	private String userPwd;

	public Student(int id, String userName, String userPwd) {
		super();
		this.id = id;
		this.userName = userName;
		this.userPwd = userPwd;
	}
	
	public Student(String userName, String userPwd) {
		super();
		this.userName = userName;
		this.userPwd = userPwd;
	}

	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 getUserPwd() {
		return userPwd;
	}

	public void setUserPwd(String userPwd) {
		this.userPwd = userPwd;
	}
}

  //dao层(DbManager.java)

  用数据源连接数据库,注意要将数据库驱动包添加到Tomcat ,lib目录下。  

package com.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.sql.DataSource;

public class DbManager {
	public static Connection getConnection(){
		Connection conn = null;		
		try {
			Context context = new InitialContext();
			DataSource ds = (DataSource) context.lookup("java:comp/env/jdbc/test");
			conn = ds.getConnection();
		} catch (NamingException e) {
			e.printStackTrace();
		} catch (SQLException e) {
			e.printStackTrace();
		}		
		return conn;		
	}
	
	public static void closeConnection(Connection conn, PreparedStatement pst,ResultSet rs){
		try{
			if(rs !=null){
				rs.close();
			}
			if(pst !=null){
				pst.close();
			}
			if(conn !=null){
				conn.close();
			}
		}catch(SQLException e){
			e.printStackTrace();
		}
	}
}

 

  //StudentDao.java

package com.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import com.model.Student;

public class StudentDao {
	public List<Student> findName(Student student){
		Connection conn = null;
		PreparedStatement pst = null;
		ResultSet rs = null;
		
		List<Student> list = new ArrayList<Student>();		
		try {
			conn = DbManager.getConnection();
			conn.setAutoCommit(false);
			pst = conn.prepareStatement("select * from student where userName=? and userPwd=?");
			pst.setString(1, student.getUserName());
			pst.setString(2, student.getUserPwd());
			rs = pst.executeQuery();
			while (rs.next()){
				list.add(new Student(rs.getString(1),rs.getString(2)));
			}
			conn.commit();
		} catch (SQLException e) {
			try {
				conn.rollback();
			} catch (SQLException e1) {
				e1.printStackTrace();
			}
			e.printStackTrace();
		}finally{
			DbManager.closeConnection(conn, pst, rs);
		}
		return list;		
	}
	
	public int create(Student student){
		Connection conn = null;
		PreparedStatement pst = null;
		int state = 0;
		
		try {
			conn = DbManager.getConnection();
			conn.setAutoCommit(false);
			pst = conn.prepareStatement("insert into student values(?,?)");
			pst.setString(1, student.getUserName());
			pst.setString(2, student.getUserPwd());
			state = pst.executeUpdate();
			conn.commit();
		}catch (SQLException e) {
			try {
				conn.rollback();
			} catch (SQLException e1) {
				e1.printStackTrace();
			}
			e.printStackTrace();
		} finally {
			DbManager.closeConnection(conn, pst, null);
		}
		return state;
	}
}

  //服务层service(StudentService.java) 

package com.service;

import java.util.List;
import com.model.Student;

public interface StudentService {
	public List<Student> findName(Student student);
	public int create(Student student);
}

  //实现层(StudentServiceImpl.java)  

package com.service.impl;

import java.util.List;
import com.dao.StudentDao;
import com.model.Student;
import com.service.StudentService;

public class StudentServiceImpl implements StudentService {
	private StudentDao studentDao;

	public StudentServiceImpl() {
		studentDao = new StudentDao();
	}

	public List<Student> findName(Student student) {
		return studentDao.findName(student);
	}

	public int create(Student student) {
		return studentDao.create(student);
	}
}

   //action层

  //StringUtil.java 

package com.action;

public class StringUtil {
	public static boolean isBlank(String str) {
		return str == null || str.trim().length() == 0;
	}
}

  //StudentServlet.java

package com.action;

import java.io.IOException;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.model.Student;
import com.service.StudentService;
import com.service.impl.StudentServiceImpl;

public class StudentServlet extends HttpServlet {
	private StudentService studentService;

	public StudentServlet() {
		studentService = new StudentServiceImpl();
	}

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

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		response.setContentType("text/html");
		request.setCharacterEncoding("utf-8");
		response.setCharacterEncoding("utf-8");
		String userName = request.getParameter("userName");
		String userPwd = request.getParameter("userPwd");
		System.out.println(userName);
		System.out.println(userPwd);
		
		if(StringUtil.isBlank(userName)&&(StringUtil.isBlank(userPwd))){
			request.setAttribute("error","姓名和密碼錯誤!");
			request.getSession().setAttribute("login", "/login");
			request.getRequestDispatcher("/login").forward(request, response);
			return;
		}
		
		List<Student> list = studentService.findName(new Student(userName,userPwd));
		if(list.size()>0){
			request.getSession().setAttribute("login", list.get(0));
			request.getRequestDispatcher("/chapter1.jsp").forward(request, response);
		}else{
			request.setAttribute("error", "账号和密码错误!");
			request.getSession().setAttribute("login2", "login2");
			request.getRequestDispatcher("/login").forward(request, response);
			return;
		}
	}
}

  //RigesterServlet.java 

package com.action;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.model.Student;
import com.service.StudentService;
import com.service.impl.StudentServiceImpl;

public class RigesterServlet extends HttpServlet {
	private StudentService studentService;

	public RigesterServlet() {
		studentService = new StudentServiceImpl();
	}
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		doPost(request, response);
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {		
		response.setContentType("text/html");
		request.setCharacterEncoding("utf-8");
		response.setCharacterEncoding("utf-8");
		
		String userName = request.getParameter("userName");
		String userPwd = request.getParameter("userPwd");
		String pwd2 = request.getParameter("pwd2");
		
		request.setAttribute("userName", userName);
		request.setAttribute("userPwd", userPwd);
		request.setAttribute("pwd2", pwd2);
		
		if(StringUtil.isBlank(userName)){
			request.setAttribute("userName", userName);
			request.getRequestDispatcher("/register").forward(request, response);
			return;
		}
		if(StringUtil.isBlank(userPwd)){
			request.setAttribute("userPwd", userPwd);
			request.getRequestDispatcher("/register").forward(request, response);
			return;
		}
		if(StringUtil.isBlank(pwd2)){
			request.setAttribute("pwd2", pwd2);
			request.getRequestDispatcher("/register").forward(request, response);
			return;
		}
		
		Student student = new Student(userName,userPwd);
		if(studentService.create(student)>0){
			request.getRequestDispatcher("/chapter2.jsp").forward(request, response);
		}
	}
}

  //context.xml 数据源配置文件 

<Context>
<Resource name="jdbc/test" auth="Container"
	type="javax.sql.DataSource" maxActive="100" maxIdle="30"
	maxWait="10000" username="sa" password="admin"
	driverClassName="com.microsoft.sqlserver.jdbc.SQLServerDriver"
	url="jdbc:sqlserver://localhost:1433;DatabaseName=test" />
</Context>

 
   //login.jsp   

<%@ page language="java" pageEncoding="utf-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<c:set var="bathPath" value="${pageContext.request.contextPath}"/>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
  </head>
  <body>
  	<div>
  	<form action="${bathPath}/StudentServlet" method="post">
  		<div>
  			<label>用户名:</label>
  			<label><input type="text" name="userName" /></label><br />
  			<label>密&nbsp;&nbsp;碼:</label>
  			<label><input type="password" name="userPwd" /></label>  			
  		</div>
  		<div>
  			<label><input type="submit" value="登錄" /></label>
  			<label><input type="button" value="註冊" onclick="window.location='${bathPath}/register'"/></label>
  		</div>
  	</form>
  	</div>
  </body>
</html>

  //register.jsp 

<%@ page language="java" pageEncoding="utf-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<c:set var="bathPath" value="${pageContext.request.contextPath}"/>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
  </head>
  <body>
  	<div>
  	<form action="${bathPath}/RigesterServlet" method="post">
  		<div>
  			<label for="username" class="label">用&nbsp;户&nbsp;名:</label>
		 	 	<input type="text" name="userName"/>
		 	 	<label id="chage1"></label><br/>
		 	 	<label for="username">*正确填写用户名,6-12位之间请用英文小写、下划线、数字。</label><br/>
		 	 	<label for="pwd1">用户密码:</label>
		 	 	<input type="password" name="userPwd" /><br/>
		 	 	<label for="pwd1">*正确填写密码,6-12位之间请用英文小写、下划线、数字。</label><br/>
		 	 	<label for="pwd2">确认密码:</label>
		 	 	<input type="password" name="pwd2"/><br/>
		 	 	<label for="pwd2">*两次密码要一致,请用英文小写、下划线、数字。</label><br/>
  		</div>
  		<div>
  			<label><input type="submit" value="註冊" /></label>
  			<label><input type="reset" value="取消" /></label>
  		</div>
  	</form>
  	</div>
  </body>
</html>

 

 

  • 大小: 6.9 KB
分享到:
评论
1 楼 luo877280 2010-12-23  
 

相关推荐

    登录和注册的jsp页面+servlet+javaBean

    登录和注册的jsp页面+servlet+javaBean登录和注册的jsp页面+servlet+javaBean登录和注册的jsp页面+servlet+javaBean登录和注册的jsp页面+servlet+javaBean登录和注册的jsp页面+servlet+javaBean登录和注册的jsp页面+...

    Jsp+Servlet+JavaBean+JDBC简单登录实例

    **JSP+Servlet+JavaBean+JDBC简单登录实例详解** 在Web开发中,构建一个用户登录系统是常见的需求。本实例将详细讲解如何利用JavaServer Pages (JSP)、Servlet、JavaBean以及Java Database Connectivity (JDBC) 来...

    jsp+Servlet+javaBean实现登录注册

    3. 设计JSP页面,创建登录和注册表单,与Servlet交互。 4. 编写Servlet,处理HTTP请求,与数据库进行交互。 5. 配置Web应用的部署描述符(web.xml),设置Servlet映射和初始化参数。 6. 运行和测试系统,确保登录和...

    基于JSP+Servlet+JavaBean+JDBC+DAO的Web图书管理架构设计该系统

    基于JSP+Servlet+JavaBean+JDBC+DAO的Web架构设计该系统,进一步了解并掌握如何对数据库进行操作,以及如何分析、设计一个应用系统。 需求要求: 该系统的基本需求是,系统要实现如下的基本管理功能: (1)用户分为...

    Jsp+JavaBean+Servlet(MVC设计模式)的寝室管理系统

    Jsp+JavaBean+Servlet(MVC设计模式)的寝室管理系统 Jsp+JavaBean+Servlet(MVC设计模式)的寝室管理系统 Jsp+JavaBean+Servlet(MVC设计模式)的寝室管理系统

    JSP+JavaBean+Servlet技术实现某网站用户注册和登录功能( 源码打包)

    综上所述,JSP+JavaBean+Servlet的组合为开发人员提供了一种强大的工具,以构建高效、可维护的Web应用程序。在这个用户注册和登录的示例中,JSP用于前端展示和用户交互,JavaBean用于数据封装和业务逻辑,而Servlet...

    jsp+servlet+javaBean实现MVC

    jsp+servlet+javaBean实现MVC jsp+servlet+javaBean实现MVCjsp+servlet+javaBean实现MVCjsp+servlet+javaBean实现MVCjsp+servlet+javaBean实现MVCjsp+servlet+javaBean实现MVCjsp+servlet+javaBean实现MVCjsp+...

    基于JSP+Servlet+JavaBean实现的图书管理系统

    在Web开发中,JSP(JavaServer Pages)、Servlet和JavaBean是常见的技术栈,它们共同构建了动态、交互式的Web应用程序,尤其适用于构建功能丰富的图书管理系统。在这个系统中,JSP主要负责视图展示,Servlet处理业务...

    jsp+servlet+javaBean实现MVC源码

    jsp+servlet+javaBean实现MVC源码 jsp+servlet+javaBean实现MVC源码 jsp+servlet+javaBean实现MVC源码 jsp+servlet+javaBean实现MVC源码 jsp+servlet+javaBean实现MVC源码 jsp+servlet+javaBean实现MVC源码 jsp+...

    (Servlet+JSP+JavaBean实现)用户注册和登陆

    Servlet+JSP+JavaBean(MVC)模式适合开发复杂的web应用,在这种模式下,servlet负责处理用户请求,jsp负责数据显示,javabean负责封装数据。 Servlet+JSP、JavaBean模式程序各个模块之间层次清晰,web开发推荐采用此...

    jsp+servlet+javabean学生信息管理系统

    【标题】:“jsp+servlet+javabean学生信息管理系统”是一种基于Web的教育管理软件,它结合了三种核心技术——JavaServer Pages(JSP)、Servlet和JavaBeans,用于实现对学生信息的有效管理和操作。这个系统旨在提高...

    基于myeclipse编写javaBean+JSP+Servlet网上问卷调查系统源码.zip

    基于myeclipse编写javaBean+JSP+Servlet网上问卷调查系统源码.zip基于myeclipse编写javaBean+JSP+Servlet网上问卷调查系统源码.zip基于myeclipse编写javaBean+JSP+Servlet网上问卷调查系统源码.zip基于myeclipse编写...

    JSP+JavaBean+Servlet技术(MVC模型)

    JSP+JavaBean+Servlet技术(MVC模型) JSP+JavaBean+Servlet技术(MVC模型) JSP+JavaBean+Servlet技术(MVC模型) JSP+JavaBean+Servlet技术(MVC模型) JSP+JavaBean+Servlet技术(MVC模型) JSP+JavaBean+Servlet...

    JSP+servlet+javabean实现购物车

    在IT行业中,构建一个在线购物系统是常见的任务,而JSP、Servlet和JavaBean是JavaEE平台下开发Web应用的三大核心组件,常用于构建动态网站。本项目以"JSP+servlet+javabean实现购物车"为主题,旨在演示如何使用这些...

    JSP+Servlet+JavaBean制作简单留言板

    在本文中,我们将深入探讨如何使用JavaEE技术栈,包括JSP、Servlet和JavaBean,来构建一个简单的在线留言板系统。这个系统的核心功能是允许用户提交留言并展示其他用户的留言。我们将从整体架构、关键组件和技术原理...

    基于JSP+Servlet+JavaBean+MySQL的爱心宠物管理系统.zip

    基于JSP+Servlet+JavaBean+MySQL的爱心宠物管理系统 基于JSP+Servlet+JavaBean+MySQL的爱心宠物管理系统 基于JSP+Servlet+JavaBean+MySQL的爱心宠物管理系统 基于JSP+Servlet+JavaBean+MySQL的爱心宠物管理系统 基于...

Global site tag (gtag.js) - Google Analytics