使用工具:
- MyExclipse10
- JDK1.7
- Navicat(Mysql)
系统数据库设计
- 根据需要创建数据库,用户表(msg_userinfo) 和短信息表(msg)
create database message;
use message;
-- 用户表
create table msg_userinfo(
username varchar(20) not null primary key comment'用户名',
password varchar(20) not null comment'密码',
email varchar(20) not null comment'邮箱地址'
)
-- 短信息表
create table mag(
msgid int not null primary key auto_increment comment'短信息id',
username varchar(20) not null comment'短信息发送方',
title varchar(40) not null comment'标题',
msgcouent varchar(400) not null comment'消息内容',
state int not null comment'消息状态',
sendto varchar(20) not null comment'短信息接收方',
msg_create_date datetime not null comment'消息发送时间'
)
#这里好像是要连主键 ,我就不连了
{/tabs-pane}
{tabs-pane label="添加数据"}
-- 用户表
insert into msg_userinfo(username,password,email)
values
('xiaoxin','123456','2534218070@qq.com'),
('zhanghao','123123','574455225@qq.com')
-- 信息表
insert into mag(username,title,msgcouent,state,sendto,msg_create_date)
values
('xiaoxin','小新博客','博客官网:www.yy0228.cn','1','zhanghao','2021-11-12'),
('zhanghao','小新主页','主页官网:yy0228.cn','1','xiaoxin','2021-11-12')
{/tabs-pane}
技术实现
<Resource
name="jdbc/message"
auth="Container"
type="javax.sql.DataSource"
driverClassName="com.mysql.jdbc.Driver"
url="jdbc:mysql://localhost:3306/message?characterEncoding=utf-8"
username="root"
password="root"
maxActive="100"
maxIdle="30"
maxWait="10000"
/>
{/tabs-pane}
{tabs-pane label="JNDI"}
package com.xyh.dao;
import java.io.IOException;
import java.io.InputStream;
import java.sql.*;
import java.util.Properties;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.sql.DataSource;
public class BaseJNDIDao {
private Connection conn;
private PreparedStatement ps;
private ResultSet rs;
/**
*1. 打开数据库连接
* @throws Exception
*/
public void openConne() throws Exception{
Context ctx = new InitialContext();
DataSource ds = (DataSource)ctx.lookup("java:comp/env/jdbc/message");
conn = ds.getConnection();
}
/**
* 3.获取ps对象的公共方法
* @param sql
* @param objs
* @throws Exception
*/
public void getps(String sql,Object...objs) throws Exception{
ps = conn.prepareStatement(sql);
for(int i = 0; i<objs.length;i++){
ps.setObject((i+1), objs[i]);
}
}
/**
* 4.通用的查询方法
* @param sql
* @param objs
* @return
* @throws Exception
*/
public ResultSet executeQuery(String sql,Object... objs) throws Exception{
getps(sql,objs);
rs = ps.executeQuery();
return rs;
}
/**
* 5.通用的增删改方法
* @param sql
* @param objs
* @return
* @throws Exception
*/
public int executeUpdate(String sql ,Object... objs) throws Exception{
getps(sql,objs);
int count = ps.executeUpdate();
return count;
}
}
{/tabs-pane}
{tabs-pane label="Userinfo实体类"}
package com.xyh.pojo;
public class Userinfo {
private String username;
private String password;
private String email;
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public Userinfo() {
super();
}
public Userinfo(String username, String password, String email) {
super();
this.username = username;
this.password = password;
this.email = email;
}
@Override
public String toString() {
return "Userinfo [username=" + username + ", password=" + password
+ ", email=" + email + "]";
}
}
{/tabs-pane}
项目需求
- 用户登录和退出
- 编写业务接口和实现类
package com.xyh.dao;
import java.util.List;
import com.xyh.pojo.Userinfo;
public interface UserinfoDao {
/**
* 用户注册 添加数据
* @param u 注册的数据
* @return 成功数
* @throws Exception
*/
int addUser(Userinfo u) throws Exception;
/**
* 判断用户是否被注册
* @param username
* @return
* @throws Exception
*/
int selUser(String username) throws Exception;
/**
* 用户登录方法
* @param uf
* @return 返回用户的数据
* @throws Exception
*/
Userinfo selLogin(Userinfo uf) throws Exception;
/**
* 查询全部用户账号
* @return
* @throws Exception
*/
List<String> selusername() throws Exception;
}
{/tabs-pane}
{tabs-pane label="UserinfoDaoImpl"}
package com.xyh.dao.Impl;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;
import com.xyh.dao.BaseJNDIDao;
import com.xyh.dao.UserinfoDao;
import com.xyh.pojo.Userinfo;
public class UserinfoDaoImpl extends BaseJNDIDao implements UserinfoDao{
@Override
public int addUser(Userinfo u) throws Exception {
this.openConne();
int count = 0;
count = executeUpdate("insert into msg_userinfo(username,password,email) values (?,?,?)", u.getUsername(),u.getPassword(),u.getEmail());
return count;
}
@Override
public int selUser(String username) throws Exception {
this.openConne();
ResultSet rs = executeQuery("select count(1) from msg_userinfo where username =?", username);
int count =0;
if(rs.next()){
count = rs.getInt(1);
}
return count;
}
@Override
public Userinfo selLogin(Userinfo uf) throws Exception {
this.openConne();
ResultSet rs = executeQuery("select * from msg_userinfo where username=? and password=?", uf.getUsername(),uf.getPassword());
Userinfo uif = new Userinfo();
if(rs.next()){
uif.setUsername(rs.getString("username"));
uif.setPassword(rs.getString("password"));
uif.setEmail(rs.getString("email"));
}
return uif;
}
@Override
public List<String> selusername() throws Exception {
this.openConne();
List<String> namelist = new ArrayList<String>();
ResultSet rs = executeQuery("select * from msg_userinfo ");
while(rs.next()){
namelist.add(rs.getString("username"));
}
return namelist;
}
}
{/tabs-pane}
package com.xyh.dao;
import java.util.List;
import com.xyh.pojo.Mag;
public interface MagDao {
/**
* 根据用户名查询出信息
* @param username
* @return
* @throws Exception
*/
List<Mag> seluser(String username) throws Exception;
/**
* 显示一条信息
* @param id
* @return
* @throws Exception
*/
Mag selMag(int id) throws Exception;
/**
* 修改信息的阅读状态
* @param id
* @return
* @throws Exception
*/
int upstate(int id) throws Exception;
/**
* 发送信息记录
* @param m
* @return
* @throws Exception
*/
int addMag(Mag m) throws Exception;
/**
* 删除新闻信息
* @param id
* @return
* @throws Exception
*/
int delMag(int id) throws Exception;
}
{/tabs-pane}
{tabs-pane label="MagDaoImpl"}
package com.xyh.dao.Impl;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;
import com.xyh.dao.BaseJNDIDao;
import com.xyh.dao.MagDao;
import com.xyh.pojo.Mag;
public class MagDaoImpl extends BaseJNDIDao implements MagDao{
@Override
public List<Mag> seluser(String username) throws Exception {
List<Mag> maglist = new ArrayList<Mag>();
this.openConne();
ResultSet rs = executeQuery("select * from mag where sendto =?", username);
while(rs.next()){
Mag mag = new Mag();
mag.setMsgid(rs.getInt("msgid"));
mag.setUsername(rs.getString("username"));
mag.setTitle(rs.getString("title"));
mag.setMsgcouent(rs.getString("msgcouent"));
mag.setState(rs.getInt("state"));
mag.setSendto(rs.getString("sendto"));
mag.setMsg_create_date(rs.getString("msg_create_date"));
maglist.add(mag);
}
return maglist;
}
@Override
public Mag selMag(int id) throws Exception {
Mag mag = new Mag();
this.openConne();
ResultSet rs = executeQuery("select * from mag where msgid=?", id);
while(rs.next()){
mag.setMsgid(rs.getInt("msgid"));
mag.setUsername(rs.getString("username"));
mag.setTitle(rs.getString("title"));
mag.setMsgcouent(rs.getString("msgcouent"));
mag.setState(rs.getInt("state"));
mag.setSendto(rs.getString("sendto"));
mag.setMsg_create_date(rs.getString("msg_create_date"));
}
return mag;
}
@Override
public int upstate(int id) throws Exception {
this.openConne();
return executeUpdate("update mag set state = 1 where msgid=?", id);
}
@Override
public int addMag(Mag m) throws Exception {
this.openConne();
return this.executeUpdate("insert into mag(username,title,msgcouent,state,sendto,msg_create_date) values (?,?,?,?,?,?)", m.getUsername(),m.getTitle(),m.getMsgcouent(),m.getState(),m.getSendto(),m.getMsg_create_date());
}
@Override
public int delMag(int id) throws Exception {
this.openConne();
return executeUpdate("delete from mag where msgid =?",id);
}
}
{/tabs-pane}
Service
package com.xyh.Service;
import java.util.List;
import com.xyh.pojo.Userinfo;
public interface UserinfoService {
/**
* 注册用户
* @param u
* @return
* @throws Exception
*/
int addUser(Userinfo u) throws Exception;
/**
* 判断用户是否被注册
* @param username
* @return
* @throws Exception
*/
int selUser(String username) throws Exception;
/**
* 用户登录方法
* @param uf
* @return 返回用户的数据
* @throws Exception
*/
Userinfo selLogin(Userinfo uf) throws Exception;
/**
* 查询全部用户账号
* @return
* @throws Exception
*/
List<String> selusername() throws Exception;
}
{/tabs-pane}
{tabs-pane label="UserinfoServiceImpl"}
package com.xyh.Service.Impl;
import java.util.List;
import com.xyh.Service.UserinfoService;
import com.xyh.dao.UserinfoDao;
import com.xyh.dao.Impl.UserinfoDaoImpl;
import com.xyh.pojo.Userinfo;
public class UserinfoSericeImpl implements UserinfoService{
UserinfoDao uf = new UserinfoDaoImpl();
@Override
public int addUser(Userinfo u) throws Exception {
return uf.addUser(u);
}
@Override
public int selUser(String username) throws Exception {
return uf.selUser(username);
}
@Override
public Userinfo selLogin(Userinfo ufd) throws Exception {
return uf.selLogin(ufd);
}
@Override
public List<String> selusername() throws Exception {
return uf.selusername();
}
}
{/tabs-pane}
package com.xyh.Service;
import java.util.List;
import com.xyh.pojo.Mag;
public interface MagService {
/**
* 根据用户名查询出信息
* @param username
* @return
* @throws Exception
*/
List<Mag> seluser(String username) throws Exception;
/**
* 显示一条信息
* @param id
* @return
* @throws Exception
*/
Mag selMag(int id) throws Exception;
/**
* 修改信息的阅读状态
* @param id
* @return
* @throws Exception
*/
int upstate(int id) throws Exception;
/**
* 发送信息记录
* @param m
* @return
* @throws Exception
*/
int addMag(Mag m) throws Exception;
/**
* 删除新闻信息
* @param id
* @return
* @throws Exception
*/
int delMag(int id) throws Exception;
}
{/tabs-pane}
{tabs-pane label="MageserviceImpl"}
package com.xyh.Service.Impl;
import java.util.List;
import com.xyh.Service.MagService;
import com.xyh.dao.MagDao;
import com.xyh.dao.Impl.MagDaoImpl;
import com.xyh.pojo.Mag;
public class MagserviceImpl implements MagService{
MagDao md = new MagDaoImpl();
@Override
public List<Mag> seluser(String username) throws Exception {
return md.seluser(username);
}
@Override
public Mag selMag(int id) throws Exception {
return md.selMag(id);
}
@Override
public int upstate(int id) throws Exception {
return md.upstate(id);
}
@Override
public int addMag(Mag m) throws Exception {
return md.addMag(m);
}
@Override
public int delMag(int id) throws Exception {
return md.delMag(id);
}
}
{/tabs-pane}
JSP页面
登录功能
<html>
<head>
<base href="<%=basePath%>">
<title>短信息管理系统</title>
</head>
<body>
<div align="center">
<form action="UserServlet" method="post">
<div><img src="static/images/sms_login_title.png"></img></div><br/><br/>
<input type="hidden" name="op" value="login"/>
<div>用户名:<input type="text" name="username"/></div><br/>
<div>密 码:<input type="password" name="password"/></div><br/><br/>
<div><input type="submit" style="background:url('static/images/sms_btn_login.png');width:87px;height:39px; border:0" value="" /> <a href="reg.jsp"><img src="static/images/sms_btn_reg.png"></img></a></div>
<br/>
<div>${msg}</div>
</form>
</div>
</body>
</html>
{/tabs-pane}
{tabs-pane label="Servlet代码"}
String username = req.getParameter("username");
String password = req.getParameter("password");
if ("".equals(username)) {
req.setAttribute("msg", "用户名为空!不能登录");
req.getRequestDispatcher("index.jsp").forward(req, resp);
} else {
if ("".equals(password)) {
req.setAttribute("msg", "密码为空!不能登录");
req.getRequestDispatcher("index.jsp").forward(req, resp);
} else {
try {
Userinfo user = ufs.selLogin(new Userinfo(req
.getParameter("username"), req
.getParameter("password")));
if (user.getUsername() == null) {
req.setAttribute("msg", "账号或密码错误!");
req.getRequestDispatcher("index.jsp").forward(req,
resp);
} else {
req.setAttribute("user", user);
req.getRequestDispatcher("admin.jsp").forward(req,
resp);
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
{/tabs-pane}
注册功能
<html>
<head>
<base href="<%=basePath%>">
<title>用户注册</title>
</head>
<body>
<div align="center">
<form action="UserServlet" method="post">
<input type="hidden" name="op" value="reg"/>
<div><img src="static/images/sms_reg_title.png"></img></div><br/><br/>
<div>用 户 名: <input type="text" name="username"/></div><br/>
<div>密 码: <input type="password" name="password"/></div><br/>
<div>确认密码: <input type="password" name="passwords"/></div><br/>
<div>邮 箱: <input type="email" name="email"/></div><br/><br/>
<div><input type="submit" style="background:url('static/images/sms_btn_reg.png');width:87px;height:39px; border:0" value="" /> <input type="reset" style="background:url('static/images/sms_btn_reset.png');width:87px;height:39px; border:0" value="" /></div>
<br/>
<div>${msg}</div>
<br/>
<div><a href="index.jsp"><img src="static/images/sms_btn_goback.png"></img></a></div>
</form>
</div>
</body>
</html>
{/tabs-pane}
{tabs-pane label="Servlet代码"}
req.setCharacterEncoding("utf-8");
UserinfoService ufs = new UserinfoSericeImpl();
if("reg".equals(req.getParameter("op"))){
String password = req.getParameter("password");
String passwords = req.getParameter("passwords");
if(password.equals(passwords)){
String username = req.getParameter("username");
try {
if(ufs.selUser(username)>0){
req.setAttribute("msg", "提示:该用户名已经被注册!");
req.getRequestDispatcher("reg.jsp").forward(req, resp);
}else{
String eamil = req.getParameter("email");
if(eamil.equals("")){
req.setAttribute("msg", "提示:邮箱不能为空哦!");
req.getRequestDispatcher("reg.jsp").forward(req, resp);
}else{
Userinfo ui = new Userinfo();
ui.setUsername(username);
ui.setPassword(passwords);
ui.setEmail(eamil);
int count = ufs.addUser(ui);
if(count>0){
req.setAttribute("msg", "提示:注册成功赶紧登录吧!");
req.getRequestDispatcher("index.jsp").forward(req, resp);
}else{
req.setAttribute("msg", "提示:不知道啥原因注册失败了");
req.getRequestDispatcher("reg.jsp").forward(req, resp);
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
}else{
req.setAttribute("msg", "两次密码不一致!");
req.getRequestDispatcher("reg.jsp").forward(req, resp);
}
}
{/tabs-pane}
主界面
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'admin.jsp' starting page</title>
<style type="text/css">
a {
text-decoration: none;
color: #000;
}
.da {
width: 1000px;
height: 800px;
padding-top: 68px;
padding-right: 20px;
margin: 0 auto;
background: url("static/images/sms_home_bg.png") no-repeat;
margin: 0 auto
}
.da1 {
padding-left: 30px;
padding-right: 30px;
}
.da2 {
padding-top: 28px;
padding-left: 60px;
padding-right: 60px;
}
</style>
</head>
<body>
<div class="da">
<div class="da1">
<div align="left">
<img src="static/images/sms_my_message.png"></img>
</div>
<div align="right">
<p>
当前用户:${username} <a href="Magservlet?op=mag">发送消息</a> <a
href="UserServlet?op=del">退出</a>
</p>
</div>
</div>
<div class="da2">
<!-- 判断是否有数据 -->
<c:if test="${not empty Maglist }">
<c:forEach var="ms" items="${Maglist}" varStatus="st">
<div style="overflow:hidden">
<span style="float:left;"> <a href="Magservlet?op=sel&msgid=${ms.msgid }">
<c:choose>
<c:when test="${ms.state==0 }">
<img src="static/images/sms_unReaded.png"></img>
</c:when>
<c:when test="${ms.state==1 }">
<img src="static/images/sms_readed.png"></img>
</c:when>
</c:choose> <span>${ms.title }</span> <span>${ms.msgcouent}</span>
</a> </span> <span style="float:right;"> <a href="Magservlet?op=del&id=${ ms.msgid}">删除</a> <a href="Magservlet?op=mag&id=${ ms.msgid}">回信</a>
<a href=""#>${ms.msg_create_date }</a> </span>
</div>
</c:forEach>
</c:if>
</div>
</div>
</body>
</html>
{/tabs-pane}
发送信息
<html>
<head>
<base href="<%=basePath%>">
<title>发送信息</title>
<style type="text/css">
a {
text-decoration: none;
color: #000;
}
.da {
width: 1000px;
height: 800px;
padding-top: 68px;
padding-right: 20px;
margin: 0 auto;
background: url("static/images/sms_home_bg.png") no-repeat;
margin: 0 auto
}
.da1 {
padding-left: 30px;
padding-right: 30px;
}
.da2 {
padding-top: 28px;
padding-left: 60px;
padding-right: 60px;
}
</style>
</head>
<body>
<div class="da">
<div class="da1">
<div align="left">
<img src="static/images/sms_my_message.png"></img>
</div>
${msg }
<div align="right">
<p>
当前用户:${username} <a href="#">发送消息</a> <a
href="UserServlet?op=del">退出</a>
</p>
</div>
</div>
<div class="da2">
<form action="Magservlet?op=add" method="post">
<div>
<span> 发送给 : <select name="user">
<c:if test="${not empty userlist}">
<c:forEach var="names" items="${userlist }">
<c:if test="${names.equals(Mag.username)}">
<option selected="selected">${names}</option>
</c:if>
<c:if test="${not names.equals(Mag.username)}">
<option>${names}</option>
</c:if>
</c:forEach>
</c:if>
</select> </span>
<span>
标题:<input type="text" name="title" required="required" value="${Mag.title }"/>
</span>
</div>
<br/>
<div align="center">
<textarea rows="25" required="required" cols="100" name="msgcouent"></textarea>
</div>
<br/>
<div align="center">
<input type="submit" style="background:url('static/images/sms_send.gif');width:120px;height:30px; border:0" value="" />
</div>
</form>
</div>
</div>
</body>
</html>
{/tabs-pane}
Servlet代码
package com.xyh.Servlet;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import com.xyh.Service.MagService;
import com.xyh.Service.UserinfoService;
import com.xyh.Service.Impl.MagserviceImpl;
import com.xyh.Service.Impl.UserinfoSericeImpl;
import com.xyh.pojo.Mag;
import com.xyh.pojo.Userinfo;
public class Magservlet extends HttpServlet{
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
doPost(req, resp);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
MagService ms = new MagserviceImpl();
req.setCharacterEncoding("utf-8");
String op = req.getParameter("op");
List<Mag> maglist = new ArrayList<Mag>();
if("sel".equals(op)){
int id = Integer.parseInt(req.getParameter("msgid"));
try {
ms.upstate(id);
req.setAttribute("Mag",ms.selMag(id));
req.getRequestDispatcher("config.jsp").forward(req, resp);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}else if("mag".equals(op)){
try {
UserinfoService ufs = new UserinfoSericeImpl();
String idstr = req.getParameter("id");
if(idstr != null){
int id = Integer.parseInt(idstr);
req.setAttribute("Mag",ms.selMag(id));
}
List<String> userlist = ufs.selusername();
if(userlist.size()>0){
req.setAttribute("userlist", userlist);
req.getRequestDispatcher("send.jsp").forward(req, resp);
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}else if("add".equals(op)){
String sendto = req.getParameter("user");
String title =req.getParameter("title");
String msgcouent = req.getParameter("msgcouent");
int state = 0;
HttpSession session = req.getSession();
String username = (String) session.getAttribute("username");
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
String time = sdf.format(new Date())+"";
Mag mg = new Mag(username,title,msgcouent,state,sendto,time);
try {
int count = ms.addMag(mg);
if(count >0){
req.setAttribute("msg", "发送成功!");
req.getRequestDispatcher("Magservlet?op=mag").forward(req, resp);
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//删除信息功能
}else if("del".equals(op)){
try {
int count = ms.delMag(Integer.parseInt(req.getParameter("id")));
HttpSession session = req.getSession();
String username = (String) session.getAttribute("username");
req.setAttribute("Maglist", ms.seluser(username));
req.getRequestDispatcher("admin.jsp").forward(req,resp);
} catch (NumberFormatException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
{/tabs-pane}
{tabs-pane label="UserServlet"}
package com.xyh.Servlet;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import com.xyh.Service.MagService;
import com.xyh.Service.UserinfoService;
import com.xyh.Service.Impl.MagserviceImpl;
import com.xyh.Service.Impl.UserinfoSericeImpl;
import com.xyh.pojo.Userinfo;
public class UserServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
doPost(req, resp);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
req.setCharacterEncoding("utf-8");
UserinfoService ufs = new UserinfoSericeImpl();
HttpSession session = req.getSession();
if ("reg".equals(req.getParameter("op"))) {
String password = req.getParameter("password");
String passwords = req.getParameter("passwords");
if (password.equals(passwords)) {
String username = req.getParameter("username");
try {
if (ufs.selUser(username) > 0) {
req.setAttribute("msg", "提示:该用户名已经被注册!");
req.getRequestDispatcher("reg.jsp").forward(req, resp);
} else {
String eamil = req.getParameter("email");
if (eamil.equals("")) {
req.setAttribute("msg", "提示:邮箱不能为空哦!");
req.getRequestDispatcher("reg.jsp").forward(req,
resp);
} else {
Userinfo ui = new Userinfo();
ui.setUsername(username);
ui.setPassword(passwords);
ui.setEmail(eamil);
int count = ufs.addUser(ui);
if (count > 0) {
req.setAttribute("msg", "提示:注册成功赶紧登录吧!");
req.getRequestDispatcher("index.jsp").forward(
req, resp);
} else {
req.setAttribute("msg", "提示:不知道啥原因注册失败了");
req.getRequestDispatcher("reg.jsp").forward(
req, resp);
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
} else {
req.setAttribute("msg", "两次密码不一致!");
req.getRequestDispatcher("reg.jsp").forward(req, resp);
}
} else if ("login".equals(req.getParameter("op"))) {
String username = req.getParameter("username");
String password = req.getParameter("password");
if ("".equals(username)) {
req.setAttribute("msg", "用户名为空!不能登录");
req.getRequestDispatcher("index.jsp").forward(req, resp);
} else {
if ("".equals(password)) {
req.setAttribute("msg", "密码为空!不能登录");
req.getRequestDispatcher("index.jsp").forward(req, resp);
} else {
try {
Userinfo user = ufs.selLogin(new Userinfo(req
.getParameter("username"), req
.getParameter("password")));
if (user.getUsername() == null) {
req.setAttribute("msg", "账号或密码错误!");
req.getRequestDispatcher("index.jsp").forward(req,
resp);
} else {
/*req.setAttribute("user", user);*/
session.setAttribute("username",username );
MagService ms = new MagserviceImpl();
req.setAttribute("Maglist", ms.seluser(user.getUsername()));
req.getRequestDispatcher("admin.jsp").forward(req,
resp);
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}else if("del".equals(req.getParameter("op"))){
//使session立即失效
session.invalidate();
resp.sendRedirect("index.jsp");
}
}
}
{/tabs-pane}
Thanks a lot. I like it.