用户登录案例:
user描述类:
public class User {
private String username;
private String psw;
private String NikeName;
public User(){}
public User(String username, String psw, String nikeName) {
this.username = username;
this.psw = psw;
NikeName = nikeName;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPsw() {
return psw;
}
public void setPsw(String psw) {
this.psw = psw;
}
public String getNikeName() {
return NikeName;
}
public void setNikeName(String nikeName) {
NikeName = nikeName;
}
}
UserDB(模仿数据库):
public class UserDB {
private static List<User> list=new ArrayList<User>();
static{
list.add(new User("djw", "123", "day"));
list.add(new User("zb", "123", "bb"));
}
//判断用户名和密码是否正确
public static User findUser(String username,String psw){
for(User u:list){
if(username.equals(u.getUsername())&&psw.equals(u.getPsw())){
return u;
}
}
return null;
}
}
html登录界面:
<body>
<h1>xxx主页</h1>
<form action="/TextSession/servlet/LoginServlet" method="post">
用户名<input type="text" name="username"/><br/>
密码<input type="password" name="psw"/><br/>
<input type="text" name="code"><img src="/TextSession/servlet/CodeServlet"/><br/>
<input type="submit" value="登录">
</form>
</body>
登录Servlet类:
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
PrintWriter out=response.getWriter();
//判断是否已经登录,
HttpSession session =request.getSession();
//创建登录标记
User user=(User) session.getAttribute("user");
//已经登录的话,显示昵称,告知跳转到登录界面
if(user!=null){
response.setHeader("Refresh", "2;URL=/TextSession/servlet/IndexServlet");
out.write(user.getNikeName()+"您已经登录了,2秒后跳转到主页面");
}
//将正确的用户名和密码封装到beanutils中
User u=new User();
try {
BeanUtils.populate(u, request.getParameterMap());
} catch (Exception e) {
e.printStackTrace();
}
//判断验证吗是否正确
String code=(String) session.getAttribute("code");
String TextCode=request.getParameter("code");
if(!TextCode.equals(code)){
out.write("验证码错误,2秒后跳转到登录页面");
response.setHeader("Refresh", "2;URL=/TextSession/1.html");
return;
}
//判断用户名密码是否正确
if(UserDB.findUser(u.getUsername(),u.getPsw())==null){
response.setHeader("Refresh", "2;URL=/TextSession/1.html");
out.write("帐号密码错误,2秒后跳转到登录页面");
}else{
//将bean对象写入Session中,
HttpSession new_session=request.getSession();
new_session.setAttribute("user", u);
//登录成功,跳转到主界面
out.write("登录成功,2秒后跳转到主页面");
response.setHeader("Refresh", "2;URL=/TextSession/servlet/IndexServlet");
}
}
主页面servlet代码:
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
PrintWriter out=response.getWriter();
out.write("恭喜你来到xxxx网站");
out.write("<a href='"+request.getContextPath()+"/servlet/CancelServlet'>注销</a>");
}
注销类代码:
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
PrintWriter out=response.getWriter();
HttpSession session=request.getSession();
session.removeAttribute("user");
out.write("成功注销,2秒后回到登录页面");
response.setHeader("Refresh", "2;URL=/TextSession/1.html");
}
CodeServlet类:生成验证码:
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
int width=120;
int height=20;
BufferedImage bi=new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
//获取画笔对象
Graphics gs=bi.getGraphics();
//画边框颜色
gs.setColor(Color.blue);
gs.drawRect(0, 0, width, height);
//设置背景色
gs.setColor(Color.yellow);
gs.fillRect(1, 1, width-2, height-2);
//获取随机线条
Random rd=new Random();
gs.setColor(Color.gray);
for(int x=0;x<15;x++){
gs.drawLine(rd.nextInt(width), rd.nextInt(height), rd.nextInt(width), rd.nextInt(height));
}
//获取随机数字
gs.setColor(Color.red);
gs.setFont(new Font("宋体",Font.BOLD,18));
int new_width=20;
StringBuffer sb=new StringBuffer();
for(int x=0;x<4;x++){
int num= rd.nextInt(10);
sb.append(num);
gs.drawString(num+"", new_width, 20);
new_width+=16;
}
HttpSession session=request.getSession();
session.setAttribute("code", sb.toString());
response.setHeader("Expires", "-1");
response.setHeader("Cache-Control", "no-cache");
response.setHeader("Pragma", "no-cache");
ServletOutputStream out=response.getOutputStream();
//Image对象:输出流输出图片
ImageIO.write(bi, "jpg", out);
}