Java教程

使用httpsession实现验证登录

本文主要是介绍使用httpsession实现验证登录,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

项目目录:

 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>

运行效果:

 

这篇关于使用httpsession实现验证登录的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!