代码样例:
注册RegServlet
package com.neuedu.servlet;
import java.io.IOException;
import java.io.PrintWriter;
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 com.neuedu.model.service.IUserService;
import com.neuedu.model.service.impl.UserServiceImpl;
import com.neuedu.pojo.User;
/**
* Servlet implementation class RegServlet
*/
@WebServlet("/regServlet")
public class RegServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public RegServlet() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doPost(request,response);
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.setCharacterEncoding("utf-8");
response.setCharacterEncoding("utf-8");
//接收用户输入
String name=request.getParameter("uname");
System.out.println(name);
String password=request.getParameter("paw");
String repassword=request.getParameter("paw1");
String email=request.getParameter("email");
String idCard=request.getParameter("idcard");
String[] cookieFlag=request.getParameterValues("CookieYN");
System.out.println(cookieFlag);
//打包user
User user=new User();
user.setUserName(name);
user.setUserPwd(password);
user.setUserPwd1(repassword);
user.setEmail(email);
user.setIdCard(idCard);
//调用service进行处理
IUserService userService=new UserServiceImpl();
int i=userService.addUser(user);
//返回处理结果
response.setContentType("text/html;charset=utf-8");
PrintWriter out=response.getWriter();
if(i>0){
out.println("success");
}else{
out.println("failure");
}
}
}
登录LoginServlet
package com.neuedu.servlet;
import java.io.IOException;
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 com.neuedu.model.service.IUserService;
import com.neuedu.model.service.impl.UserServiceImpl;
import com.neuedu.pojo.User;
/**
* Servlet implementation class LoginServlet
*/
@WebServlet("/loginServlet")
public class LoginServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#HttpServlet()
*/
public LoginServlet() {
super();
// TODO Auto-generated constructor stub
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doPost(request, response);
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.setCharacterEncoding("utf-8");
response.setCharacterEncoding("utf-8");
//获取页面输入的用户名和密码
String username=request.getParameter("username");
String password=request.getParameter("password");
User user=new User();
user.setUserName(username);
user.setUserPwd(password);
IUserService service=new UserServiceImpl();
User dbuser=service.getUser(user);
if(dbuser!=null){//如果数据库里有此用户
System.out.println("回首页");
//判断是普通用户还是管理员
//如果是普通用户,进首页
if(dbuser.getPower()==0){
//index.html
response.sendRedirect(request.getContextPath()+"/user/index.html");
}else{
//如果是管理员,进管理员首页
//admin/index.html
response.sendRedirect(request.getContextPath()+"/admin/index.html");
}
}else{
//回登录页重新登录
request.getRequestDispatcher("/login.html").forward(request, response);
}
}
}
接口
public interface IUserService {
int addUser(User user);
User getUser(User user);
}
public interface IUserDao {
public int add(User user);
User getUser(User user);
}
相关service
package com.neuedu.model.service.impl;
import com.neuedu.model.dao.IUserDao;
import com.neuedu.model.dao.impl.UserDaoImpl;
import com.neuedu.model.service.IUserService;
import com.neuedu.pojo.User;
public class UserServiceImpl implements IUserService {
IUserDao userDao=new UserDaoImpl();
@Override
public int addUser(User user) {
int i=0;
//验证合法性
//比如用户名是否重复
//两次密码是否一致。。。。。。
//如果验证通过,就存库
User dbuser=userDao.getUser(user);
//调用dao存库
if(dbuser!=null){//已经被注册过了
return 0;
}else{
i=userDao.add(user);
}
return i;
}
@Override
public User getUser(User user) {
return userDao.getUser(user);
}
}
dao:
package com.neuedu.model.dao.impl;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import com.neuedu.model.dao.IUserDao;
import com.neuedu.pojo.User;
import com.neuedu.utils.DBUtil;
public class UserDaoImpl implements IUserDao {
PreparedStatement ps=null;
Connection conn=null;
ResultSet rs=null;
@Override
public int add(User user) {
int i=0;
try {
conn=DBUtil.getConnection();
ps=conn.prepareStatement("INSERT INTO t_user VALUES(NULL,?,?,?,?,?)");
ps.setString(1,user.getUserName());
ps.setString(2,user.getUserPwd());
ps.setString(3,user.getEmail());
ps.setString(4,user.getIdCard());
ps.setInt(5, user.getPower());
i=ps.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
}
return i;
}
@Override
public User getUser(User user) {
User dbuser=null;
conn=DBUtil.getConnection();
try {
ps=conn.prepareStatement("select * from t_user where user_name=? and user_pwd=?");
ps.setString(1, user.getUserName());
ps.setString(2, user.getUserPwd());
rs=ps.executeQuery();
if(rs.next()){//如果有该用户
dbuser=new User();
dbuser.setId(rs.getInt("user_id"));
dbuser.setUserName(rs.getString("user_name"));
dbuser.setUserPwd(rs.getString("user_pwd"));
dbuser.setEmail(rs.getString("user_email"));
dbuser.setIdCard(rs.getString("user_idcard"));
dbuser.setPower(rs.getInt("user_power"));
}
} catch (SQLException e) {
e.printStackTrace();
}
return dbuser;
}
}