servlet连接数据库

登入界面

前台
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>登入</title>
</head>
<body>
	<form action="/loginconnect/login" method="post" id="loginform">
		账号:<input type="text" id="uname" name="uname" /></br>
		密码:<input type="password" id="upwd" name="upwd" /></br>
		<input id="btn" type="button" value="登入" onclick="dengru();" /></br>
		<span id="error" style="color:red">${msg}</span>
	</form>
	<script>
		function dengru(){
			var uname = document.getElementById("uname");
			var upwd = document.getElementById("upwd");
			if(uname.value == "" || upwd.value == ""){
				document.getElementById("error").innerHTML="用户密码不能为空";
				return;
			}
			document.getElementById("loginform").submit();
		}
	</script>
</body>
</html>
servlet后台
package com.mage.servlet;

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

import javax.jws.WebService;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.mage.po.User;


@WebServlet("/login")
public class LoginServlet extends HttpServlet{
	private static final long SerialVersionUID = 1L;
	@Override
	protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		String uname = req.getParameter("uname");
		String upwd = req.getParameter("upwd");
		if(uname==null||"".equals(uname)) {
			req.setAttribute("upwd", upwd);
			req.setAttribute("msg", "账号不能为空");
			req.getRequestDispatcher("/login.jsp").forward(req, resp);
			return;
		}
		if(upwd == null || "".equals(upwd)) {
			req.setAttribute("uname", uname);
			req.setAttribute("msg", "密码不能为空");
			req.getRequestDispatcher("/login.jsp").forward(req, resp);
			return;
		}
		// JDBC通过用户名查询数据库中的用户信息  user
		User user = null;
		Connection conn = null;
		PreparedStatement sta = null;
		ResultSet res = null;
		try {
			// 加载驱动
			Class.forName("com.mysql.jdbc.Driver");
			//建立连接
			conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/logindemo", "root", "123456");
			// 编写sql
			String sql = "select * from user where uname=?";
			// 预编译
			sta = conn.prepareStatement(sql);
			// 设置参数,下标从1开始
			sta.setString(1, uname);
			// 执行查询,得到结果集
			res = sta.executeQuery();
			// 分析结果集
			while(res.next()) {
				user = new User();
				user.setId(res.getInt("id"));
				user.setUname(uname);
				user.setUpwd(res.getString("upwd"));
				user.setNick(res.getString("nick"));
				user.setHead(res.getString("head"));
				user.setSex(res.getInt("sex"));
				user.setAge(res.getInt("age"));
				user.setAddress(res.getString("address"));
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally {
			try {
				if(res!=null) {
					res.close();
				}
				if(sta!=null) {
					sta.close();
				}
				if(conn!=null) {
					conn.close();
				}
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		//判断user是否为空
		if(user == null) {
			req.setAttribute("uname", uname);
			req.setAttribute("upwd", upwd);
			req.setAttribute("msg", "账户不存在");
			req.getRequestDispatcher("/login.jsp").forward(req, resp);
			return;
		}
		// 判断前台传过来的密码是否和数据库中的密码一致
		if(upwd.equals(user.getUpwd())) {
			// 将用户信息user存到session中
			req.getSession().setAttribute("user", user);
			// 将用户名和密码存到Cookie中
			Cookie cookie = new Cookie("user", uname+"---"+upwd);
			cookie.setMaxAge(7*24*60*60);
			resp.addCookie(cookie);
			// 重定向跳转到首页
			resp.sendRedirect("/loginconnect/index.jsp");
			return;
		}
		// 不一致:将用户名和密码以及提示信息存到域对象中
		req.setAttribute("uname", uname);
		req.setAttribute("upwd", upwd);
		req.setAttribute("msg", "*用户名或密码错误*");
		// 请求转发跳转到login.jsp
		req.getRequestDispatcher("/login.jsp").forward(req, resp);
		
	}
	
}

首页
<%@page import="com.mage.po.User" %>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body style="background-color: black; color: white;text-align: center">
	<h1>欢迎来到万达爷工作室</h1>
	<img src="statics/${user.head }">
	<h2>用户名:${user.uname }</h2>
	<h2>密码:${user.upwd }</h2>
	<h2>昵称:${user.nick }</h2>
	<%
		User user = (User)session.getAttribute("user");
		if(user != null && user.getSex() == 1){
			out.write("<h2>性别:男</h2>");
		}else if(user != null && user.getSex() != 1){
			out.write("<h2>性别:女</h2>");
		}else{
			out.write("<h2>性别:不详</h2>");
		}
	%>
	<h2>年龄:${user.age }</h2>
	<h2>地址:${user.address }</h2>
</body>
</html>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值