JSP+Servlet+Tomcat实现简单的用户注册,登录,查询用户信息功能

实现功能
1.注册页面添加登录链接
2.登录页面添加注册链接
3.注册成功跳转到 登录页面
4.注册失败跳转到 注册页面
5.登录失败跳转到 登录页面
6.jsp页面实现用户信息查询

一、web项目结构在这里插入图片描述
二、项目代码
1.登录Servlet服务代码

package com.zhangsan;

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.zhangsan.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 {
		// 1.首先将编码全部设置为utf-8

		// 请求中的编码设置为utf-8
		request.setCharacterEncoding("utf-8");
		// 响应中的代码设置为utf-8
		response.setCharacterEncoding("utf-8");
		// 设置响应的显示格式
		response.setContentType("text/html");

		// 2.处理请求

		// getParameter()接收post请求中的数据

		String userName = request.getParameter("userName");
		String pwd = request.getParameter("pwd");

		// 登录业务——>数据库查询——>select * from users where user_name=? and pwd=?
		// values(?,?,?,?)
		// jdbc
		// (1)调用DBUtil工具类getConn()方法连接数据库

		Connection conn = DBUtil.getConn();

		// (2)准备要执行的sql语句

		String sql = "select * from users where user_name=? and pwd=?";

		// (3)Preparestatement访问数据库

		PreparedStatement ps = null;
		// (4)定义布尔值判断登录是否成功

		boolean isTrue = false;

		ResultSet rs = null;
		try {
			ps = conn.prepareStatement(sql);

			// 占位符赋值
			ps.setString(1, userName);
			ps.setString(2, pwd);

			// 接收返回的结果集
			rs = ps.executeQuery();
			if (rs.next()) {
				isTrue = true;
			} else {
				isTrue = false;
			}

		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		// 调用DBUtil工具类close()关闭connection preparestatement resultset
		DBUtil.close(conn, ps, rs);
		PrintWriter out = response.getWriter();
		out
				.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");
		out.println("<HTML>");
		out.println("  <HEAD><TITLE>A Servlet</TITLE></HEAD>");
		out.println("  <BODY>");
		if (isTrue) {
			out.println("登录成功!");

		} else {
			out.println("登录失败!");
			// 登录失败跳转到登录界面
			response.sendRedirect("index.jsp");
		}

		out.println("  </BODY>");
		out.println("</HTML>");
		out.flush();
		out.close();
	}

}

2.注册Servlet服务代码

package com.zhangsan;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.PreparedStatement;
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.zhangsan.util.DBUtil;

public class RegServlet 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.首先将编码全部设置为utf-8

		// 请求中的编码设置为utf-8
		request.setCharacterEncoding("utf-8");
		// 响应中的代码设置为utf-8
		response.setCharacterEncoding("utf-8");
		// 设置响应的显示格式
		response.setContentType("text/html");

		// 2.处理请求

		// getParameter()接收post请求中的数据

		String name = request.getParameter("name");
		String pwd = request.getParameter("pwd");
		String userName = request.getParameter("userName");
		Integer age = Integer.parseInt(request.getParameter("age"));

		// 注册业务——>数据库增加——>insert into users(name,age,pwd,user_name)
		// values(?,?,?,?)
		// jdbc
		// (1)调用DBUtil工具类getConn()方法连接数据库

		Connection conn = DBUtil.getConn();

		// (2)准备要执行的sql语句

		String sql = "insert into users(name,age,pwd,user_name) values(?,?,?,?)";

		// (3)Preparestatement访问数据库

		PreparedStatement ps = null;
		// (4)定义布尔值判断注册是否成功

		boolean isTrue = false;
		try {
			ps = conn.prepareStatement(sql);

			// 占位符赋值
			ps.setString(1, name);
			ps.setString(2, pwd);
			ps.setString(3, userName);
			ps.setInt(4, age);

			// 接收返回受影响的行数
			int count = ps.executeUpdate();

			if (count > 0) {
				isTrue = true;
			} else {
				isTrue = false;
			}

		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		// 调用DBUtil工具类close()关闭connection preparestatement
		DBUtil.close(conn, ps, null);
		PrintWriter out = response.getWriter();
		out
				.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");
		out.println("<HTML>");
		out.println("  <HEAD><TITLE>A Servlet</TITLE></HEAD>");
		out.println("  <BODY>");
		if (isTrue) {
			out.println("注册成功!");
			// 注册成功跳转到登录界面
			response.sendRedirect("index.jsp");
		} else {
			out.println("注册失败!");
			// 注册失败跳转到注册界面
			response.sendRedirect("reg.jsp");
		}

		out.println("  </BODY>");
		out.println("</HTML>");
		out.flush();
		out.close();
	}

}

3.DBUtil工具类

package com.zhangsan.util;

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

public class DBUtil {

	static {

		try {
			Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public static Connection getConn() {

		Connection conn = null;

		try {
			conn = DriverManager.getConnection(
					"jdbc:sqlserver://localhost:1433;databaseName=MyDB", "sa",
					"1");
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();

		}

		return conn;
	}

	public static void close(Connection conn, PreparedStatement ps, ResultSet rs) {

		if (conn != null) {
			try {
				conn.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		if (ps != null) {
			try {
				ps.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		if (rs != null) {
			try {
				rs.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}

	}
}

4.登录界面

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
	String path = request.getContextPath();
	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 'login.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>

		<from>

		<body>
			<form action="loginServlet" method="post">
				<br />
				用户名:
				<input type="text" name="userName" />
				<br />
				密码:
				<input type="password" name="pwd" />
				<br />
				<input type="submit" value="登录" />

			</form>
			<a href="reg.jsp">还未注册,去注册</a>

		</body>
</html>

5.注册界面

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
	String path = request.getContextPath();
	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 'reg.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="regServlet" method="post">
			<br />
			用户名:
			<input type="text" name="userName" />
			<br />
			密码:
			<input type="password" name="pwd" />
			<br />
			确认密码:
			<input type="password" name="pwd" />
			<br />
			姓名:
			<input type="text" name="name" />
			<br />
			年龄:
			<input type="text" name="age" />
			<br />
			<input type="submit" values="注册" />

		</form>
		<a href="login.jsp">已有账号,去登陆。</a>
	</body>
</html>

5.用户信息jsp界面

<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@page import="java.sql.Connection"%>
<%@page import="java.sql.PreparedStatement"%>
<%@page import="java.sql.ResultSet"%>
<%@page import="java.sql.SQLException"%>
<%@page import="com.zhangsan.util.DBUtil"%>

<%
	String path = request.getContextPath();
	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 'userList.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>

		<table>
			<tr>

				<th>
					编号
				</th>
				<th>
					姓名
				</th>
				<th>
					年龄
				</th>
				<th>
					用户名
				</th>
				<th>
					密码
				</th>
			</tr>

			<%
				Connection conn = DBUtil.getConn();
				String sql = "select * from users ";
				PreparedStatement ps = null;
				ResultSet rs = null;
				try {
					ps = conn.prepareStatement(sql);
					rs = ps.executeQuery();
					while (rs.next()) {
			%>

			<tr>
				<td><%=rs.getInt("id")%></td>
				<td><%=rs.getString("name")%></td>
				<td><%=rs.getInt("age")%></td>
				<td><%=rs.getString("user_name")%></td>
				<td><%=rs.getString("pwd")%></td>


			</tr>
			<%
				}

				} catch (SQLException e) {
					e.printStackTrace();
				}
				DBUtil.close(conn, ps, rs);
			%>

		</table>


	</body>
</html>

6.web.xml配置界面

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" 
	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_2_5.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>loginServlet</servlet-name>
    <servlet-class>com.zhangsan.LoginServlet</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>regServlet</servlet-name>
    <servlet-class>com.zhangsan.RegServlet</servlet-class>
  </servlet>


  <servlet-mapping>
    <servlet-name>loginServlet</servlet-name>
    <url-pattern>/loginServlet</url-pattern>
  </servlet-mapping>
  <servlet-mapping>
    <servlet-name>regServlet</servlet-name>
    <url-pattern>/regServlet</url-pattern>
  </servlet-mapping>
  <welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
  </welcome-file-list>
</web-app>

三、数据库结构在这里插入图片描述四、实现效果
1.登录界面
在这里插入图片描述2.注册界面
在这里插入图片描述3.用户详情查询

在这里插入图片描述

  • 4
    点赞
  • 29
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
学生成绩管理系统是一个比较典型的Web应用程序,可以使用Java ServletJSP技术实现。下面是一个简单实现步骤: 1. 确定系统功能和设计数据库表 首先,我们需要确定学生成绩管理系统的功能需求,例如学生信息管理、课程信息管理、成绩录入和查询等等。然后,设计数据库表格以存储数据。 例如,我们可以设计以下几个表格: 学生表(Student):学生ID、姓名、性别、出生日期、联系方式等。 课程表(Course):课程ID、课程名称、学分等。 成绩表(Score):学生ID、课程ID、成绩等。 2. 创建项目和配置环境 使用IntelliJ IDEA创建一个Web项目,然后配置环境。我们需要配置Tomcat服务器、MySQL数据库连接和JDBC驱动程序等。 3. 实现数据访问层 在项目中创建一个Java类,用于实现数据库的访问操作。我们可以使用JDBC技术连接数据库,然后实现数据的增删改查操作。 4. 实现业务逻辑层 在项目中创建另一个Java类,用于实现业务逻辑。例如,实现学生信息管理、课程信息管理、成绩录入和查询功能。 5. 实现表示层 使用JSP技术实现用户界面,为用户提供交互界面。例如,实现学生信息管理页面、课程信息管理页面、成绩录入页面查询页面等。 6. 部署和测试系统 最后,我们需要将项目部署到Tomcat服务器上,并测试学生成绩管理系统的功能和性能。 总之,使用IntelliJ IDEA、Java ServletJSP、MySQLTomcat等技术实现学生成绩管理系统是非常简单的。只需要按照上述步骤一步步实现,就可以得到一个完整的Web应用程序。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值