<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ page import="sun.security.util.Password" %>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html lang="en">
<head>
<meta charset="UTF-8"/>
<title>Document</title>
<script type="text/javascript" src="static/js/jquery-1.9.0.min.js"></script>
<script type="text/javascript" src="static/js/login.js"></script>
<link href="static/css/login2.css" rel="stylesheet" type="text/css"/>
</head>
<body>
<h1>
生鲜管理系统<sup>V2017</sup>
</h1>
<div class="login" style="margin-top: 50px;">
<div class="header">
<div class="switch" id="switch">
<a class="switch_btn_focus" id="switch_qlogin"
href="javascript:void(0);" tabindex="7">快速登录</a> <a
class="switch_btn" id="switch_login" href="javascript:void(0);"
tabindex="8">快速注册</a>
<div class="switch_bottom" id="switch_bottom"
style="position: absolute; width: 64px; left: 0px;"></div>
</div>
</div>
<div class="web_qr_login" id="web_qr_login"
style="display: block; height: 235px;">
<!--登录-->
<div class="web_login" id="web_login">
<div class="login-box">
<div class="login_form">
<form action="user"
accept-charset="utf-8" id="login_form" class="loginForm"
method="post">
<input type="hidden" name="method" value="login"/>
<input type="hidden" name="did" value="0"/> <input type="hidden"
name="to" value="log"/>
<div class="uinArea" id="uinArea">
<label class="input-tips" for="u">帐号:</label>
<div class="inputOuter" id="uArea">
<input type="text" id="u" name="name" value="${cookie.name.value}" class="inputstyle"/>
</div>
</div>
<div class="pwdArea" id="pwdArea">
<label class="input-tips" for="p">密码:</label>
<div class="inputOuter" id="pArea">
<input type="password" id="p" name="password" value="${cookie.password.value}" class="inputstyle"/>
</div>
</div>
<div style="margin-left: 45px;margin-top: 10px">
<input style="vertical-align: middle;" type="checkbox" value="yes" name="remember">记住密码<br/>
</div>
<div style="padding-left: 50px; margin-top: 20px;">
<input type="submit" value="登 录" style="width: 150px;"
class="button_blue"/>
</div>
</form>
</div>
</div>
</div>
<!--登录end-->
</div>
<!--注册-->
<div class="qlogin" id="qlogin" style="display: none;">
<div class="web_login">
<form name="form2" id="regUser" accept-charset="utf-8"
action="register" method="post">
<input type="hidden" name="to" value="reg"/> <input type="hidden"
name="did" value="0"/>
<ul class="reg_form" id="reg-ul">
<div id="userCue" class="cue">快速注册请注意格式</div>
<li><label for="user" class="input-tips2">用户名:</label>
<div class="inputOuter2">
<input type="text" id="user" name="name" maxlength="16"
class="inputstyle2"/>
</div>
</li>
<li><label for="passwd" class="input-tips2">密码:</label>
<div class="inputOuter2">
<input type="password" id="passwd" name="password" maxlength="16"
class="inputstyle2"/>
</div>
</li>
<li><label for="passwd2" class="input-tips2">确认密码:</label>
<div class="inputOuter2">
<input type="password" id="passwd2" name="password2" maxlength="16"
class="inputstyle2"/>
</div>
</li>
<li><label for="email" class="input-tips2">邮箱:</label>
<div class="inputOuter2">
<input type="email" id="email" name="email" class="inputstyle2"/>
</div>
</li>
<li>
<div class="inputArea">
<input type="submit" id="reg"
style="margin-top: 10px; margin-left: 85px;"
class="button_blue" value="同意协议并注册"/> <a href="#" class="zcxy"
target="_blank.">注册协议</a>
</div>
</li>
<div class="cl"></div>
</ul>
</form>
</div>
</div>
<!--注册end-->
</div>
</body>
</body>
</html>
@WebServlet(name = "UserServlet",urlPatterns = "/user")
public class UserServlet extends BaseServlet {
public void login(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String name = request.getParameter("name");
String password = request.getParameter("password");
UserService userService=new UserService();
User user=null;
try {
//调用service中登录方法
user = userService.login(name, password);
} catch (SQLException e) {
e.printStackTrace();
}
if (user!=null){
//登录成功后我们再获取是否保存密码的信息,如果失败了保存密码就没有意义了
String remember = request.getParameter("remember");
if (remember!=null&&remember.equals("yes")){
// 将用户名和密码加入到cookie中
Cookie nameCookie = new Cookie("name", name);
Cookie passwordCookie = new Cookie("password", password);
//设置cookie的有效期 防止销毁
nameCookie.setMaxAge(60*10);
passwordCookie.setMaxAge(60*10);
//将cookie发送给客户端保存
response.addCookie(nameCookie);
response.addCookie(passwordCookie);
}
//登录成功跳转生鲜种类列表界面
response.sendRedirect(request.getContextPath()+"/category?method=getCategoryList");
}else {
//登录失败提示
response.setContentType("text/html;charset=utf-8");
response.getWriter().write("用户登录失败");
}
}
public void register(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String name = request.getParameter("name");
String password = request.getParameter("password");
String email = request.getParameter("email");
UserService userService=new UserService();
boolean register = userService.register(name,password,email);
if (register) {
response.sendRedirect(request.getContextPath()+"login.jsp");
}else {
response.setContentType("text/html;charset=utf-8");
response.getWriter().write("注册失败");
}
}
}
package net.zixue.web;
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 java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
/**
* Created by Administrator on 2017/7/8.
*/
@WebServlet(name = "BaseServlet")
public class BaseServlet extends HttpServlet {
@Override
protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
req.setCharacterEncoding("utf-8");
try {
// 1 获取方法名字
String method = req.getParameter("method");
// 2 获取到当前对象的字节码文件
Class clazz=this.getClass();
// 3 拿到字节码对象中的方法
Method clazzMethod = clazz.getMethod(method, HttpServletRequest.class, HttpServletResponse.class);
// 4 执行方法
clazzMethod.invoke(this,req,resp);
} catch (NoSuchMethodException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
}
}