项目目录:
CodeServlet.java源码:
package com.haidi8.login;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import cn.dsna.util.images.ValidateCode;
public class CodeServlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
ValidateCode vc = new ValidateCode(110, 25, 4, 9);
//向session中保存验证码
request.getSession().setAttribute("scode", vc.getCode());
vc.write(response.getOutputStream());
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
}
DoLogin.java源码:
package com.haidi8.login;
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;
public class DoLogin 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 pwd = request.getParameter("pwd");
String code = request.getParameter("code");
//从session中获取验证码
String scode = (String) request.getSession().getAttribute("scode");
//处理业务逻辑
if("tom".equals(userName)&&"123".equals(pwd)){
//分发转向
if(!code.equalsIgnoreCase(scode)){
request.setAttribute("msg", "验证码错误");
request.getRequestDispatcher("/index.jsp").forward(request, response);
}
out.print("登录成功!");
}
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
}
web.xml代码:
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<display-name></display-name>
<servlet>
<servlet-name>codeservlet</servlet-name>
<servlet-class>com.haidi8.login.codeservlet</servlet-class>
</servlet>
<servlet>
<servlet-name>dologin</servlet-name>
<servlet-class>com.haidi8.login.dologin</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>codeservlet</servlet-name>
<url-pattern>/codeservlet</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>dologin</servlet-name>
<url-pattern>/dologin</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
index.jsp源码:
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
</head>
<body>
<%
String msg = (String)request.getAttribute("msg");
if(msg!=null){
out.print(msg);
}
%>
<form action="/session/doLogin" method="post">
用户名:<input type="text" name="userName"/><br>
密码:<input type="password" name="pwd"/><br>
验证码:<input type="text" name="code"/>
<img src="/session/codeServlet" onclick="changeCode()"/><a href="javascript:changeCode()" >看不清换一张</a><br>
<input type="submit" value="登录"/><br>
</form>
</body>
</html>
运行效果:
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
文章由极客之音整理,本文链接:https://www.bmabk.com/index.php/post/94171.html