Servlet注册登录

注册登录(Servlet)

1.编写数据库

create table YhEmail(
id int primary key auto_increment,
name varchar(20),
password INT(10)
);
select * from YhEmail;

2.数据库连接

public class DbutilYhEmail {
 public Connection con=null;
 ResultSet res=null;
 PreparedStatement prd=null; 
 public Statement sta=null;
 public Connection getConnection() {
  try {
   Class.forName("com.mysql.jdbc.Driver");
   try {
    con= DriverManager.getConnection(
 "jdbc:mysql://localhost:3306/***?characterEncoding=UTF-8"
      ,"***"
      ,"***");
    System.out.println("链接数据库成功");
   } catch (SQLException e) { 
    e.printStackTrace();
   }
  } catch (ClassNotFoundException e) {
   
   e.printStackTrace();
  }
  return con;
 } 

这里的***用自己的用户密码还有表;

3.编写测试类:

public class emTest {
 public static void main(String[] args) {
  DbutilYhEmail test=new DbutilYhEmail();
  test.getConnection();
   }
}

4.用户名密码封装:

public class YhEmail {
 private int id;
 private String name;
 private String password;
 public int getId() {
  return id;
 }
 public void setId(int id) {
  this.id = id;
 }
 public String getName() {
  return name;
 }
 public void setName(String name) {
  this.name = name;
 }
 public String getPassword() {
  return password;
 }
 public void setPassword(String password) {
  this.password = password;
 }
 
 
}

5.在DbutilYhEmail里面写入录入查找用户与判断登录类

public List<YhEmail> getAll(){//查询
  List<YhEmail> list=new ArrayList<YhEmail>();
  con=this.getConnection();
  try {
   prd=con.prepareStatement("select  *  from  YhEmail");
   res=prd.executeQuery();
   while(res.next()) {
    YhEmail test=new YhEmail();
    test.setId(res.getInt(1));
    test.setName(res.getString(2));
    test.setPassword(res.getString(3));
    list.add(test);
   }
  } catch (SQLException e) {   
   e.printStackTrace();
  }finally {
   try {
    prd.close();
    con.close();
   } catch (SQLException e) { 
    e.printStackTrace();
   }     
  }return list;
 }

6.编写简单的登录界面

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>邮箱</title>
 <script type="text/javascript">
 var boo=false;
 function changepic(){
   if(boo==false){
   document.getElementById("pic").src="tou2.png";//获得指定元素user的值,并赋给name
   boo=true;
   
  }
   else{
   document.getElementById("pic").src="tou1.png";//获得指定元素user的值,并赋给name
 boo=false;

  }
 }
	
  </script>
  <style type="text/css">
  	*{  margin: 0;
  		padding: 0;
  	}
  	body{background: #999;
  	}
  	#top{width: 100%;
  		height: 400px;
  		background: #009688;
  		text-align: center;}
  		h1{padding-top: 100px;}
  		#login{width: 600px;
  			height: 200px;
  			background: #fff;
  			margin: 120px auto;
  			padding: 40px 60px;
  			line-height: 60px;
  		}
  		#login input{width: 300px;
  			height: 30px;
  			padding-left: 30px;
  			border-radius: 5px;
  			border: 1px solid #ccc;
  		}
  		#login button{
  			width: 100px;
  			height: 30px;
  			background:#009688;
  			font-size: 1.2em;
  			margin-right: 40px;
  			border-radius: 10px;
  			border: 1px solid #ccc;
  			position:relative;
  			left:-50px;}
  			#login #user{background: url(../images/username.png) no-repeat 4px;}
  			#login #pass{background: url(../images/password.png) no-repeat 4px;}
  			#top #pic{position: relative;
  				top: -480px;
  				}
  			
          #lo button{
            width: 100px;
  			height: 30px;
  			background:#009688;
  			font-size: 1.2em;
  			margin-right: 40px;
  			border-radius: 10px;
  			border: 1px solid #ccc;
  			position: relative;
            left: 770px;
            top:23px;
          }
  </style>
</head>
<body>
	<div id="top">
 		<h1>登录界面</h1>
 		<div id="login">
 			<form action="loginServlet128"   method="post" >
 				账号:<input id="user" name="name"/><br/>
 				密码:<input type="password" id="pass"  name="password" onclick="changepic()"/><br/>
 				<button type="submit">登录</button>				
 			</form>	
 		</div>		 
 		<img src="tou1.png" id="pic"  />
 	 </div>
 	 
 	 <div id="lo">
 	 <form  id="zc" action="yhlr.jsp"> 		
 			<button type="submit" >注册</button>
 			</form>
 			 </div> 
</body>
</html>

7.用户录入jsp:

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>注册</title>
</head>
<body>
<form action="YhEmailInsert" method="post">
<input  type="text"   placeholder="账户" name="name"/></br>
<input  type="text"   placeholder="密码" name="password"/></br>
<button type="submit">添加</button>
</form>
</body>
</html>

8.Servlet部分:
对用户名以及密码录入数据库为后期判断登录做准备

package EmailServlet;


import java.io.IOException;

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 javax.servlet.http.HttpSession;

import Email128.DbutilYhEmail;

/**
 * Servlet implementation class YhEmailInsert
 */
@WebServlet("/YhEmailInsert")
public class YhEmailInsert extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public YhEmailInsert() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		response.getWriter().append("Served at: ").append(request.getContextPath());
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		DbutilYhEmail bean=new DbutilYhEmail();
		request.setCharacterEncoding("utf-8");
		String   name=request.getParameter("name");
		String  password=request.getParameter("password");
		bean.YhEmailInsert(name, password);
		response.sendRedirect("index.jsp");
	}

}

9.为登录用户名密码是否准确做判断

package EmailServlet;

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

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 javax.servlet.http.HttpSession;

import Email128.DbutilYhEmail;

/**
 * Servlet implementation class loginServlet128
 */
@WebServlet("/loginServlet128")
public class loginServlet128 extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public loginServlet128() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		response.getWriter().append("Served at: ").append(request.getContextPath());
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		DbutilYhEmail bean=new DbutilYhEmail();
		request.setCharacterEncoding("utf-8");
		String   name=request.getParameter("name");
		String  password=request.getParameter("password");
		HttpSession session = request.getSession();
		session.setAttribute("name", name);
		bean.YhEmailInsert(name, password);
		System.out.println("验证成功");
		response.sendRedirect("first.jsp");
		
	}

}

10.结果展示:
dengl
在这里插入图片描述
在这里插入图片描述

  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 好的,下面是使用servlet实现注册登录的步骤: 1.创建一个servlet类,可以继承HttpServlet类。 2.在web.xml文件中配置servlet类和相应的URL模式。 3.在servlet类中实现doGet或doPost方法,处理用户提交的请求。 4.对于注册功能,可以在doPost方法中获取用户提交的表单数据,并进行验证。如果验证通过,则将用户数据保存到数据库中。 5.对于登录功能,可以在doPost方法中获取用户提交的表单数据,并根据数据库中保存的用户信息进行验证。如果验证通过,则将用户信息保存在session中。 6.在JSP页面中使用表单向servlet提交请求,实现注册和登录功能。 7.在需要进行用户验证的页面中,从session中获取用户信息进行验证,如果验证失败,则跳转到登录页面。 以上就是使用servlet实现注册登录的基本步骤,需要根据具体的业务需求进行调整和完善。 ### 回答2: Servlet注册登录是一种常见的用户认证和授权的方式,下面是一个简单的示例: 在注册页面上,用户需要提供一些必要的信息,如用户名、密码、电子邮件等。当用户提交注册表单时,Servlet会收集并验证这些信息。如果信息有效,则将用户信息保存到数据库中,并提示用户注册成功。如果信息无效,则返回注册页面并显示错误消息。注册过程也可以进行其他一些验证,如检查用户名是否已经存在等。 在登录页面上,用户需要输入已注册的用户名和密码进行验证。当用户提交登录表单时,Servlet会从数据库中检索与提供的用户名匹配的用户信息。如果找到匹配的用户信息并且密码也匹配,则认为用户登录成功,并将用户重定向到受限资源页面。如果未找到匹配的用户信息或密码不匹配,则返回登录页面并显示错误消息。 在Servlet中,可以使用Java的数据库连接API(如JDBC)来操作数据库。可以使用SQL语句执行插入、更新和查询操作。另外,为了增加安全性,密码应该使用哈希函数进行加密,并在数据库中存储哈希值而不是明文密码。 除了基本的注册和登录功能,还可以添加其他功能,如“记住我”功能、密码重置功能等。这些功能可以通过在服务器端保存或生成令牌来实现。 总之,Servlet注册登录是一种常见的用户认证和授权方式,通过收集用户信息、验证用户身份和管理会话状态来实现。合适的数据库操作和密码加密可以增加系统的安全性。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值