<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>注册</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">
<meta http-equiv="content-type" content="text/html;charset=utf-8">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
<style type="text/css">
position: absolute;
left: 300px;
top: 300px;
}
.wordwidth{
width:60px;
}
</style>
<script type="text/javascript">
function change() {
var img1 = document.getElementById("img1");
img1.src = "/estore05/checkImgServlet?" + new Date().getTime();
}
</script>
</head>
<body background="${ pageContext.request.contextPath }/img/1.jpg">
<h1 style="color:black" align="center">注册</h1>
<hr>
<p style="color: red; font-weight: 900">${msg }</p>
<form action="${ pageContext.request.contextPath }/user" method="post"
id="form1">
<input type="hidden" name="method" value="regist" />
<table border="1" width="300">
<tr>
<td class="wordwidth">用户名:</td><td><input type="text" name="username" value=""/></td>
</tr>
<tr>
<td>密码:</td><td><input type="password" name="password" /></td>
</tr>
<tr>
<td>邮箱:</td><td><input type="text" name="email" value="" /></td>
</tr>
<tr>
<td>验证码:</td><td><INPUT TYPE="text" NAME="checkcode" width="20" /><img
id="img1" src="/estore05/checkImgServlet" onclick="change()"><br />
</td>
</tr>
<tr>
<td colspan="2" align="center"><input type="submit" id="reg" value="注册"
style="background-color:transparent;
border-bottom:0px;
border-left:0px;
border-right:0px;
border-top:0px;"/></td>
</tr>
</table>
</form>
</body>
</html>
这是servlet的代码:
package cn.itcast.user;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.sql.SQLException;
import java.util.Map;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.beanutils.BeanUtils;
import com.sun.mail.iap.Response;
@SuppressWarnings("all")
public class UserServlet extends HttpServlet {
public void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
/**
* 1.接收数据 2.封装数据 3.处理数据 4.显示到JSP
*/
req.setCharacterEncoding("UTF-8");
String method = req.getParameter("method");
if (method == null || "".equals(method)){
req.setAttribute("msg", "Method参数不正确");
req.getRequestDispatcher("/jsps/msg.jsp").forward(req, resp);
return;
}else if("regist".equals(method)){
regist(req,resp);
}else if("active".equals(method)){
active(req,resp);
}else if("login".equals(method)){
login(req,resp);
}else if("quit".equals(method)){
quit(req,resp);
}
}
private void quit(HttpServletRequest req, HttpServletResponse resp) {
req.getSession().invalidate();
try {
resp.sendRedirect(req.getContextPath()+"/jsps/main.jsp");
} catch (IOException e) {
e.printStackTrace();
}
}
private void login(HttpServletRequest req, HttpServletResponse resp) {
//获取数据
Map<String, String[]> map = req.getParameterMap();
//封装数据
User user = new User();
try {
BeanUtils.populate(user, map);
//处理数据
UserService us = new UserService();
User existUser = us.login(user);
//判断该用户是否存在
if(existUser == null){
req.setAttribute("msg","用户名或密码有误,或者未激活");
req.getRequestDispatcher("/jsps/msg.jsp").forward(req, resp);
}else{
req.getSession().invalidate();
//登陆成功了
req.getSession().setAttribute("existUser",existUser);
resp.sendRedirect(req.getContextPath()+"/jsps/main.jsp");
}
} catch (Exception e) {
e.printStackTrace();
}
}
private void active(HttpServletRequest req, HttpServletResponse resp) {
//jieshoushuju
String code = req.getParameter("code");
//处理数据
//通过激活码查询该用户
UserService us = new UserService();
User user = null;
try {
user = us.findByCode(code);
if(user == null){
req.setAttribute("msg", "激活码不正确");
req.getRequestDispatcher("/jsps/msg.jsp").forward(req, resp);
}else{
//激活码正确,将其状态改为1.认为是激活过的
us.modifyState(user);
req.setAttribute("msg","激活成功!请去登陆");
req.getRequestDispatcher("/jsps/msg.jsp").forward(req, resp);
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
private void regist(HttpServletRequest req, HttpServletResponse resp) {
String code1 = req.getParameter("checkcode");
String code2 = (String) req.getSession().getAttribute("checkcode");
if("".equals(code1)||!code1.equalsIgnoreCase(code2)){
try {
req.setAttribute("msg", "您输入的验证码有误");
req.getRequestDispatcher("/jsps/msg.jsp").forward(req, resp);
return;
} catch (Exception e) {
e.printStackTrace();
}
}
Map<String, String[]> map = req.getParameterMap();
//封装数据
User user = new User();
try {
if("".equals(req.getParameter("username")) && "".equals(req.getParameter("password"))){
req.setAttribute("msg", "您的输入有误");
req.getRequestDispatcher("/jsps/msg.jsp").forward(req, resp);
return;
}
BeanUtils.populate(user, map);
//处理数据
UserService us = new UserService();
us.regist(user);
//显示到jsp
req.setAttribute("msg", "注册成功,请前往邮箱激活并登陆");
req.getRequestDispatcher("/jsps/msg.jsp").forward(req, resp);
} catch (Exception e) {
e.printStackTrace();
}
}
public void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
doGet(req, resp);
}
}
注册不成功是什么意思😳