利用servlet实现简单的登陆以及注册功能

注册登陆界面

1.首先创建login.jsp登陆页面

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>
 <form action="login" method="post">
 姓名: <input type="text" name="name"><br>
 密码: <input type="text" name="pwd"><br>
 用户名:<input type="text" name="userName"><br>
 年龄: <input type="text" name="age"><br>
  	  <input type="submit" value="注册">
  <a href="">登录</a>
</form> 
  </body>
</html>

在写form表单的时候有两个点需要注意,分别是actionmethod

action:下面的表单拥有两个输入字段以及一个提交按钮,当提交表单时,表单数据会提交到名为 action="xxxxxx"的页面

method: 属性规定浏览器使用 method 属性设置的方法将表单中的数据传送给服务器进行处理。共有两种方法:POST 方法和 GET 方法

2.再新建utils包,创建一个公共工具DBUtils类 -----公共工具DBUtils类(此工具类解决代码冗余问题)
DBUtils简化了JDBC的开发步骤,使得我们可以用更少量的代码实现连接数据库的功能

  1. // 1.加载驱动
    
  2. // 2.使用DriverManager建立到数据库的连接
    
  3. // 7.关闭资源
    
package utils;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class DBUtils {
	// 1.加载驱动
	static {
		try {
			Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		}
	}

	// 2.使用DriverManager建立到数据库的连接
	public static Connection getConn() {
		Connection conn = null;
		try {
			conn = DriverManager.getConnection(
					"jdbc:sqlserver://localhost:1433;databaseName=school",
					"sa", "1");
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return conn;
	}

	// 7.关闭资源
	public static void close(Connection conn, PreparedStatement pt, ResultSet rt) {

		try {
			if (pt != null)
				pt.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		try {
			if (rt != null)
				rt.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		try {
			if (conn != null)
				conn.close();
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}

}

最后就是编写LoginServlet代码如下:

package org.yxd;

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 jdbc.utils.DBUtils;

public class login 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 {
		// 1.设置编码
		request.setCharacterEncoding("UTF-8");
		response.setCharacterEncoding("UTF-8");
		// 设置响应的格式(类型)
		response.setContentType("text/html");
		
		// 2.接收请求中的参数
		String name =request.getParameter("name");
		String pwd =request.getParameter("pwd");
		//jdbc的相关代码
		      boolean a = false;
		//1.获取工具类
		Connection conn =DBUitl.getConn();
		//2.准备sql的代码
		String sql = "SELECT * FROM students WHERE name =? and pwd =?";
		// 3.准备一个preparedstatement 对象准备和数据库做交互
		PreparedStatement pt =null;
		ResultSet rt=null;
		try {
			pt=conn.prepareStatement(sql);
			pt.setString(1, name);
			pt.setString(2, pwd);
			rt=pt.executeQuery();
			 if(rt.next()){
				 a=true;
			 }else {
				 a=false;
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		
		// 3.发送响应
		PrintWriter out = response.getWriter();
		if (a) {
			out.print("登录成功");
		} else {
			out.print("登录失败");
		}
		out.flush();
		out.close();
		
		
	}

}

注册同理可得


版权声明:本文为Yu_lumingfei原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。