使用jsp+servlet+mysql+tomcat实现登录网页设计

文章申明:原文章为java jsp+servlet+mysql实现登录网页设计经阅读后重新编写了其中的代码

开发框架

IDE:Eclipse Java EE,Servers:Tomcat 8.0, DataBase:MySQL V5.7

涉及下面几个文件:

1、登录页面 login.jsp

2、成功跳转页面 success.jsp

3、失败跳转页面 fail.jsp

4、servlet 处理类 LoginTestServlet.java

5、配置文件 web.xml

-------------------------------------文章正文,请依次阅读------------------------------------------

LoginTestServlet.java内容

package com.study;

import java.io.IOException;
import java.sql.DriverManager;
import java.sql.ResultSet;

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 com.mysql.jdbc.Connection;
import com.mysql.jdbc.Statement;

/**
 * Servlet implementation class LoginTestServlet
 * @Description java+mysql+tomcat实现登录
 * @author Ken
 * @date 2017-7-16 19:44:48
 */
@WebServlet("/LoginTestServlet")
public class LoginTestServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	
	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public LoginTestServlet() {
		super();
	}
	
	/**
	 * @see HttpServlet#doGet(HttpServletRequest requset, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doPost(request, response);
	}
	
	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.setContentType("text/html;charset=gb2312");
		request.setCharacterEncoding("gb2312");
		
		String result = "";
		
		String username = request.getParameter("username");
		String psw = request.getParameter("password");
		
		if(username == "" || username == null || username.length() > 20) {
			try {
				result = "请输入用户名(不能超过20个字符)";
				request.setAttribute("message", result);
				response.sendRedirect("login.jsp");
				return;
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		
		if(psw == "" || psw == null || psw.length() > 20 ) {
			try {
				result = "请输入密码(不能超过20个字符)";
				request.setAttribute("message", result);
				response.sendRedirect("login.jsp");
				return;
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		
		//登记JDBC驱动程序
		try {
			Class.forName("com.mysql.jdbc.Driver");
		} catch (Exception e) {
			System.out.print("Class Not Found Exception");
		}
		
		//链接URL
		String url = "jdbc:mysql://localhost:3306/study";
		Connection conn = null;
		Statement stmt = null;
		ResultSet rs = null;
		
		try {
			conn = (Connection) DriverManager.getConnection(url,"root", "root567~");
			stmt = (Statement) conn.createStatement();
			
			String sql = "select * from userInfo where username='"+username+"' and userpsw= '"+psw+"'";
			rs = stmt.executeQuery(sql);
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		HttpSession session = request.getSession();
		session.setAttribute("username", username);
		
		try {
			if(rs.next()) {
				session.setAttribute("age", rs.getString("age"));
				session.setAttribute("sex", rs.getString("sex"));
				session.setAttribute("weight", rs.getString("weight"));
				response.sendRedirect("success.jsp");
			} else {
				session.setAttribute("message", "用户名或密码不匹配。");
				response.sendRedirect("fail.jsp");
				return;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}


login.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="LoginTestServlet" name="frmLogin" method="post">
		<h1 align="center">用户登录</h1>
		<center>
			<table border="1">
				<tr>
					<td>用户名:</td>
					<td>
						<input type="text" name="username" value="" size="20" maxlength="20" 
						οnfοcus="if (this.value=='Your name') this.value='';" />
					</td>
				</tr>
				<tr>
					<td>密 码:</td>
					<td>
						<input type="password" name="password" value="" size="20" maxlength="20" 
						οnfοcus="if (this.value=='Your Password') this.value='';" />
					</td>
				</tr>
			</table>
			<br />
				<input type="submit" name="submit" value="提交" οnclick="return validateLogin()">
				<input type="reset" name="Reset" value="重置" />
		</center>
	</form>
	
	<script type="text/javascript">
		function validateLogin(){
			var sUserName = document.frmLogin.username.value;
			var sPassWord = document.frmLogin.password.value;
			if(sUserName == ""){
				alert("请输入用户名!");
				return false;
			}
			
			if(sPassWord == "") {
				alert("请输入密码!");
				return false;
			}
		}
	</script>
</body>
</html>


success.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>
<%
	String username = (String)session.getAttribute("username");
	String age = (String)session.getAttribute("age");
	String weight = (String)session.getAttribute("weight");
	String sex = (String)session.getAttribute("sex");
	System.out.println("性别:A" + sex + "A");
	if (sex.trim().equals("M")) {
		sex = "男";
	}else{
		sex = "女";
	}
%>
	<div align="center">
		<%=username %>
		<p>欢迎您,登录成功!</p><br />
		<font color="blue">登录用户信息:</font>
		<table border = 1>
			<tr>
				<td>姓名:</td>
				<td> <%=username %> </td>
			</tr>
			<tr>
				<td>年龄:</td>
				<td> <%=age %> </td>
			</tr>
			<tr>
				<td>体重:</td>
				<td> <%=weight %> </td>
			</tr>
			<tr>
				<td>性别:</td>
				<td> <%=sex %> </td>
			</tr>
		</table>		
		<a href="login.jsp">返回</a>
	</div>
</body>
</html>

fail.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>
	<%
		String username = (String)session.getAttribute("username");
		String msg = (String)session.getAttribute("message");
	%>
	<div align="center">
		<%=username %>
		<p>对不起,登录失败!</p><br />
		<font color="red">原因:</font>
		<%=msg %><br />
		<br />
		<p>5秒后将返回登录界面</p>
	</div>
	
	<%
		response.setHeader("Refresh", "5;URL=/Login/login.jsp");
	%>
</body>
</html>


web.xml内容

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
  <display-name>Login</display-name>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>
  
  <!-- 添加servlet -->
  <servlet>
  	<servlet-name>LoginTestServlet</servlet-name>
  	<servlet-class>com.study.LoginTestServlet</servlet-class>
  </servlet>
  
  <servlet-mapping>
  	<servlet-name>LoginTestServlet</servlet-name>
  	<url-pattern>/loginTestServlet</url-pattern>
  </servlet-mapping>
</web-app>


对应的数据库和表,Eclipse链接MySQL的方法请自行度娘下哈

CREATE DATABASE study;

CREATE TABLE userInfo (
    id INT NOT NULL PRIMARY KEY,
    username CHAR(20) NOT NULL,
    userpsw CHAR(20),
    age INT,
    weight INT,
    sex ENUM('M', 'F')
);



附上运行后的效果






  • 0
    点赞
  • 18
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
本书共包含投票系统、通讯簿管理系统、新闻发布系统、软件下载中心、电子书店系统和论坛系统等六个系统。这六个系统均使用JSP语言和HTML标记语言编写完成的。要想运行该程序,还要进行如下操作: (1) 安装JDK1.4.0或以上版本。 (2) 安装Apache Tomcat 4.0或以上版本。 (3) 配置ODBC数据源。数据源名按各系统所使用的名称配置,具体名称如下 ① 投票系统的数据源名:vote ② 通讯簿管理系统的数据源名:user ③ 新闻发布系统的数据源名:news ④ 软件下载中心的数据源名:download ⑤ 电子书店系统的数据源名:bookstore ⑥ 论坛系统的数据源名:forum vote文件夹包含的是投票系统的源代码。投票系统分为普通用户访问界面和管理员访问界面两部分。普通用户访问界面由index.jsp页面进入,不需要用户名和密码;管理员访问界面由login.jsp页面进入,管理员用户名是:admin,密码是:admin。 userinfo文件夹包含的是通讯簿管理系统的源代码。通讯簿管理系统分为普通用户访问界面和管理员访问界面两部分。两种用户均通过login.htm页面进入系统,所使用的用户名的身份不同即进入不同的访问界面。用户名和密码可以在数据库mydb中的user数据表中查询。 news文件夹包含的是新闻发布系统的源代码。新闻发布系统分为普通用户访问界面和管理员访问界面两部分。普通用户访问界面使用index.jsp页面进入,不需要用户名和密码;管理员访问用户界面使用login.jsp页面进入,管理员的用户名是:admin,密码是:admin。 download文件夹包含的是软件下载中心的源代码。软件下载中心分为普通用户访问界面和管理员访问界面两部分。普通用户访问界面使用index.jsp页面进入,不需要用户名和密码;管理员访问用户界面使用login.jsp页面进入,管理员的用户名是:admin,密码是:admin。 bookstore文件夹包含的是电子书店的源代码。电子书店分为普通用户访问界面和管理员访问界面两部分。两种用户均通过login.jsp页面进入系统,所使用的用户名的身份不同即进入不同的访问界面。用户名和密码可以在数据库book中的user数据表中查询。 forum文件夹包含的是论坛系统的源代码。论坛系统分为普通用户访问界面、版主访问界面和管理员访问界面三部分。三种用户均通过login.jsp页面进入系统,所使用的用户名的身份不同即进入不同的访问界面。用户名和密码可以在数据库forum中的user数据表中查询。 由于编者水平有限,编写时间仓促,书中错误和不妥之处在所难免,请读者和专家批评指正。 ,

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值