与数据库进行交互,实现登录注册功能

注册:需要先进行注册就是通过页面在数据库存入数据
登录:首先查询数据库是否有你输入的数据,有数据就登录成功
这里使用的数据库是SQL server数据库,想要换其他数据库只需要换成所对应的jar包和修改数据库连接,就可以了。
关于代码用到的工具插件自己找:jQuery.js,sqljdbc4.jar
DBUtil.java:https://blog.csdn.net/weixin_43330884/article/details/102828455
注册页面代码:add.jsp

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<!DOCTYPE HTML>
<html>
  <head>
    <title>注册</title>
    <script type="text/javascript" src="js/jQuery.js"></script>
  </head>
  <body>
    <form action="AddServlet" method="post">
    	用户名:<input type="text" name="name" id="name"/><br/>
    	密码:<input type="text" name="pwd" id="pwd"/><br/>
    	<input type="submit" value="注册"/><br/>
    </form>
    <script type="text/javascript">
    	$(function(){
    		$("form").submit(function(){
    			var name = $("#name").val();
    			var pwd = $("#pwd").val();
    			var nameFlag = false;
    			var pwdFlag = false;
    			
    			if(name == "" || name == null)
    				alert("用户名不能为空");
    				nameFlag = false;
    			esle
    				nameFlag = true;
    			
    			if(pwd == "" || pwd == null)
    				alert("密码不能为空");
    				pwdFlag = false;
    			esle
    				pwdFlag = true;
    			
    			if(nameFlag&&pwdFlag)
    				return true;
    			esle
    				return false;
    		});
    	});
    </script>
  </body>
</html>

下边是一些验证,没有特殊要求可以不要;
登录:login.jsp

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>

<!DOCTYPE HTML>
<html>
  <head> 
    <title>登录</title>
    <script type="text/javascript" src="js/jQuery.js"></script>
  </head>
  <body>
    <form action="LoginServlet" method="post">
    	用户名:<input type="text" name="name" id="name"/><br/>
    	密码:<input type="text" name="pwd" id="pwd"/><br/>
    	<input type="submit" value="登录"/><br/>
    </form>
    <script type="text/javascript">
    	$(function(){
    		$("form").submit(function(){
    			var name = $("#name").val();
    			var pwd = $("#pwd").val();
    			var nameFlag = false;
    			var pwdFlag = false;
    			if(name == ""|| name==null){
    			alert("用户名不能为空");
    				nameFlag = false;
    			}else{
    				nameFlag = true;
    			}
    			if(pwd == ""|| pwd==null){
    			alert("密码不能为空");
    				pwdFlag = false;
    			}else{
    				pwdFlag = true;
    			}
    			if(nameFlag && pwdFlag){
    				return true;
    			}else{
    				return false;
    			}
    		});
    	});
    </script>
  </body>
</html>

当然也可以都写一个页面方便,由于这是小作业我就分开写了方便他人看懂。
登录成功界面:哈哈哈哈,这就是个测试不要介意

  <body>
    登录成功
  </body>

后端代码之注册:AddServlet.java:

package com.chaz.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

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

import com.chaz.util.DBUtil;

public class AddServlet 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 {
		//解决中文编码问题
		response.setContentType("text/html;charset=utf-8");
		request.setCharacterEncoding("utf-8");
		String name = request.getParameter("name");//获取输入框的值
		String pwd = request.getParameter("pwd");
		Connection conn = DBUtil.getConn();//连接数据库
		PreparedStatement ps = null;
		String sql = "insert into userInfo(name,pwd) values(?,?)";//sql语句
		
		try {
			ps = conn.prepareStatement(sql);
			ps.setString(1, name);
			ps.setString(2, pwd);
			int count = ps.executeUpdate();
			
			if(count>0){
				System.out.println("插入数据成功");
				response.sendRedirect("login.jsp");//成功就跳转登录界面
			}else{
				System.out.println("失败");
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}	
	}
}

登录:LoginServlet.java:

package com.chaz.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

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

import com.chaz.util.DBUtil;

public class LoginServlet 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 {

		response.setContentType("text/html;charset=utf-8");
		request.setCharacterEncoding("utf-8");
		String name = request.getParameter("name");//获取输入框的值
		String pwd = request.getParameter("pwd");
		Connection conn = DBUtil.getConn();//连接数据库
		PreparedStatement ps = null;
		ResultSet rs = null;
		String sql = "select * from userInfo where name=? and pwd=?";
		try {
			ps = conn.prepareStatement(sql);
			ps.setString(1, name);
			ps.setString(2, pwd);
			rs = ps.executeQuery();
			if(rs.next()){
				response.sendRedirect("index.jsp");
			}else{
				//输入错误则从新登录
				response.sendRedirect("login.jsp");
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}

web.xml:

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0"
    xmlns="http://java.sun.com/xml/ns/javaee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
  <servlet>
    <description>This is the description of my J2EE component</description>
    <display-name>This is the display name of my J2EE component</display-name>
    <servlet-name>AddServlet</servlet-name>
    <servlet-class>com.chaz.servlet.AddServlet</servlet-class>
  </servlet>
  <servlet>
    <description>This is the description of my J2EE component</description>
    <display-name>This is the display name of my J2EE component</display-name>
    <servlet-name>LoginServlet</servlet-name>
    <servlet-class>com.chaz.servlet.LoginServlet</servlet-class>
  </servlet>

  <servlet-mapping>
    <servlet-name>AddServlet</servlet-name>
    <url-pattern>/AddServlet</url-pattern>
  </servlet-mapping>
  <servlet-mapping>
    <servlet-name>LoginServlet</servlet-name>
    <url-pattern>/LoginServlet</url-pattern>
  </servlet-mapping>

</web-app>

你可能感兴趣的:(JavaWeb)