Session完成用户登陆2

来源:互联网 发布:优化差的游戏 编辑:程序博客网 时间:2024/05/17 06:14

 

今天讲的知识是使用Session完成用户登陆,利用Session实现验证码校验,下面是我写的代码:

servlet文件----------------

package com.hbsi.servlet;

import java.io.IOException;
import java.io.PrintWriter;import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.hbsi.dao.UserDao;
import com.hbsi.domain.User;

public class LoginServlet extends HttpServlet {

public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {

request.setCharacterEncoding("UTF-8");

response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
String username = request.getParameter("username");
String password = request.getParameter("password");
String checkcode = request.getParameter("checkcode");

String imgcode = (String) request.getSession().getAttribute("imgcode");
System.out.println(imgcode);
if(checkcode!=null && imgcode!=null && checkcode.equals(imgcode)){

UserDao udao = new UserDao();
User user = udao.find(username, password);
if(user!=null){
request.getSession().setAttribute("user",user);
response.sendRedirect("/BookHistory/servlet/IndexServletShopping");
}
out.print("用户名密码错误");
}else{
out.print("验证码错误");
}
}

public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {

doGet(request, response);
}

}

html文件--------------------------

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>Login.html</title>

<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">

<script type="text/javascript">
function change(img){
img.src = img.src + "?" + new Date().getTime();
}
</script>

</head>

<body>
<form action="/BookHistory/servlet/LoginServlet" method="post">
用户名:<input type="text" name="username"/><br/>
密码:<input type="password" name="password"/><br/>
验证码:<input type="text" name="checkcode"/><img src="/BookHistory/servlet/Response3" Xonclick="change(this)"/><br/>

<input type="submit" value="登陆"/>

</form>
</body>
</html>

 

原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 四个月月经不来怎么办 快两个月没来月经了怎么办 月经停了两个月怎么办 别人诬告我我该怎么办 有人造谣我我该怎么办 宝宝晚上不睡觉哭闹怎么办 婴儿晚上不睡觉哭闹怎么办 2月宝宝排便困难怎么办 3岁宝宝老是哭闹怎么办 2岁了囟门闭合晚怎么办 宝宝卤门闭合慢怎么办 手经常碰水脱皮怎么办 迅雷文件已移除怎么办 手机不读sd卡怎么办 g买卖卖错账号怎么办 森林被野人拖走怎么办 我的世界没有羊怎么办 黑魂3杀死铁匠后怎么办 幻境7下8走错了怎么办 换了手机号微信怎么办 微信游戏没了怎么办 找sf网站被劫持怎么办 护发精油抹多了怎么办 用了护发素洗头怎么办 电脑c盘空间不足怎么办 把水蛭吃肚子里怎么办 不小心喝到蚂蟥怎么办 水蛭喝进肚子里怎么办 蚂蝗钻入皮肤里怎么办 孩子屁眼红疼怎么办啊 宝宝屁眼红疼怎么办4岁 屁股眼上火很疼怎么办 屁股上火了很痛怎么办 脚被虫子咬肿了怎么办 人在低谷的时候怎么办 支付宝忘了密码怎么办 忘了支付宝账号怎么办 支付宝账号丢了怎么办 生完孩子奶水不足怎么办 生完宝宝没奶水怎么办 生完三天没奶水怎么办