登入界面
前台
<%@ 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;
}
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");
String sql = "select * from user where uname=?";
sta = conn.prepareStatement(sql);
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) {
e.printStackTrace();
}finally {
try {
if(res!=null) {
res.close();
}
if(sta!=null) {
sta.close();
}
if(conn!=null) {
conn.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
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())) {
req.getSession().setAttribute("user", user);
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", "*用户名或密码错误*");
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>